Created
July 15, 2021 20:34
-
-
Save arberg/8445c185372da82374833f53df384f1b to your computer and use it in GitHub Desktop.
Kotlin Coroutine Callback
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
import kotlinx.coroutines.CoroutineScope | |
import kotlinx.coroutines.Dispatchers | |
import kotlinx.coroutines.channels.Channel | |
import kotlinx.coroutines.launch | |
interface OnCompleteCallback { | |
fun complete() | |
/** | |
* Waits for complete callback | |
*/ | |
suspend fun await() | |
} | |
class DefaultOnCompleteCallback : OnCompleteCallback { | |
private val channel = Channel<Unit>(1) | |
override fun complete() { | |
CoroutineScope(Dispatchers.Main).launch { | |
channel.send(Unit) | |
} | |
} | |
override suspend fun await() { | |
channel.receive() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment