stateInDelayed

fun <T> Flow<T>.stateInDelayed(initialValue: T, scope: CoroutineScope): StateFlow<T>(source)

Shares this flow in scope, stopping its upstream five seconds after the last subscriber leaves.

Collection starts with the first subscriber. A later subscriber receives the retained latest value and restarts the upstream if it stopped. Reading value alone does not start collection. The timeout governs this shared upstream, not separate jobs launched into scope by callbacks. Cancelling scope cancels the sharing coroutine.

Return

StateFlow retaining the latest value for new subscribers.

Parameters

initialValue

Value available before the source first emits.

scope

Scope owning the sharing coroutine, usually a ViewModel's scope.

Type Parameters

T

The emitted value type.

See also

stateIn
SharingStarted.WhileSubscribed