Skip to content

Instantly share code, notes, and snippets.

@vchernyshov
Created March 15, 2020 20:51
Show Gist options
  • Save vchernyshov/d661e153e768915c5a2b3a23a7338b1c to your computer and use it in GitHub Desktop.
Save vchernyshov/d661e153e768915c5a2b3a23a7338b1c to your computer and use it in GitHub Desktop.
class State<T>(private val scope: CoroutineScope, initialValue: T? = null) {
private val channel =
initialValue?.let { ConflatedBroadcastChannel(it) } ?: ConflatedBroadcastChannel()
var value: T
get() = channel.value
set(value) {
scope.launch { channel.send(value) }
}
val flow: Flow<T>
get() = channel.asFlow()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment