Tim Malseed
12/02/2021, 11:28 AMdata class Flag<T : Any>(val key: String, val description: String, val defaultValue: T)
class FlagManager() {
private val flagMap = mutableMapOf<Flag<out Any>, MutableStateFlow<Any>>()
fun registerFlag(flag: Flag<Any>) {
if (flagMap.containsKey(flag)) {
throw IllegalStateException("Flag cannot be registered more than once")
}
flagMap[flag] = MutableStateFlow(flag.defaultValue)
}
fun <T : Any> getFlagState(flag: Flag<T>): StateFlow<T> {
if (!flagMap.containsKey(flag)) {
throw IllegalStateException("Flag not registered")
}
return (flagMap[flag] as MutableStateFlow<T>).asStateFlow()
}
fun <T : Any> updateFlagState(flag: Flag<T>, value: T) {
if (!flagMap.containsKey(flag)) {
throw IllegalStateException("Flag not registered")
}
(flagMap[flag] as MutableStateFlow<T>).value = value
}
}
Tgo1014
12/02/2021, 11:43 AMTim Malseed
12/02/2021, 11:50 AM