Hi everyone!
I am currently re-doing the kotlinx.coroutines library (or at least a really simplified version) in order to learn more about how coroutines really work and how to use them correctly. I would really like to make that librairy multiplateform and I already build the JVM part (works like a charm).
I struggle now to understand the result of the JS compiler as it adds continuations parameter to my suspending functions
Kotlin:
suspend fun delay(delay : Long): Unit = suspendCoroutine { cont ->
if(cont.context[SuspentionTimingKey] is SuspentionTiming){
if(delay>0){
(cont.context[SuspentionTimingKey] as SuspentionTiming).shouldResumeAt = getCurrentMillis()+delay;
}
}
Dispatcher.dispatch(cont,Unit);
}
suspend fun suspend(){
delay(0);
}
JS Output:
function delay$lambda(closure$delay) {
return function (cont) {
var tmp$;
if (Kotlin.isType(cont.context.get_8oh8b3$(SuspentionTimingKey_getInstance()), SuspentionTiming)) {
if (closure$delay.compareTo_11rb$(Kotlin.Long.fromInt(0)) > 0) {
(Kotlin.isType(tmp$ = cont.context.get_8oh8b3$(SuspentionTimingKey_getInstance()), SuspentionTiming) ? tmp$ : throwCCE()).shouldResumeAt = getCurrentMillis().add(closure$delay);
}
}
Dispatcher_getInstance().dispatch_n6qzss$(cont, Unit);
return Unit;
};
}
var SafeContinuation_init = Kotlin.kotlin.coroutines.experimental.SafeContinuation_init_n4f53e$;
function suspendCoroutine$lambda(closure$block) {
return function (c) {
var safe = SafeContinuation_init(c);
closure$block(safe);
return safe.getResult();
};
}
function delay(delay, continuation) {
return suspendCoroutine$lambda(delay$lambda(delay))(continuation.facade);
}
function suspend(continuation_0, suspended) {
var instance = new Coroutine$suspend(continuation_0);
if (suspended)
return instance;
else
return instance.doResume(null);
}
}
Where does that continuation parameter come from? am I doing something wrong?
I also have an error saying that $receiver is undefined in the main kotlin.js file, which is wierd as I do not use any receiver in any of my coroutines for now.
Again my lib works just fine with Java, what am I doing wrong for it to fail in Js ?