I have Coroutines code which is using a callbackFlow
like this:
fun getUniqueEventAsFlow(receiverId: String): Flow<Any> = callbackFlow {
RxEventBus().register(
receiverId,
FirstUniqueEvent::class.java,
false
) { amEvent ->
offer(amEvent)
}
// Suspend until either onCompleted or external cancellation are invoked
awaitClose {
unsubscribeFromEventBus(receiverId)
cancel()
}
}.flowOn(Dispatchers.Default)
.catch { throwable ->
reportError(throwable as Exception)
}
What I'd like to do is wrap the following so that it can be called automatically, since I have many similar functions in the code:
// Suspend until either onCompleted or external cancellation are invoked
awaitClose {
unsubscribeFromEventBus(receiverId)
cancel()
}
}.flowOn(Dispatchers.Default)
.catch { throwable ->
reportError(throwable as Exception)
}
I would like to wrap the awaitClose & flowOn once, and not have to write it for every callbackFlow. Do you know which Kotlin higher order construct I can use to achieve this?
Thank you, Igor