carbaj0
11/15/2023, 7:38 AMAlejandro Serrano.Mena
11/15/2023, 8:47 AMStore
really reminds me of fritz2's approach, which also focuses on a single flowcopy
version working directly on a MutableFlow
/ MutableState
could be very useful when working with Compose, isn't it?simon.vergauwen
11/15/2023, 9:40 AMupdate
+ copy
out of the box could be very interesting. I am also a big fan of single flow for UI state.Alejandro Serrano.Mena
11/15/2023, 2:36 PMStore
is too opinionated at this point to be included in the main library, but several utilities could really helpcarbaj0
11/15/2023, 4:41 PMStylianos Gakis
11/17/2023, 11:46 AMMutableStateFlow
.
On Android at least, I’ve opted to basically completely stop using this pattern
private val _state = MutableStateFlow(…)
val state = _state.asStateFlow()
to handle screen state, as this more often than not means that you are then creating some hot state that lives for the entire time that the state holder lives for, and does not care for if there are any consumers at the time or not. So this means that if you are listening to any cold flow, you’re turning it hot and are listening to it, even if there may be nobody listening on the values that it produces.
Instead, it’s always better to define a bunch of cold flows for data transformations etc. and then combine them all together in a chain like:
combine(flow1, flow2) { _, _ -> ...}
.stateIn(...)
To get the final StateFlow
which is going to be public API of the state holder class.
So this means that I never end up having a MutableStateFlow
instance to use those extension functions for. Is this something that you folks still end up using? Is this too much of an Android concern perhaps?Alejandro Serrano.Mena
11/17/2023, 2:00 PMStateFlow
(here's the code -> https://github.com/arrow-kt/arrow/pull/3299/files#diff-5488443062b3bef62e232724eda7af102bb35ee694caf248a8e220d6781561f9R23)
what you don't get, though, is the ability to apply a Lens
(only a Getter
) because you cannot mutate it. In other words, since your state
is just a StateFlow
, you can only view more specific information, but not change itStylianos Gakis
11/17/2023, 2:04 PMstateIn()
, you can just call another Flow.map {}
and use normal lenses to change some part of the state you got at that point, right before the last stateIn
call which turns your flow into the StateFlow
that you need.