Skip to content

Instantly share code, notes, and snippets.

@arberg
Created July 15, 2021 20:34
Show Gist options
  • Save arberg/8445c185372da82374833f53df384f1b to your computer and use it in GitHub Desktop.
Save arberg/8445c185372da82374833f53df384f1b to your computer and use it in GitHub Desktop.
Kotlin Coroutine Callback
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