Created
December 14, 2022 09:27
-
-
Save aartikov/a56cc94bb306e05b7b7927353910da08 to your computer and use it in GitHub Desktop.
Creates CoroutineScope for Decompose component
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 ComponentContext.componentCoroutineScope(): CoroutineScope { | |
val scope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate) | |
if (lifecycle.state != Lifecycle.State.DESTROYED) { | |
lifecycle.doOnDestroy { | |
scope.cancel() | |
} | |
} else { | |
scope.cancel() | |
} | |
return scope | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Btw, since Essenty version 1.3.0-beta01,
Lifecycle#doOnDestroy {}
callback is automatically called if the lifecycle is already destroyed. So there is no need to check forDESTROYED
state manually.