Skip to content

Instantly share code, notes, and snippets.

@BetterProgramming
Created November 2, 2022 16:15
Show Gist options
  • Save BetterProgramming/8777b652b88053f8a3f90136db9cfbe5 to your computer and use it in GitHub Desktop.
Save BetterProgramming/8777b652b88053f8a3f90136db9cfbe5 to your computer and use it in GitHub Desktop.
val stateJob = flowToCallback(
adapterScope, speechRecognizerStateFlow,
speechRecognizerStateCallback
)
fun <T> flowToCallback(scope: CoroutineScope, flow: Flow<T>,
callback: Consumer<T>): Job {
return scope.launch {
try {
flow.collect {
// invokes the closure which will call C calback
callback.accept(it)
}
} catch (e: CancellationException) {
println("collect cancelled")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment