Skip to content

Instantly share code, notes, and snippets.

@vlad-kasprov
Last active October 29, 2023 08:57
Show Gist options
  • Save vlad-kasprov/ca73e916708493381c33f2aaa6999873 to your computer and use it in GitHub Desktop.
Save vlad-kasprov/ca73e916708493381c33f2aaa6999873 to your computer and use it in GitHub Desktop.
class ExampleRepository {
private val cache = MutableStateFlow<Example?>(null)
/**
* Observe the current value. Emits `null` if it's empty.
*/
fun observe(): Flow<Example?> = cache.asStateFlow()
/**
* Get the current value. Returns `null` if it's empty.
*/
fun get(): Example? = cache.value
/**
* Update the current value. Setting `null` will clear it.
*/
fun set(value: Example?) {
cache.update { value }
}
/**
* Clear the current value.
*/
fun clear() {
cache.update { null }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment