Skip to content

Instantly share code, notes, and snippets.

@fluxtah
Created January 31, 2020 09:26
Show Gist options
  • Save fluxtah/74f2d1c5e8f5d4d1c62585af0245a216 to your computer and use it in GitHub Desktop.
Save fluxtah/74f2d1c5e8f5d4d1c62585af0245a216 to your computer and use it in GitHub Desktop.
@Composable
fun <T> observe(observable: () -> LiveData<T>, result: (T) -> Unit) {
val thing = stateFor<T?> { null }
onActive {
val observer =
Observer<T> {
thing.value = it
result(it)
}
with(observable()) {
observeForever(observer)
onDispose {
removeObserver(observer)
}
}
}
thing.value
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment