Last active
May 25, 2021 17:12
-
-
Save Nillerr/f7b52d4f17f0c8fae1f594e76d041ae8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun <T> Flow<T>.collect(onEach: (T) -> Unit, onCompletion: (cause: Throwable?) -> Unit): Cancellable { | |
val scope = CoroutineScope(SupervisorJob() + Dispatchers.Main) | |
scope.launch { | |
try { | |
collect { | |
onEach(it) | |
} | |
onCompletion(null) | |
} catch (e: Throwable) { | |
onCompletion(e) | |
} | |
} | |
return object : Cancellable { | |
override fun cancel() { | |
scope.cancel() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment