whileSubscribedPlugin

inline fun <S : MVIState, I : MVIIntent, A : MVIAction> whileSubscribedPlugin(name: String? = null, minSubscriptions: Int = 1, stopDelay: Duration = 1.seconds, crossinline block: suspend PipelineContext<S, I, A>.() -> Unit): StorePlugin<S, I, A>

Create a new plugin that invokes block each time the subscriber count reaches minSubscriptions. Nothing is invoked when more subscribers than minSubscriptions appear, however, the block will be invoked again if the subscriber count drops below minSubscriptions and then reaches the new value again. The block will be canceled when the subscription count drops below minSubscriptions.

You are expected to suspend inside block as it's invoked asynchronously, because jobs launched inside block will be launched in the PipelineContext of the store, not the subscription scope. If you want to launch jobs in the scope of the block, use kotlinx.coroutines.coroutineScope.

There is no guarantee that this will be invoked when a new subscriber appears It may be so that a second subscriber appears before the first one disappears (due to the parallel nature of coroutines). In that case, the block will continue instead of being canceled and relaunched.

The stopDelay governs how long the block will stay active after the subscriber count drops below minSubscriptions This is useful for platforms like Android, where a configuration change may briefly cause unsubscription. By default, a small delay is introduced.

See also