Skip to content

Commit 7c8577c

Browse files
qwwdfsaddkhalanskyjb
authored andcommitted
Improve Flow documentation (Kotlin#3127)
Co-authored-by: dkhalanskyjb <[email protected]>
1 parent 9115544 commit 7c8577c

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ import kotlin.coroutines.*
108108
* val myFlow = flow {
109109
* // GlobalScope.launch { // is prohibited
110110
* // launch(Dispatchers.IO) { // is prohibited
111-
* // withContext(CoroutineName("myFlow")) // is prohibited
111+
* // withContext(CoroutineName("myFlow")) { // is prohibited
112112
* emit(1) // OK
113113
* coroutineScope {
114114
* emit(2) // OK -- still the same coroutine
@@ -191,6 +191,9 @@ public interface Flow<out T> {
191191
*
192192
* To ensure the context preservation property, it is not recommended implementing this method directly.
193193
* Instead, [AbstractFlow] can be used as the base type to properly ensure flow's properties.
194+
*
195+
* All default flow implementations ensure context preservation and exception transparency properties on a best-effort basis
196+
* and throw [IllegalStateException] if a violation was detected.
194197
*/
195198
public suspend fun collect(collector: FlowCollector<T>)
196199
}

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ import kotlin.native.concurrent.*
115115
* ### Implementation notes
116116
*
117117
* Shared flow implementation uses a lock to ensure thread-safety, but suspending collector and emitter coroutines are
118-
* resumed outside of this lock to avoid dead-locks when using unconfined coroutines. Adding new subscribers
118+
* resumed outside of this lock to avoid deadlocks when using unconfined coroutines. Adding new subscribers
119119
* has `O(1)` amortized cost, but emitting has `O(N)` cost, where `N` is the number of subscribers.
120120
*
121121
* ### Not stable for inheritance
@@ -132,13 +132,13 @@ public interface SharedFlow<out T> : Flow<T> {
132132

133133
/**
134134
* Accepts the given [collector] and [emits][FlowCollector.emit] values into it.
135-
* This method should never be used directly. To emit values from a shared flow into a specific collector, either `collector.emitAll(flow)` or `collect { ... }` extension
136-
* should be used.
135+
* To emit values from a shared flow into a specific collector, either `collector.emitAll(flow)` or `collect { ... }`
136+
* SAM-conversion can be used.
137137
*
138138
* **A shared flow never completes**. A call to [Flow.collect] or any other terminal operator
139139
* on a shared flow never completes normally.
140140
*
141-
* @see [Flow.collect]
141+
* @see [Flow.collect] for implementation and inheritance details.
142142
*/
143143
override suspend fun collect(collector: FlowCollector<T>): Nothing
144144
}

0 commit comments

Comments
 (0)