TokenizedLiveCallbackRegistry

value class TokenizedLiveCallbackRegistry<I, O> : LiveCallbackRegistry<I, O> , Function2<CallbackToken<I, O>, I, List<O>>

An auto-releasing registry of LiveCallbackContainers that correlates callbacks and its origins.

It respects the lifecycle of other app components, such as activities, fragments, or services. This awareness ensures the registered callbacks are only invoked when appropriate.

You can register a callback paired with an object that implements the LifecycleOwner interface. This relationship allows the callback to be released when the state of the lifecycle changes to DESTROYED. This is especially useful for activities and fragments because they can safely register callbacks and not worry about leaks: the callback and lifecycle are instantly released when their lifecycles are destroyed.

Registered callbacks can be invoked when appropriate, given that a CallbackToken corresponding to the one returned by register is provided on invoke.

This is meant to be used with callbacks that receive a parameter and return a value. For other kinds of callbacks, there are more specific versions:

Parameters

I

The type of parameter the callbacks receives.

O

The type of return of the callbacks.

Constructors

TokenizedLiveCallbackRegistry
Link copied to clipboard
fun TokenizedLiveCallbackRegistry()

Creates an empty registry that relays invocations to the registered callbacks based on the provided CallbackTokens.

Functions

invoke
Link copied to clipboard
open operator override fun invoke(token: CallbackToken<I, O>, input: I): List<O>

Relays invocations to the registered callbacks whose tokens matches token.

register
Link copied to clipboard
open override fun register(lifecycle: Lifecycle, runWhileStopped: Boolean, callback: Callback<I, O>): CallbackToken<I, O>

Registers callback paired with lifecycle to be invoked later, and releases them when lifecycle is DESTROYED.