Comments (3)
A flow is "cold", so on its own it can't be used in the way you describe. It's only active when the consumer is collecting it, and all of its state lives inside the collect
function. Outside collect
, the flow isn't running or producing values, and there's no way to make a stateful iterator.
To turn it into a stateful "hot" stream of values, we can add an additional producer coroutine using produceIn
. The coroutine will be responsible for maintaining the flow in an active state, and will expose each successive value via a Channel
.
With produceIn
, your example code would look like this:
myCoroutineScope.launch {
val flow = myComponent.getSomeThings()
val channel = flow.produceIn(this)
for (item in channel) {
β¦
}
}
Since the producer coroutine is now responsible for the lifecycle of the flow, including any resources it might hold, it's important to use correct structured concurrency to ensure everything's cleaned up afterwards.
from kotlinx.coroutines.
Every flow operator is open-source and fairly short. You can replace it with its implementation by getting rid of a few optimizations.
originalFlow
.onStart { /* START */ }
becomes
flow {
/* START */
emitAll(originalFlow)
}
originalFlow
.onStart { /* START */ }
.onEmpty { /* EMPTY */ }
becomes
flow {
var isEmpty = true
flow {
/* START */
emitAll(originalFlow)
}.collect {
isEmpty = false
emit(it)
}
if (isEmpty) {
/* EMPTY */
}
}
With onEach
, this becomes
flow {
flow {
var isEmpty = true
flow {
/* START */
emitAll(originalFlow)
}.collect {
isEmpty = false
emit(it)
}
if (isEmpty) {
/* EMPTY */
}
}.collect {
/* EACH */
emit(it)
}
}
and so on.
from kotlinx.coroutines.
https://youtrack.jetbrains.com/issue/KT-33851/iterator-less-for-statement-operator-convention in the future also might help w.r.t. more imperative usages
Another option is to transform flow into the channel via produceIn
or reuse the facility from #3274
But in general, our consensus is that we do intend to provide one more way to consume the flow which is drastically different (semantics-wise and performance-wise) than the regular pull model.
from kotlinx.coroutines.
Related Issues (20)
- Flow collection silently hangs when dispatcher throws an exception
- Suggestion for a potential new Flow's `timeout` extension HOT 4
- Mutex is unlocked on cancellation HOT 2
- Crash on GraalVM at `1.9.0-RC` HOT 11
- Non-linearizable behavior in `cancel` + `awaitClose` inside of `produce` HOT 1
- Inherit from `kotlinx.coroutines` warning even with interface delegation ? HOT 1
- Consider stabilizing `CoroutineStart.ATOMIC` HOT 3
- 1.9.0-RC: `kotlinx-coroutines-core/jvm/src/internal/ProbesSupport.kt` calls ` kotlinx.coroutines.debug.internal.probeCoroutineResumed` HOT 1
- 1.9.0-RC: DispatchersToStringTest.testLimitedParallelism fails when CORE_POOL_SIZE == 2 HOT 3
- CancellableContinuation.invokeOnCancellation should accept a suspend callback HOT 16
- ActorCoroutine with ConflatedBufferedChannel capactiy HOT 1
- Calling CompletionStage.asDeferred() on the same future many times causes stack overflows HOT 3
- [Performance impacting] Upgrade Android Build rules to use standard android library Gradle plugin
- Flow.catch skips upstream exceptions HOT 7
- 1.9.0-RC: `kotlinx-coroutines-core/concurrent/src/...` contains common (expect) and platform (actual) files HOT 3
- TextMeasurer.measure only works in Dispatchers.Main on ios HOT 1
- stateFlow unable to receive updated values ββusing collect HOT 4
- StateFlow, different values, compareAndSet return false HOT 3
- 1.9.0-RC test task:compileDemoDebugUnitTestKotlin error Suspension functions can only be called within coroutine body. HOT 1
- Improve invokeOnCompletion and invokeOnCancellation API HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from kotlinx.coroutines.