Last active
October 29, 2023 08:57
-
-
Save vlad-kasprov/ca73e916708493381c33f2aaa6999873 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
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