Last active
December 19, 2018 14:30
-
-
Save markov/36f197a8cab2072750e99d976104670a to your computer and use it in GitHub Desktop.
Simpler Coroutines Scopes
This file contains hidden or 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
import kotlinx.coroutines.CoroutineScope | |
import kotlinx.coroutines.Job | |
interface CoroutineScopeAndJob : CoroutineScope { | |
val job: Job | |
} |
This file contains hidden or 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
class ExampleAfter : CoroutineScopeAndJob by JobBasedScope(Dispatchers.IO) |
This file contains hidden or 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
class ExampleBefore : CoroutineScope { | |
private val job = Job() | |
override val coroutineContext: CoroutineContext get() = job + Dispatchers.IO | |
} |
This file contains hidden or 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
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