I am writing a custom flow and inside it I want to non-blocking collect some other flow, process it's emissions and emit them:
val externalFlow = flowOf(1,3,4)
val myFlow = flow<Int> {
coroutineScope { externalFlow.collect { this@flow.emit(it + 8) } }
emit(1)
emit(99)
}
I've used
coroutineScope
, but I'm worried that it won't be cancelled when
myFlow
collection is cancelled or interrupted.
It's just that I've found
internal flowScope
builder in
coroutines.core
and it says:
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.
Can I achieve similar effect for my case? Or using
coroutineScope
like I did is ok?