All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.com.pubnub.api.v2.subscriptions.SubscriptionOptions.kt Maven / Gradle / Ivy

Go to download

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!

The newest version!
package com.pubnub.api.v2.subscriptions

import com.pubnub.api.models.consumer.pubsub.PNEvent
import kotlin.jvm.JvmStatic

/**
 * SubscriptionOptions is a mechanism used for supplying optional modifiers for subscriptions.
 *
 * The options currently available in the PubNub client are:
 * * [SubscriptionOptions.filter]
 * * [SubscriptionOptions.receivePresenceEvents]
 */
open class SubscriptionOptions internal constructor(
    optionsSet: Set = emptySet(),
) {
    open val allOptions = optionsSet.toSet()
        get() =
            field.ifEmpty {
                setOf(this)
            }

    /**
     * Combine multiple options, for example:
     *
     * val options = `SubscriptionOptions.filter { /* some expression*/ } + SubscriptionOptions.receivePresenceEvents()`
     */
    open operator fun plus(options: SubscriptionOptions): SubscriptionOptions {
        val newOptions =
            buildSet {
                addAll(allOptions)
                addAll(options.allOptions)
            }
        return SubscriptionOptions(newOptions)
    }

    companion object {
        /**
         * Enable receiving presence events for a given subscriptions to a channel or channel group.
         */
        @JvmStatic
        fun receivePresenceEvents(): SubscriptionOptions = ReceivePresenceEventsImpl

        /**
         * Create a filter for messages delivered to [com.pubnub.api.v2.callbacks.BaseEventListener].
         * Please see [com.pubnub.api.v2.callbacks.BaseEventListener] for available events.
         */
        @JvmStatic
        fun filter(predicate: (PNEvent) -> Boolean): SubscriptionOptions = FilterImpl(predicate)
    }
}

/**
 * A no-op options object. It doesn't modify subscription behavior in any way.
 */
object EmptyOptions : SubscriptionOptions() {
    override val allOptions = emptySet()
}

class FilterImpl internal constructor(val predicate: (PNEvent) -> Boolean) : SubscriptionOptions()

object ReceivePresenceEventsImpl : SubscriptionOptions()




© 2015 - 2024 Weber Informatics LLC | Privacy Policy