zak.taccardi
05/23/2024, 6:22 PMStateFlow<T>
whose initial value T
is lazily initialized, or asynchronously initialized?
For example, let’s say T
needs to be initially loaded from disk. I would like that T
to be loaded asynchronously or lazily once used, and off the main threadstreetsofboston
05/23/2024, 6:33 PMstreetsofboston
05/23/2024, 6:36 PMsealed class Container<out T> { object Loading: Container<Nothing>; data class Value<T>(private value: T): Container<T>
zak.taccardi
05/23/2024, 6:40 PMstreetsofboston
05/23/2024, 6:41 PMstreetsofboston
05/23/2024, 6:42 PMstreetsofboston
05/23/2024, 6:46 PMfun <T> Flow<T>.withInitialValue<T>(getInitialValue: suspend ()-> T) : Flow<T> {
return this.onStart { emit(getInitialValue()) }
}
streetsofboston
05/23/2024, 6:49 PMZach Klippenstein (he/him) [MOD]
05/23/2024, 9:40 PMvalue
would have to be a suspending property, which would defeat the purpose.zak.taccardi
05/23/2024, 9:45 PMstate.value
is invoked before the async read finishes, then do that blocking readZach Klippenstein (he/him) [MOD]
05/23/2024, 9:49 PMstreetsofboston
05/24/2024, 11:55 AMLoading
(or something similar) as the initial state and that then will fetch and emit the Theme. Start loading that theme asap. Apply a filter
to that StateFlow that filters on the Theme value (filters out the Loading one). Make a method that returns that filtered flow
Then when you need that Theme in a screen, call that method returning that filtered flow, call first()
on it that then returns Theme. Combine that with the rest of your UI-state emissions.