com.pubnub.api.presence.eventengine.effect.PresenceEffectFactory.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pubnub-kotlin Show documentation
Show all versions of pubnub-kotlin Show documentation
PubNub is a cross-platform client-to-client (1:1 and 1:many) push service in the cloud, capable of
broadcasting real-time messages to millions of web and mobile clients simultaneously, in less than a quarter
second!
package com.pubnub.api.presence.eventengine.effect
import com.pubnub.api.eventengine.Effect
import com.pubnub.api.eventengine.EffectFactory
import com.pubnub.api.eventengine.Sink
import com.pubnub.api.presence.eventengine.effect.effectprovider.HeartbeatProvider
import com.pubnub.api.presence.eventengine.effect.effectprovider.LeaveProvider
import com.pubnub.api.presence.eventengine.event.PresenceEvent
import com.pubnub.api.subscribe.eventengine.effect.RetryPolicy
import java.time.Duration
import java.util.concurrent.ScheduledExecutorService
internal class PresenceEffectFactory(
private val heartbeatProvider: HeartbeatProvider,
private val leaveProvider: LeaveProvider,
private val presenceEventSink: Sink,
private val policy: RetryPolicy,
private val executorService: ScheduledExecutorService,
private val heartbeatInterval: Duration,
) : EffectFactory {
override fun create(effectInvocation: PresenceEffectInvocation): Effect? {
return when (effectInvocation) {
is PresenceEffectInvocation.Heartbeat -> {
val heartbeatRemoteAction = heartbeatProvider.getHeartbeatRemoteAction(
effectInvocation.channels,
effectInvocation.channelGroups
)
HeartbeatEffect(heartbeatRemoteAction, presenceEventSink)
}
is PresenceEffectInvocation.DelayedHeartbeat -> {
val heartbeatRemoteAction = heartbeatProvider.getHeartbeatRemoteAction(
effectInvocation.channels,
effectInvocation.channelGroups
)
DelayedHeartbeatEffect(
heartbeatRemoteAction,
presenceEventSink,
policy,
executorService,
effectInvocation
)
}
is PresenceEffectInvocation.Leave -> {
val leaveRemoteAction = leaveProvider.getLeaveRemoteAction(
effectInvocation.channels,
effectInvocation.channelGroups
)
LeaveEffect(leaveRemoteAction)
}
is PresenceEffectInvocation.Wait -> {
WaitEffect(heartbeatInterval, presenceEventSink)
}
PresenceEffectInvocation.CancelDelayedHeartbeat,
PresenceEffectInvocation.CancelWait
-> null
}
}
}