How to convert pass Observable emissions to `Mutab...
# flow
m
How to convert pass Observable emissions to
MutableSharedFlow<T>
?
w
What would you expect such flow emits? Whatever the upstream flow emits + whatever is pushed to the
MutableSharedFlow
?
j
Why do you need to convert? Based on your SO post, since you already have a
Flow
and a
MutableSharedFlow
, why not just collect the
Flow
, and set the collected values on the
MutableSharedFlow
as you collect them?
m
I did that, but it doesn't work
I've updated my question in SO post
👍 1
j
@Mohamed Ibrahim This succeeds and does what you want, I think:
Copy code
val ps = PublishSubject.create<Int>()
val mf = MutableSharedFlow<Int>()
val pf = ps.asFlow()
    .onEach {
        mf.emit(it)
    }
launch {
    pf.take(3).collect()
}
launch {
    mf.take(3).collect {
        println("$it") // Prints 1 2 3
    }
}
launch {
    yield() // Without this we suspend indefinitely
    ps.onNext(1)
    ps.onNext(2)
    ps.onNext(3)
}
loading 1
@Mohamed Ibrahim See my update on SO.
🙏 1