Skip to content

docs: Clarify MutableSharedFlow.emit/tryEmit on subscribers and overflow #3185

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Feb 16, 2022
14 changes: 14 additions & 0 deletions kotlinx-coroutines-core/common/src/flow/SharedFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,15 @@ public interface MutableSharedFlow<T> : SharedFlow<T>, FlowCollector<T> {
* Emits a [value] to this shared flow, suspending on buffer overflow if the shared flow was created
* with the default [BufferOverflow.SUSPEND] strategy.
*
* Buffer is only used by shared flow to wait for slow subscribers.
* **When this shared flow has no subscribers `emit` call never suspends.**
* It just stores the most recently emitted value to its replay cache (if it was configured),
* dropping the older elements from the replay cache.
*
* A shared flow configured with a [BufferOverflow] strategy other than [SUSPEND][BufferOverflow.SUSPEND]
* (either [DROP_OLDEST][BufferOverflow.DROP_OLDEST] or [DROP_LATEST][BufferOverflow.DROP_LATEST]) never
* suspends on [emit], since its buffer overflow is handled without suspension.
*
* See [tryEmit] for a non-suspending variant of this function.
*
* This method is **thread-safe** and can be safely invoked from concurrent coroutines without
Expand All @@ -182,6 +191,11 @@ public interface MutableSharedFlow<T> : SharedFlow<T>, FlowCollector<T> {
* emitted successfully. When this function returns `false`, it means that the call to a plain [emit]
* function will suspend until there is a buffer space available.
*
* Buffer is only used by shared flow to wait for slow subscribers.
* **When this shared flow has no subscribers [emit] call never suspends, and thus `tryEmit` always returns `true`.**.
* It just stores the most recently emitted value to its replay cache (if it was configured),
* dropping the older elements from the replay cache.
*
* A shared flow configured with a [BufferOverflow] strategy other than [SUSPEND][BufferOverflow.SUSPEND]
* (either [DROP_OLDEST][BufferOverflow.DROP_OLDEST] or [DROP_LATEST][BufferOverflow.DROP_LATEST]) never
* suspends on [emit], and thus `tryEmit` to such a shared flow always returns `true`.
Expand Down