Created
October 18, 2020 12:04
-
-
Save chaxiu/58c5733206c07bed2b4ca8d0aabfe2b7 to your computer and use it in GitHub Desktop.
This file contains 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 testCoroutine(completion: Continuation<Any?>): Any? { | |
class TestContinuation(completion: Continuation<Any?>?) : ContinuationImpl(completion) { | |
// Represents the current state of the coroutines state machine | |
var label: Int = 0 | |
// Coroutines results | |
var result: Any? = null | |
// Used to save the calculation results of the previous coroutine | |
var mUser: Any? = null | |
var mFriendList: Any? = null | |
// invokeSuspend is the key | |
// It will eventually call testCoroutine(this) | |
// to start the coroutine state machine | |
// The state machine code is the when statement below | |
// The essence of coroutine is CPS + state machine | |
override fun invokeSuspend(_result: Result<Any?>): Any? { | |
result = _result | |
label = label or Int.Companion.MIN_VALUE | |
// testCoroutine(this) could be called several times | |
return testCoroutine(this) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment