Hey folks. I need to consume RxJS observables from...
# javascript
j
Hey folks. I need to consume RxJS observables from KMP and wondering if anyone out there can offer advice or is aware of any samples, etc. I'm thinking about wrapping observerables in flows so they can be used higher in the stack in a standard fashion. I'm new to KMP and flows though, so don't know if there are any gotchas or if there's a better general approach.
c
Something like this should get you started, but note that I can't test it at the moment.
Copy code
fun Observable<T>.toFlow(): Flow<T> {
    val observable = this
    lateinit var subscription: Subscription

    return flow {
        subscription = observable.subscribe {
            emit(it)
        }
    }.onCompletion {
        subscription.unsubscribe()
    }
}
There is probably a nicer way to write this, but it should at least give you a starting point
If anyone who knows coroutines more than I do wants to attempt this, here's what you need to know: •
Observable.subscribe
takes a callback that is executed for each new element, starts immediately to process elements, and returns a
Subscription
Subscription.unsubscribe
kills a subscription that is started
j
Thanks @CLOVIS that looks like a good start. I'll give it a try!
t
For
EventInstance
we solve it via `Channel`, bacause
emit
requires suspend context.
j
@turansky thank you. i was actually starting to look at channels.
PS that repo looks like a treasure trove thanks for sharing that link!
kodee happy 1