Skip to content

Instantly share code, notes, and snippets.

@markov
Last active December 19, 2018 14:30
Show Gist options
  • Save markov/36f197a8cab2072750e99d976104670a to your computer and use it in GitHub Desktop.
Save markov/36f197a8cab2072750e99d976104670a to your computer and use it in GitHub Desktop.
Simpler Coroutines Scopes
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
interface CoroutineScopeAndJob : CoroutineScope {
val job: Job
}
class ExampleAfter : CoroutineScopeAndJob by JobBasedScope(Dispatchers.IO)
class ExampleBefore : CoroutineScope {
private val job = Job()
override val coroutineContext: CoroutineContext get() = job + Dispatchers.IO
}
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Job
import kotlin.coroutines.CoroutineContext
class JobBasedScope(val dispatcher: CoroutineDispatcher) : CoroutineScopeAndJob {
override val job = Job()
override val coroutineContext: CoroutineContext get() = job + dispatcher
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment