com.pubnub.api.subscribe.eventengine.effect.ReceiveMessagesEffect.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.PubNubException
import com.pubnub.api.endpoints.remoteaction.RemoteAction
import com.pubnub.api.eventengine.ManagedEffect
import com.pubnub.api.eventengine.Sink
import com.pubnub.api.subscribe.eventengine.event.SubscribeEvent
import org.slf4j.LoggerFactory
internal class ReceiveMessagesEffect(
private val receiveMessagesRemoteAction: RemoteAction,
private val subscribeEventSink: Sink,
) : ManagedEffect {
private val log = LoggerFactory.getLogger(ReceiveMessagesEffect::class.java)
override fun runEffect() {
log.trace("Running ReceiveMessagesEffect")
receiveMessagesRemoteAction.async { result, status ->
if (status.error) {
subscribeEventSink.add(
SubscribeEvent.ReceiveFailure(
status.exception
?: PubNubException("Unknown error")
)
)
} else {
subscribeEventSink.add(SubscribeEvent.ReceiveSuccess(result!!.messages, result.subscriptionCursor))
}
}
}
override fun cancel() {
receiveMessagesRemoteAction.silentCancel()
}
}