Internals of Kotlin flows. To grasp flows higher we have to… | by Tushar Saha | Dec, 2022


val a = a()
val y = foo(a).await() // suspension level #1
b()
val z = bar(a, y).await() // suspension level #2
c(z)
class <anonymous_for_state_machine> extends SuspendLambda<...> {
// The present state of the state machine
int label = 0

// native variables of the coroutine
A a = null
Y y = null

void resumeWith(Object outcome) {
if (label == 0) goto L0
if (label == 1) goto L1
if (label == 2) goto L2
else throw IllegalStateException()

L0:
// result's anticipated to be `null` at this invocation
a = a()
label = 1
outcome = foo(a).await(this) // 'this' is handed as a continuation
if (outcome == COROUTINE_SUSPENDED) return // return if await had suspended execution
L1:
// exterior code has resumed this coroutine passing the results of .await()
y = (Y) outcome
b()
label = 2
outcome = bar(a, y).await(this) // 'this' is handed as a continuation
if (outcome == COROUTINE_SUSPENDED) return // return if await had suspended execution
L2:
// exterior code has resumed this coroutine passing the results of .await()
Z z = (Z) outcome
c(z)
label = -1 // No extra steps are allowed
return
}
}

enjoyable CoroutineScope.launch(
context: CoroutineContext = EmptyCoroutineContext,
// …
): Job

enjoyable interface FlowCollector<T> {
droop enjoyable emit(worth: T)
}

interface Move<T> {
droop enjoyable accumulate(collector: FlowCollector<T>)
}

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles