-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify FlowScope implementation, stylistic improvements
- Loading branch information
Showing
7 changed files
with
78 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
kotlinx-coroutines-core/common/src/flow/internal/FlowCoroutine.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.coroutines.flow.internal | ||
|
||
import kotlinx.coroutines.* | ||
import kotlinx.coroutines.flow.* | ||
import kotlinx.coroutines.internal.* | ||
import kotlinx.coroutines.intrinsics.* | ||
import kotlin.coroutines.* | ||
import kotlin.coroutines.intrinsics.* | ||
import kotlinx.coroutines.flow.unsafeFlow as flow | ||
|
||
/** | ||
* Creates a [CoroutineScope] and calls the specified suspend block with this scope. | ||
* This builder is similar to [coroutineScope] with the only exception that it *ties* lifecycle of children | ||
* and itself regarding the cancellation, thus being cancelled when one of the children becomes cancelled. | ||
* | ||
* For example: | ||
* ``` | ||
* flowScope { | ||
* launch { | ||
* throw CancellationException() | ||
* } | ||
* } // <- CE will be rethrown here | ||
* ``` | ||
*/ | ||
internal suspend fun <R> flowScope(@BuilderInference block: suspend CoroutineScope.() -> R): R = | ||
suspendCoroutineUninterceptedOrReturn { uCont -> | ||
val coroutine = FlowCoroutine(uCont.context, uCont) | ||
coroutine.startUndispatchedOrReturn(coroutine, block) | ||
} | ||
|
||
/** | ||
* Creates a flow that also provides a [CoroutineScope] for each collector | ||
* Shorthand for: | ||
* ``` | ||
* flow { | ||
* flowScope { | ||
* ... | ||
* } | ||
* } | ||
* ``` | ||
* with additional constraint on cancellation. | ||
* To cancel child without cancelling itself, `cancel(ChildCancelledException())` should be used. | ||
*/ | ||
internal fun <R> scopedFlow(@BuilderInference block: suspend CoroutineScope.(FlowCollector<R>) -> Unit): Flow<R> = | ||
flow { | ||
val collector = this | ||
flowScope { block(collector) } | ||
} | ||
|
||
internal class FlowCoroutine<T>(context: CoroutineContext, uCont: Continuation<T>) : | ||
ScopeCoroutine<T>(context, uCont) { | ||
|
||
public override fun childCancelled(cause: Throwable): Boolean { | ||
if (cause is ChildCancelledException) return true | ||
return cancelImpl(cause) | ||
} | ||
} |
58 changes: 0 additions & 58 deletions
58
kotlinx-coroutines-core/common/src/flow/internal/FlowScope.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters