https://kotlinlang.org logo
#rx
Title
# rx
a

Andy Gibel

03/26/2021, 6:34 PM
Given two streams A and B, I want to merge them such that the merge emits items from A only until B emits. At that point it emits B only. How can I do this? Thanks!
e

ephemient

03/26/2021, 6:51 PM
could be something like this:
Copy code
var emittedB = false
merge(a.takeWhile { !emittedB }, b.onEach { emittedB = true })
a

Andy Gibel

03/26/2021, 6:51 PM
so, takeWhile has a stream overload - playing around with that
u

uli

03/27/2021, 4:31 PM
Do not use external states and side effects
e

ephemient

03/27/2021, 4:36 PM
it's fine if it's encapsulated. if the resulting flow may be subscribed to multiple times,
Copy code
flow {
    var emittedB = false
    emitAll(merge(a.takeWhile { !emittedB }, b.onEach { emittedB = true }))
}
would do
4 Views