com.pubnub.api.subscribe.eventengine.effect.SubscribeEffectFactory.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.subscribe.eventengine.effect
import com.pubnub.api.endpoints.remoteaction.RemoteAction
import com.pubnub.api.eventengine.Effect
import com.pubnub.api.eventengine.EffectFactory
import com.pubnub.api.eventengine.Sink
import com.pubnub.api.models.consumer.pubsub.PNEvent
import com.pubnub.api.subscribe.eventengine.effect.effectprovider.HandshakeProvider
import com.pubnub.api.subscribe.eventengine.effect.effectprovider.ReceiveMessagesProvider
import com.pubnub.api.subscribe.eventengine.event.SubscribeEvent
import com.pubnub.api.subscribe.eventengine.event.SubscriptionCursor
import java.util.concurrent.Executors
import java.util.concurrent.ScheduledExecutorService
internal data class ReceiveMessagesResult(
val messages: List,
val subscriptionCursor: SubscriptionCursor
)
internal class SubscribeEffectFactory(
private val handshakeProvider: HandshakeProvider,
private val receiveMessagesProvider: ReceiveMessagesProvider,
private val subscribeEventSink: Sink,
private val policy: RetryPolicy,
private val executorService: ScheduledExecutorService = Executors.newSingleThreadScheduledExecutor(),
private val messagesConsumer: MessagesConsumer,
private val statusConsumer: StatusConsumer,
) : EffectFactory {
override fun create(effectInvocation: SubscribeEffectInvocation): Effect? {
return when (effectInvocation) {
is SubscribeEffectInvocation.EmitMessages -> {
EmitMessagesEffect(messagesConsumer, effectInvocation.messages)
}
is SubscribeEffectInvocation.EmitStatus -> {
EmitStatusEffect(statusConsumer, effectInvocation.status)
}
is SubscribeEffectInvocation.Handshake -> {
val handshakeRemoteAction =
handshakeProvider.getHandshakeRemoteAction(
effectInvocation.channels,
effectInvocation.channelGroups
)
HandshakeEffect(handshakeRemoteAction, subscribeEventSink)
}
is SubscribeEffectInvocation.HandshakeReconnect -> {
val handshakeRemoteAction =
handshakeProvider.getHandshakeRemoteAction(
effectInvocation.channels,
effectInvocation.channelGroups
)
HandshakeReconnectEffect(
handshakeRemoteAction,
subscribeEventSink,
policy,
executorService,
effectInvocation
)
}
is SubscribeEffectInvocation.ReceiveMessages -> {
val receiveMessagesRemoteAction: RemoteAction =
receiveMessagesProvider.getReceiveMessagesRemoteAction(
effectInvocation.channels,
effectInvocation.channelGroups,
effectInvocation.subscriptionCursor
)
ReceiveMessagesEffect(receiveMessagesRemoteAction, subscribeEventSink)
}
is SubscribeEffectInvocation.ReceiveReconnect -> {
val receiveMessagesRemoteAction = receiveMessagesProvider.getReceiveMessagesRemoteAction(
effectInvocation.channels,
effectInvocation.channelGroups,
effectInvocation.subscriptionCursor
)
ReceiveReconnectEffect(
receiveMessagesRemoteAction,
subscribeEventSink,
policy,
executorService,
effectInvocation.attempts,
effectInvocation.reason
)
}
SubscribeEffectInvocation.CancelHandshake,
SubscribeEffectInvocation.CancelHandshakeReconnect,
SubscribeEffectInvocation.CancelReceiveMessages,
SubscribeEffectInvocation.CancelReceiveReconnect,
-> null
}
}
}