Skip to content

Commit

Permalink
docs: Clarify MutableSharedFlow.emit/tryEmit on subscribers and overf…
Browse files Browse the repository at this point in the history
…low (Kotlin#3185)

The details on how buffering conceptually works and what happens without subscribers are buried deep inside the SharedFlow docs. This clarification puts the important information right into the doc of emit and tryEmit methods.

Co-authored-by: dkhalanskyjb <52952525+dkhalanskyjb@users.noreply.github.com>
  • Loading branch information
2 people authored and dee-tree committed Jul 21, 2022
1 parent 271894d commit b5083f5
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions kotlinx-coroutines-core/common/src/flow/SharedFlow.kt
Expand Up @@ -167,8 +167,15 @@ public interface SharedFlow<out T> : Flow<T> {
*/
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.
* Emits a [value] to this shared flow, suspending on buffer overflow.
*
* This call can suspend only when the [BufferOverflow] strategy is
* [SUSPEND][BufferOverflow.SUSPEND] **and** there are subscribers collecting this shared flow.
*
* If there are no subscribers, the buffer is not used.
* Instead, the most recently emitted value is simply stored into
* the replay cache if one was configured, displacing the older elements there,
* or dropped if no replay cache was configured.
*
* See [tryEmit] for a non-suspending variant of this function.
*
Expand All @@ -179,12 +186,16 @@ public interface MutableSharedFlow<T> : SharedFlow<T>, FlowCollector<T> {

/**
* Tries to emit a [value] to this shared flow without suspending. It returns `true` if the value was
* 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.
* emitted successfully (see below). When this function returns `false`, it means that a call to a plain [emit]
* function would suspend until there is buffer space available.
*
* This call can return `false` only when the [BufferOverflow] strategy is
* [SUSPEND][BufferOverflow.SUSPEND] **and** there are subscribers collecting this shared flow.
*
* 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`.
* If there are no subscribers, the buffer is not used.
* Instead, the most recently emitted value is simply stored into
* the replay cache if one was configured, displacing the older elements there,
* or dropped if no replay cache was configured. In any case, `tryEmit` returns `true`.
*
* This method is **thread-safe** and can be safely invoked from concurrent coroutines without
* external synchronization.
Expand Down

0 comments on commit b5083f5

Please sign in to comment.