Skip to content

Instantly share code, notes, and snippets.

@blaZ3
Created June 14, 2021 13:45
Show Gist options
  • Save blaZ3/8606e22e6a12fb85a0231cf8e74e047d to your computer and use it in GitHub Desktop.
Save blaZ3/8606e22e6a12fb85a0231cf8e74e047d to your computer and use it in GitHub Desktop.
ManagedCoroutineScope
import androidx.lifecycle.LifecycleCoroutineScope
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlin.coroutines.CoroutineContext
internal interface ManagedCoroutineScope : CoroutineScope {
fun launch(block: suspend CoroutineScope.() -> Unit): Job
}
internal class LifecycleManagedCoroutineScope(
private val lifecycleCoroutineScope: LifecycleCoroutineScope
) : ManagedCoroutineScope {
override val coroutineContext: CoroutineContext
get() = lifecycleCoroutineScope.coroutineContext
override fun launch(block: suspend CoroutineScope.() -> Unit): Job =
lifecycleCoroutineScope.launchWhenCreated(block)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment