mboudraa
12/06/2018, 2:59 PMstartWith
extension on ReceiveChannel
and i was wondering which is the best way to do that.
I have currently 2 implementations:
suspend fun <A> ReceiveChannel<A>.startWith(element: A): ReceiveChannel<A> {
val channel = Channel<A>()
channel.offer(element)
consumeEach { channel.send(it) }
return channel
}
suspend fun <A> ReceiveChannel<A>.startWith(element: A, coroutineScope: CoroutineScope): ReceiveChannel<A> {
return coroutineScope.produce {
offer(element)
this@startWith.consumeEach { this@produce.send(it) }
}
}
I’m not sure what’s the difference is between the 2 and which is the best approachstreetsofboston
12/06/2018, 3:07 PMCoroutineScope
parameter.
Sidenote: You call offer
, but that could fail. Wouldn’t that mean the returning channel won’t start with element
?mboudraa
12/06/2018, 3:09 PMstreetsofboston
12/06/2018, 3:10 PMchannel
is brand ‘new’ 🙂marstran
12/06/2018, 3:53 PMmboudraa
12/06/2018, 4:02 PMmboudraa
12/06/2018, 4:02 PMsend
or using GlobalScope
to avoid forwarding the CoroutineScope
everytimedave08
12/07/2018, 3:42 AM