/LaunchedEffectImpl.kt Secret
Created
May 1, 2023 11:59
Star
You must be signed in to star a gist
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
internal class LaunchedEffectImpl( | |
parentCoroutineContext: CoroutineContext, | |
private val task: suspend CoroutineScope.() -> Unit | |
) : RememberObserver { | |
private val scope = CoroutineScope(parentCoroutineContext) | |
private var job: Job? = null | |
override fun onRemembered() { | |
job?.cancel("Old job was still running!") | |
job = scope.launch(block = task) | |
} | |
override fun onForgotten() { | |
job?.cancel() | |
job = null | |
} | |
override fun onAbandoned() { | |
job?.cancel() | |
job = null | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment