https://kotlinlang.org logo
#flow
Title
# flow
m

Mohamed Ibrahim

04/27/2022, 9:19 PM
How to convert pass Observable emissions to
MutableSharedFlow<T>
?
w

wasyl

04/27/2022, 9:26 PM
What would you expect such flow emits? Whatever the upstream flow emits + whatever is pushed to the
MutableSharedFlow
?
j

julian

04/28/2022, 4:04 AM
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

Mohamed Ibrahim

04/28/2022, 11:36 AM
I did that, but it doesn't work
I've updated my question in SO post
👍 1
j

julian

04/28/2022, 6:16 PM
@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