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 testCoroutine() { | |
log("start") | |
val user = getUserInfo() | |
log(user) | |
val friendList = getFriendList(user) | |
log(friendList) | |
val feedList = getFeedList(friendList) | |
log(feedList) | |
} |
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 testCoroutine(completion: Continuation<Any?>): Any? {} |
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 getUserInfo(completion: Continuation<Any?>): Any?{} | |
fun getFriendList(user: String, completion: Continuation<Any?>): Any?{} | |
fun getFeedList(friendList: String, completion: Continuation<Any?>): Any?{} |
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 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 |
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 testCoroutine(completion: Continuation<Any?>): Any? { | |
... | |
val continuation = if (completion is TestContinuation) { | |
completion | |
} else { | |
// As parameter | |
// ↓ | |
TestContinuation(completion) | |
} |
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
// Three variables, corresponding to the three variables of the original function | |
lateinit var user: String | |
lateinit var friendList: String | |
lateinit var feedList: String | |
// The running result of the coroutine | |
var result = continuation.result | |
// The return of the suspending function | |
var suspendReturn: Any? = null |
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
when (continuation.label) { | |
0 -> { | |
// check exceptions | |
throwOnFailure(result) | |
log("start") | |
// Assign 1 to label, ready for the next state | |
continuation.label = 1 | |
// Call getUserInfo() |
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
// "fake" suspending function | |
// Although it has suspend modifier, | |
// it will not really suspend when executed, | |
// because there is no other suspending in its body | |
// ↓ | |
suspend fun noSuspendFriendList(user: String): String{ | |
return "Tom, Jack" | |
} | |
suspend fun testNoSuspend() { |
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
when (continuation.label) { | |
0 -> { | |
... | |
} | |
1 -> { | |
... | |
// function name changed | |
// ↓ | |
suspendReturn = noSuspendFriendList(user, continuation) |
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
if (suspendReturn == sFlag) { | |
} else { | |
// What is the specific code? | |
// ↓ | |
//go to next state | |
} |