Tran Thang
11/25/2021, 6:41 AMval coroutineContext: CoroutineContext get() = Job() + Dispatchers.Main
wbertan
11/25/2021, 8:58 AM/**
* Returns a context containing elements from this context and elements from other [context].
* The elements from this context with the same key as in the other one are dropped.
*/
public operator fun plus(context: CoroutineContext): CoroutineContext =
if (context === EmptyCoroutineContext) this else // fast path -- avoid lambda creation
context.fold(this) { acc, element ->
val removed = acc.minusKey(element.key)
if (removed === EmptyCoroutineContext) element else {
// make sure interceptor is always last in the context (and thus is fast to get when present)
val interceptor = removed[ContinuationInterceptor]
if (interceptor == null) CombinedContext(removed, element) else {
val left = removed.minusKey(ContinuationInterceptor)
if (left === EmptyCoroutineContext) CombinedContext(element, interceptor) else
CombinedContext(CombinedContext(left, element), interceptor)
}
}
}
Sorry don't know much to explain WHAT that is doing per say, if that is what you are looking for 😬Zach Klippenstein (he/him) [MOD]
11/25/2021, 8:21 PMJob
every time the property is read, which breaks structured concurrency in multiple ways and makes it very easy to write code that looks idiomatic and correct but is full of bugs.
That context should probably be created once and then held onto, and the job should probably have a parent or at least be explicitly cancelled at some point.