Skip to content

Instantly share code, notes, and snippets.

View RBusarow's full-sized avatar
🕶️

Rick Busarow RBusarow

🕶️
View GitHub Profile
@RBusarow
RBusarow / CachedFlow.kt
Last active December 8, 2019 05:59
A non-blocking, non-suspending, "cold" implementation of cache/replay functionality for coroutines Flow.
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:
* ```
@RBusarow
RBusarow / BroadcastFlow.kt
Last active January 7, 2020 19:33
A hot flow which will auto-start upon its first observer, broadcast to multiple observers, auto-close when there are no observers, and restart when observed again.
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
@RBusarow
RBusarow / CoroutineContext.kt
Created July 22, 2019 17:10
Conceptualization of CoroutineContext as a data class
data class CoroutineContext(
val job: Job? = null,
val interceptor: ContinuationInterceptor? = null,
val exceptionHandler: CoroutineExceptionHandler? = null,
val coroutineName: CoroutineName? = null
)
@RBusarow
RBusarow / SomeClass.kt
Created July 22, 2019 16:47
Example of layering CoroutineContext
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
@RBusarow
RBusarow / CoroutineContextElementExtractor.kt
Last active July 22, 2019 13:27
extracting Elements from a CoroutineContext
fun lookForCoroutineContextElements(context: CoroutineContext) {
val job: CoroutineContext? = context[Job]
val interceptor: CoroutineContext? = context[ContinuationInterceptor]
val exceptionHandler: CoroutineContext? = context[CoroutineExceptionHandler]
val name: CoroutineContext? = context[CoroutineName]
}
@RBusarow
RBusarow / CoroutineContextElementExtractor.kt
Last active July 22, 2019 13:27
extracting Elements from a CoroutineContext
fun lookForCoroutineContextElements(context: CoroutineContext) {
val job: Job? = context[Job]
val interceptor: ContinuationInterceptor? = context[ContinuationInterceptor]
val exceptionHandler: CoroutineExceptionHandler? = context[CoroutineExceptionHandler]
val name: CoroutineName? = context[CoroutineName]
}
@RBusarow
RBusarow / CoroutineScope.kt
Created July 13, 2019 22:48
Medium-on-a-phone friendly definition of CoroutineScope
interface CoroutineScope {
val coroutineContext: CoroutineContext
}
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
class SomeCoroutineScopeClass : CoroutineScope {
override val coroutineContext = Job() + Dispatchers.Main
}
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
}