Created
February 29, 2020 05:29
-
-
Save satoshun/06032c6ded4ed024e99ea34947624f79 to your computer and use it in GitHub Desktop.
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
@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