What's the best to wrap a callback-based listener with coroutines?

Say you have a callback with some arguments that is called many times to notify an event. How would you translate it with coroutines? Is it better a ReceiveCannel or a Flow? How to implement both without some memory leak?

Here’s my usecase. I need to use Firebase for a project that is going multiplatform and I need a data structure available in common that is capable of handling callback registrations and removal.
For example how would you handle with a Flow the authStateListener?

actual val authStateFlow: Flow<Boolean>
    get() = flow {
        supervisorScope {
            delegate.addAuthStateListener {
                launch { emit(it.currentUser != null) }

See callbackFlow. It’s for exactly this.

1 Like

:star_struck: Thank you! I was not aware of that!

Callbacks and Kotlin Flows

1 Like