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 setupMovies(completion: Continuation<Any?>) { | |
val continuation = completion as? SetupMoviesStateMachine | |
?: SetupMoviesStateMachine(completion) | |
when(continuation.label) { | |
0 -> { | |
throwOnFailure(continuation.result) | |
makeLogin("someValue", "someValue", continuation) | |
continuation.label = 1 | |
} |
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 setupMovies(completion: Continuation<Any?>) { | |
val continuation = .. | |
when(continuation.label) { | |
.. | |
3 -> { | |
println("Terminou") | |
continuation.resume(Unit) | |
} | |
else -> throw IllegalStateException(...) |
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 setupMovies() { | |
val token = makeLogin("someValue", "someValue") | |
val movies = loadMovies(token) | |
printMovies(movies) | |
println("Terminou") | |
} |
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
override fun invokeSuspend(result: Any?) { | |
this.result = result | |
setupMovies(this) | |
} |
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 setupMovies(completion: Continuation<Any?>) { | |
val continuation = completion as? SetupMoviesStateMachine | |
?: SetupMoviesStateMachine(completion) | |
when(continuation.label) { | |
0 -> { | |
makeLogin("someValue", "someValue", continuation) | |
continuation.label = 1 | |
} | |
1 -> { |
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 setupMovies(completion: Continuation<Any?>) { | |
class SetupMoviesStateMachine(completion: Continuation<Any?>): CoroutineImpl(completion) { | |
var token: Token? = null | |
var movies: List<Movie>? = null | |
var result: Any? = null | |
var label: Int = 0 | |
override fun invokeSuspend(result: 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
suspend fun setupMovies(completion: Continuation<Any?>) { | |
when(label) { | |
0 -> { | |
val token = makeLogin("someValue", "someValue") | |
} | |
1 -> { | |
val movies = loadMovies(token) | |
} | |
2 -> { | |
printMovies(movies) |
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 makeLogin(login: String, password: String): Token { | |
// make request | |
return Token("akjsdhakjhdkshdakdjhkashdkaj") | |
} | |
suspend fun loadMovies(token: Token): List<Movie> { | |
// make request with token | |
return listOf(Movie("Avengers 1"), Movie("Duna")) | |
} |
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 <T> Continuation<T>.resume(value: T) | |
fun <T> Continuation<T>.resumeWithException(exception: Throwable) |
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
interface Continuation<in T> { | |
val context: CoroutineContext | |
fun resume(value: T) | |
fun resumeWithException(exception: Throwable) | |
} |
NewerOlder