Last active
March 18, 2020 11:13
-
-
Save manuelvicnt/ee2f32baf934ec291fb6d87d720c84e8 to your computer and use it in GitHub Desktop.
LoginUser suspend fun (compiler generated code) - complete
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
/* Copyright 2019 Google LLC. | |
SPDX-License-Identifier: Apache-2.0 */ | |
fun loginUser(userId: String?, password: String?, completion: Continuation<Any?>) { | |
class LoginUserStateMachine( | |
// completion parameter is the callback to the function that called loginUser | |
completion: Continuation<Any?> | |
): CoroutineImpl(completion) { | |
// objects to store across the suspend function | |
var user: User? = null | |
var userDb: UserDb? = null | |
// Common objects for all CoroutineImpl | |
var result: Any? = null | |
var label: Int = 0 | |
// this function calls the loginUser again to trigger the | |
// state machine (label will be already in the next state) and | |
// result will be the result of the previous state's computation | |
override fun invokeSuspend(result: Any?) { | |
this.result = result | |
loginUser(null, null, this) | |
} | |
} | |
val continuation = completion as? LoginUserStateMachine ?: LoginUserStateMachine(completion) | |
when(continuation.label) { | |
0 -> { | |
// Checks for failures | |
throwOnFailure(continuation.result) | |
// Next time this continuation is called, it should go to state 1 | |
continuation.label = 1 | |
// The continuation object is passed to logUserIn to resume | |
// this state machine's execution when it finishes | |
userRemoteDataSource.logUserIn(userId!!, password!!, continuation) | |
} | |
1 -> { | |
// Checks for failures | |
throwOnFailure(continuation.result) | |
// Gets the result of the previous state | |
continuation.user = continuation.result as User | |
// Next time this continuation is called, it should go to state 2 | |
continuation.label = 2 | |
// The continuation object is passed to logUserIn to resume | |
// this state machine's execution when it finishes | |
userLocalDataSource.logUserIn(continuation.user, continuation) | |
} | |
2 -> { | |
// Checks for failures | |
throwOnFailure(continuation.result) | |
// Gets the result of the previous state | |
continuation.userDb = continuation.result as UserDb | |
// Resumes the execution of the function that called this one | |
continuation.cont.resume(continuation.userDb) | |
} | |
else -> throw IllegalStateException(...) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment