Skip to content

Instantly share code, notes, and snippets.

@aartikov
Created December 14, 2022 09:27
Show Gist options
  • Save aartikov/a56cc94bb306e05b7b7927353910da08 to your computer and use it in GitHub Desktop.
Save aartikov/a56cc94bb306e05b7b7927353910da08 to your computer and use it in GitHub Desktop.
Creates CoroutineScope for Decompose component
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
}
@arkivanov
Copy link

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 for DESTROYED state manually.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment