Channel

The class Channel represents an individual named channel within the data pipeline. It is used to track subscribers and to publish messages when there are subscribers present. It exists as a separate object to avoid channel lookups at publish time, enabling very fast publish speeds and allowing for heavy use while incurring very minimal cost. Channels are created with {@link channel}, constructing a channel directly with new Channel(name) is not supported.

Since

v15.1.0, v14.17.0

Constructors

Link copied to clipboard
constructor()

Properties

Link copied to clipboard

Check if there are active subscribers to this channel. This is helpful if the message you want to send might be expensive to prepare.

Link copied to clipboard
val name: Any

Functions

Link copied to clipboard
fun bindStore(store: AsyncLocalStorage<StoreType>, transform: (context: ContextType) -> StoreType = definedExternally)

When channel.runStores(context, ...) is called, the given context data will be applied to any store bound to the channel. If the store has already been bound the previous transform function will be replaced with the new one. The transform function may be omitted to set the given context data as the context directly.

Link copied to clipboard
fun publish(message: Any?)

Publish a message to any subscribers to the channel. This will trigger message handlers synchronously so they will execute within the same context.

Link copied to clipboard
fun runStores()

Applies the given data to any AsyncLocalStorage instances bound to the channel for the duration of the given function, then publishes to the channel within the scope of that data is applied to the stores.

Link copied to clipboard
fun subscribe(onMessage: ChannelListener)

Register a message handler to subscribe to this channel. This message handler will be run synchronously whenever a message is published to the channel. Any errors thrown in the message handler will trigger an 'uncaughtException'.

Link copied to clipboard
fun unbindStore(store: Any?)

Remove a message handler previously registered to this channel with channel.bindStore(store).

Link copied to clipboard

Remove a message handler previously registered to this channel with channel.subscribe(onMessage).