Skip to content

Commit c752d64

Browse files
Shared overflow doc (#2560)
* Clarify docs about the restriction on MutableSharedFlow function's onBufferOverflow parameter. * Clarify docs about MutableSharedFlow function without subscribers but with onBufferOverflow Co-authored-by: Zach Klippenstein <[email protected]>
1 parent 998f67b commit c752d64

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

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

+13-3
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ import kotlin.native.concurrent.*
6868
* the `onBufferOverflow` parameter, which is equal to one of the entries of the [BufferOverflow] enum. When a strategy other
6969
* than [SUSPENDED][BufferOverflow.SUSPEND] is configured, emissions to the shared flow never suspend.
7070
*
71+
* **Buffer overflow condition can happen only when there is at least one subscriber that is not ready to accept
72+
* the new value.** In the absence of subscribers only the most recent `replay` values are stored and the buffer
73+
* overflow behavior is never triggered and has no effect. In particular, in the absence of subscribers emitter never
74+
* suspends despite [BufferOverflow.SUSPEND] option and [BufferOverflow.DROP_LATEST] option does not have effect either.
75+
* Essentially, the behavior in the absence of subscribers is always similar to [BufferOverflow.DROP_OLDEST],
76+
* but the buffer is just of `replay` size (without any `extraBufferCapacity`).
77+
*
7178
* ### Unbuffered shared flow
7279
*
7380
* A default implementation of a shared flow that is created with `MutableSharedFlow()` constructor function
@@ -221,9 +228,12 @@ public interface MutableSharedFlow<T> : SharedFlow<T>, FlowCollector<T> {
221228
* @param replay the number of values replayed to new subscribers (cannot be negative, defaults to zero).
222229
* @param extraBufferCapacity the number of values buffered in addition to `replay`.
223230
* [emit][MutableSharedFlow.emit] does not suspend while there is a buffer space remaining (optional, cannot be negative, defaults to zero).
224-
* @param onBufferOverflow configures an action on buffer overflow (optional, defaults to
225-
* [suspending][BufferOverflow.SUSPEND] attempts to [emit][MutableSharedFlow.emit] a value,
226-
* supported only when `replay > 0` or `extraBufferCapacity > 0`).
231+
* @param onBufferOverflow configures an [emit][MutableSharedFlow.emit] action on buffer overflow. Optional, defaults to
232+
* [suspending][BufferOverflow.SUSPEND] attempts to emit a value.
233+
* Values other than [BufferOverflow.SUSPEND] are supported only when `replay > 0` or `extraBufferCapacity > 0`.
234+
* **Buffer overflow can happen only when there is at least one subscriber that is not ready to accept
235+
* the new value.** In the absence of subscribers only the most recent [replay] values are stored and
236+
* the buffer overflow behavior is never triggered and has no effect.
227237
*/
228238
@Suppress("FunctionName", "UNCHECKED_CAST")
229239
public fun <T> MutableSharedFlow(

0 commit comments

Comments
 (0)