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
import kotlinx.coroutines.flow.* | |
/** | |
* A "replay" flow which will record the last [size] collected values. | |
* | |
* When a collector begins collecting after values have already been recorded, | |
* those values will be collected *before* values from the [sourceFlow] are collected. | |
* | |
* example: | |
* ``` |
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
import kotlinx.coroutines.* | |
import kotlinx.coroutines.channels.Channel | |
import kotlinx.coroutines.channels.ProducerScope | |
import kotlinx.coroutines.flow.* | |
import kotlinx.coroutines.sync.Mutex | |
import kotlinx.coroutines.sync.withLock | |
@FlowPreview | |
@ExperimentalCoroutinesApi |
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
data class CoroutineContext( | |
val job: Job? = null, | |
val interceptor: ContinuationInterceptor? = null, | |
val exceptionHandler: CoroutineExceptionHandler? = null, | |
val coroutineName: CoroutineName? = null | |
) |
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
class SomeClass : CoroutineScope { | |
val jobA = Job() | |
override val coroutineContext: CoroutineContext | |
get() = jobA + Dispatchers.Main | |
fun doSomething() { | |
// create a new coroutine using jobA and Main dispatcher |
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
fun lookForCoroutineContextElements(context: CoroutineContext) { | |
val job: CoroutineContext? = context[Job] | |
val interceptor: CoroutineContext? = context[ContinuationInterceptor] | |
val exceptionHandler: CoroutineContext? = context[CoroutineExceptionHandler] | |
val name: CoroutineContext? = context[CoroutineName] | |
} |
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
fun lookForCoroutineContextElements(context: CoroutineContext) { | |
val job: Job? = context[Job] | |
val interceptor: ContinuationInterceptor? = context[ContinuationInterceptor] | |
val exceptionHandler: CoroutineExceptionHandler? = context[CoroutineExceptionHandler] | |
val name: CoroutineName? = context[CoroutineName] | |
} |
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
interface CoroutineScope { | |
val coroutineContext: CoroutineContext | |
} |
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
class SomeCoroutineScopeClass : CoroutineScope { | |
override val coroutineContext: CoroutineContext | |
init { | |
val receiver: CoroutineContext = Job() | |
val other: CoroutineContext = Dispatchers.Main | |
// combine two contexts to create an entirely new instance | |
val newContext: CoroutineContext = receiver + other | |
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
class SomeCoroutineScopeClass : CoroutineScope { | |
override val coroutineContext = Job() + Dispatchers.Main | |
} |
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
class CoroutineContext { | |
internal val map = mutableMapOf<Key<*>, Element>() | |
operator fun <E : Element> get(key: Key<E>): E? = map[key] as? E | |
interface Key<E : Element> | |
interface Element | |
} |