SharedFlowCache

A cache of SharedFlows keyed by K, sharing a single upstream collection per key across that key's subscribers. Create one with sharedFlowCache, or completingSharedFlowCache for completion/error propagation.

A key is created on first get and evicted once its upstream ends - it completes or errors, or (under a stopping started such as SharingStarted.WhileSubscribed) its subscribers have all gone - so the cache cannot grow without bound. Eviction tears down the key's sharing coroutine.

This exposes a plain SharedFlow, so subscribers see values only: the upstream's completion is not delivered to them, and neither is an unhandled upstream error - it fails the key's own sharing coroutine in isolation (the entry is evicted; the cache scope and other keys are unaffected) and reaches the scope's CoroutineExceptionHandler. Handle errors in the supplier (e.g. retry or catch). For completion/error propagation to subscribers, use completingSharedFlowCache.

get resolves the entry lazily, so subscribe to the returned flow promptly: a flow retained across its key's eviction will not restart the upstream.

Functions

Link copied to clipboard
operator fun get(key: K, supplier: (K) -> Flow<V>): SharedFlow<V>

The shared flow for key, creating it from supplier on first access.