simon.vergauwen
01/10/2019, 11:36 AMUnconfined
& EmptyCoroutineContext
?Vsevolod Tolstopyatov [JB]
01/10/2019, 3:35 PMUnconfined
is explained in KDoc and EmptyCoroutineContext
is self-describingsimon.vergauwen
01/10/2019, 5:00 PMtypealias CancelToken = suspend () -> Unit
. i.e. to unregister callbacks.CancelToken
might blocks others which would cause leaks.typealias CancelToken = suspend () -> Unit
fun main(args: Array<String>) {
val stack = listOf<CancelToken>(
{ delay(10000000000) }, //blocks forever
{
delay(100)
println("I cancelled first token: ${Thread.currentThread().name}")
},
{
delay(200)
println("I cancelled second token: ${Thread.currentThread().name}")
}
)
stack.forEach { token ->
token.startCoroutine(RunAndForgetContinuation())
}
}
class RunAndForgetContinuation<A> : Continuation<A> {
override val context: CoroutineContext
get() = EmptyCoroutineContext
override fun resumeWith(result: Result<A>) = Unit
}
EmptyCoroutineContext
correctly then I can use it to launch a coroutine from any context I am currently in.Allan Wang
01/10/2019, 9:39 PMEmptyCoroutineContext
to an existing context will result in the same context.simon.vergauwen
01/10/2019, 9:46 PMfun (suspend () -> Unit).startCoroutine
with EmptyCoroutineContext
it forks with the context that is inherited?Allan Wang
01/10/2019, 9:48 PMplus
operator you’ll see the behaviourstartCoroutine
will work. To run your block you’d need a scope to begin with, which is attached to a contextsimon.vergauwen
01/10/2019, 9:53 PMstartCoroutine
allows me to run a suspend
function with a continuation that I run on a context. In this case EmptyCoroutineContext
. https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.coroutines.experimental/start-coroutine.htmlAllan Wang
01/10/2019, 9:57 PMsimon.vergauwen
01/10/2019, 10:00 PM