Created
April 5, 2021 10:23
-
-
Save Starmel/41d9bf9a1f7e2d3c2993997a5888ac9f to your computer and use it in GitHub Desktop.
This file contains hidden or 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 <Element> AsyncResult.Companion.fromCoroutine( | |
source: suspend () -> Element | |
): AsyncResult<Element> { | |
return create { emitter -> | |
val job = CoroutineScope(defaultDispatcher).launch { | |
try { | |
val result = source.invoke() | |
emitter.onSuccess(result.makeShared()) | |
} catch (error: Throwable) { | |
emitter.onError(error.makeShared()) | |
} | |
} | |
emitter.disposable = { | |
job.cancel() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment