setListener
Registers a Listener to observe changes in toggle states stored by this CachedToggleStore.
Multiple listeners are supported concurrently. Each call adds the provided listener and returns a function that removes only that listener when invoked. To avoid memory leaks, callers should always invoke the returned unsubscribe function when the listener is no longer needed (for example, when the collector of a kotlinx.coroutines.flow.callbackFlow is closed).
Example usage:
val unsubscribe = cachedToggleStore.setListener(object : CachedToggleStore.Listener {
override fun onToggleStored(newValue: Toggle.State, oldValue: Toggle.State?) {
// React to state change
}
})
// Later, when no longer interested:
unsubscribe()Content copied to clipboard
Return
a function that removes the listener when invoked. The returned function is safe to call multiple times.
Parameters
listener
the Listener instance that will receive callbacks for each set operation. Passing null clears all registered listeners.