This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| sealed class Dispatcher { | |
| object Main : Dispatcher() | |
| object Background : Dispatcher() | |
| } | |
| fun launch(dispatcher: Dispatcher, block: suspend () -> Unit) { | |
| //Define a callback | |
| val callback = object : Continuation<Unit> { | |
| override val context = EmptyCoroutineContext |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fun launch(block: suspend () -> Unit) { | |
| val callback = object : Continuation<Unit> { | |
| override val context: CoroutineContext = EmptyCoroutineContext | |
| override fun resumeWith(result: Result<Unit>) {} | |
| } | |
| block.createCoroutineUnintercepted(callback).resumeWith(Result.success(Unit)) | |
| } | |
| ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import kotlin.concurrent.thread | |
| import kotlin.coroutines.Continuation | |
| import kotlin.coroutines.CoroutineContext | |
| import kotlin.coroutines.EmptyCoroutineContext | |
| import kotlin.coroutines.intrinsics.createCoroutineUnintercepted | |
| import kotlin.coroutines.intrinsics.suspendCoroutineUninterceptedOrReturn | |
| import kotlin.coroutines.intrinsics.COROUTINE_SUSPENDED | |
| fun main() { | |
| // Define a suspend lambda |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.lang.Exception | |
| import kotlin.concurrent.thread | |
| import kotlin.coroutines.Continuation | |
| import kotlin.coroutines.CoroutineContext | |
| import kotlin.coroutines.EmptyCoroutineContext | |
| import kotlin.coroutines.intrinsics.COROUTINE_SUSPENDED | |
| fun main() { | |
| // Compiler generates a new class that contains the suspendingLambda | |
| class Continuation1(val previousContinuation: Continuation<Double>) : Continuation<Unit> { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| suspend fun calculateSquareRoot(number: Double): Double { | |
| val block: (Continuation<Double>)->Any? = { | |
| // Wait for result in current thread | |
| kotlin.math.sqrt(number) | |
| } | |
| return suspendCoroutineUninterceptedOrReturn(block) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public interface Continuation<in T> { | |
| // A map like data structure to hold data carried by the continuation | |
| public val context: CoroutineContext | |
| // Called to complete the unfinished execution | |
| public fun resumeWith(result: Result<T>) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import kotlin.coroutines.Continuation | |
| import kotlin.coroutines.CoroutineContext | |
| import kotlin.coroutines.EmptyCoroutineContext | |
| import kotlin.coroutines.intrinsics.createCoroutineUnintercepted | |
| fun launch(block: suspend () -> Unit) { | |
| val callback = object : Continuation<Unit> { | |
| override val context: CoroutineContext = EmptyCoroutineContext | |
| override fun resumeWith(result: Result<Unit>) {} | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import kotlin.coroutines.Continuation | |
| import kotlin.coroutines.CoroutineContext | |
| import kotlin.coroutines.EmptyCoroutineContext | |
| import kotlin.coroutines.intrinsics.createCoroutineUnintercepted | |
| import kotlin.coroutines.intrinsics.COROUTINE_SUSPENDED | |
| fun main() { | |
| // Compiler generates a new class that contains the suspendingLambda | |
| class Continuation1(val previousContinuation: Continuation<String>) : Continuation<Unit> { | |
| override val context = previousContinuation.context |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import kotlin.coroutines.Continuation | |
| import kotlin.coroutines.CoroutineContext | |
| import kotlin.coroutines.EmptyCoroutineContext | |
| import kotlin.coroutines.intrinsics.createCoroutineUnintercepted | |
| fun main() { | |
| // Define a suspend lambda that returns "Hello World!" | |
| val suspendingLambda: suspend () -> String = suspend { | |
| "Hello World!" | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Composable | |
| fun ControlledExplosion() { | |
| Column( | |
| modifier = Modifier.fillMaxSize(), | |
| verticalArrangement = Arrangement.Center, | |
| horizontalAlignment = Alignment.CenterHorizontally | |
| ) { | |
| var progress by remember { mutableStateOf(0f) } | |
| Explosion(progress) |