Skip to content

Instantly share code, notes, and snippets.

@satoshun
Created February 29, 2020 05:29
Show Gist options
  • Save satoshun/06032c6ded4ed024e99ea34947624f79 to your computer and use it in GitHub Desktop.
Save satoshun/06032c6ded4ed024e99ea34947624f79 to your computer and use it in GitHub Desktop.
@Composable
fun <T> observe(
data: Flow<T>,
transform: (T) -> T = { it }
): T? {
var result: T? by state { null }
val action: suspend (T) -> Unit = remember { { result = transform(it) } }
onCommit(data) {
val job = GlobalScope.launch(Dispatchers.Main) {
data.collect(action)
}
onDispose { job.cancel() }
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment