Skip to content

Improve Flow documentation #3127

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 2 commits into from
Jan 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion kotlinx-coroutines-core/common/src/flow/Flow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ import kotlin.coroutines.*
* val myFlow = flow {
* // GlobalScope.launch { // is prohibited
* // launch(Dispatchers.IO) { // is prohibited
* // withContext(CoroutineName("myFlow")) // is prohibited
* // withContext(CoroutineName("myFlow")) { // is prohibited
* emit(1) // OK
* coroutineScope {
* emit(2) // OK -- still the same coroutine
Expand Down Expand Up @@ -191,6 +191,9 @@ public interface Flow<out T> {
*
* To ensure the context preservation property, it is not recommended implementing this method directly.
* Instead, [AbstractFlow] can be used as the base type to properly ensure flow's properties.
*
* All default flow implementations ensure context preservation and exception transparency properties on a best-effort basis
* and throw [IllegalStateException] if a violation was detected.
*/
public suspend fun collect(collector: FlowCollector<T>)
}
Expand Down
8 changes: 4 additions & 4 deletions kotlinx-coroutines-core/common/src/flow/SharedFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ import kotlin.native.concurrent.*
* ### Implementation notes
*
* Shared flow implementation uses a lock to ensure thread-safety, but suspending collector and emitter coroutines are
* resumed outside of this lock to avoid dead-locks when using unconfined coroutines. Adding new subscribers
* resumed outside of this lock to avoid deadlocks when using unconfined coroutines. Adding new subscribers
* has `O(1)` amortized cost, but emitting has `O(N)` cost, where `N` is the number of subscribers.
*
* ### Not stable for inheritance
Expand All @@ -132,13 +132,13 @@ public interface SharedFlow<out T> : Flow<T> {

/**
* Accepts the given [collector] and [emits][FlowCollector.emit] values into it.
* 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
* should be used.
* To emit values from a shared flow into a specific collector, either `collector.emitAll(flow)` or `collect { ... }`
* SAM-conversion can be used.
*
* **A shared flow never completes**. A call to [Flow.collect] or any other terminal operator
* on a shared flow never completes normally.
*
* @see [Flow.collect]
* @see [Flow.collect] for implementation and inheritance details.
*/
override suspend fun collect(collector: FlowCollector<T>): Nothing
}
Expand Down