Skip to content

Convert Disposable to Kotlin Flow? #1949

Answered by zsmb13
SeanOThomas asked this question in Q&A
Discussion options

You must be logged in to vote

We don't have API for this yet, but we'll look into providing it.

In the meantime, you should be able to use the callbackFlow function from kotlinx.coroutines to convert any callback based API into a Flow. For our event API, that would look something like this (haven't tested it, just a basic draft):

val flow = callbackFlow<ChatEvent> {
    val disposable = client.subscribeFor(ChatEventListener { event ->
        this.trySendBlocking(event)
    })
    awaitClose {
        disposable.dispose()
    }
}

// In a coroutine
flow.collect { event: ChatEvent -> 
    // ...
}

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by SeanOThomas
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants