Skip to content

Commit b5083f5

Browse files
elizarovdkhalanskyjb
authored andcommitted
docs: Clarify MutableSharedFlow.emit/tryEmit on subscribers and overflow (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 <[email protected]>
1 parent 271894d commit b5083f5

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

kotlinx-coroutines-core/common/src/flow/SharedFlow.kt

+18-7
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,15 @@ public interface SharedFlow<out T> : Flow<T> {
167167
*/
168168
public interface MutableSharedFlow<T> : SharedFlow<T>, FlowCollector<T> {
169169
/**
170-
* Emits a [value] to this shared flow, suspending on buffer overflow if the shared flow was created
171-
* with the default [BufferOverflow.SUSPEND] strategy.
170+
* Emits a [value] to this shared flow, suspending on buffer overflow.
171+
*
172+
* This call can suspend only when the [BufferOverflow] strategy is
173+
* [SUSPEND][BufferOverflow.SUSPEND] **and** there are subscribers collecting this shared flow.
174+
*
175+
* If there are no subscribers, the buffer is not used.
176+
* Instead, the most recently emitted value is simply stored into
177+
* the replay cache if one was configured, displacing the older elements there,
178+
* or dropped if no replay cache was configured.
172179
*
173180
* See [tryEmit] for a non-suspending variant of this function.
174181
*
@@ -179,12 +186,16 @@ public interface MutableSharedFlow<T> : SharedFlow<T>, FlowCollector<T> {
179186

180187
/**
181188
* Tries to emit a [value] to this shared flow without suspending. It returns `true` if the value was
182-
* emitted successfully. When this function returns `false`, it means that the call to a plain [emit]
183-
* function will suspend until there is a buffer space available.
189+
* emitted successfully (see below). When this function returns `false`, it means that a call to a plain [emit]
190+
* function would suspend until there is buffer space available.
191+
*
192+
* This call can return `false` only when the [BufferOverflow] strategy is
193+
* [SUSPEND][BufferOverflow.SUSPEND] **and** there are subscribers collecting this shared flow.
184194
*
185-
* A shared flow configured with a [BufferOverflow] strategy other than [SUSPEND][BufferOverflow.SUSPEND]
186-
* (either [DROP_OLDEST][BufferOverflow.DROP_OLDEST] or [DROP_LATEST][BufferOverflow.DROP_LATEST]) never
187-
* suspends on [emit], and thus `tryEmit` to such a shared flow always returns `true`.
195+
* If there are no subscribers, the buffer is not used.
196+
* Instead, the most recently emitted value is simply stored into
197+
* the replay cache if one was configured, displacing the older elements there,
198+
* or dropped if no replay cache was configured. In any case, `tryEmit` returns `true`.
188199
*
189200
* This method is **thread-safe** and can be safely invoked from concurrent coroutines without
190201
* external synchronization.

0 commit comments

Comments
 (0)