expensivebelly
03/01/2022, 7:14 PM@ObsoleteCoroutinesApi
public fun <E> CoroutineScope.broadcast(
context: CoroutineContext = EmptyCoroutineContext,
capacity: Int = 1,
start: CoroutineStart = CoroutineStart.LAZY,
onCompletion: CompletionHandler? = null,
@BuilderInference block: suspend ProducerScope<E>.() -> Unit
): BroadcastChannel<E>
to SharedFlow
?Nick Allen
03/01/2022, 10:50 PMchannelFlow {
send(fetchThis())
send(fetchThat())
}.buffer(someCapacity).shareIn(scope, SharingStarted.Lazily)
`channelFlow`/`callbackFlow` are kinda the Flow
equivalent of produce
.
buffering is handled with a separate call (before shareIn
).
shareIn
is what allows multiple collectors to share the producing coroutine.`expensivebelly
03/02/2022, 7:43 PMchannelFlow
in there?Nick Allen
03/02/2022, 7:49 PMchannelFlow
terminates when the end of the lambda is reached. However, shareIn
produces a Flow
that never terminates. Check out the docs (https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/share-in.html) on dealing with errors and completion.
Basically, if you want to forward errors/completion, you need to materialize them into items.