Has anyone ran into use cases for overloads of `Fl...
# coroutines
z
Has anyone ran into use cases for overloads of
Flow
operators for
StateFlow
that preserve the
value
property? I filed an issue for
map
here, curious if people have other use cases or use cases for other operators (or opinions about this proposal). https://github.com/Kotlin/kotlinx.coroutines/issues/2081
s
Why hot hold a second reference, a reference to the original StateFlow? Or collect the result Flow again, causing the current state to be collected? I can imagine something like this for
map
, but it wouldn’t work for
filter
for example….
☝🏼 1
I think the same is true for Rx; Calling
map
a Processor/Subject returns an
Observable
(or
Flowable
)
z
Yea, the number of operations this makes sense for is pretty small.
The difference from Rx is that
Subject
isn’t a read-only type, and isn’t suitable for exposing as such.
Why hot hold a second reference, a reference to the original StateFlow?
I’m not sure what you mean. For my use case, the underlying
StateFlow
is completely hidden as an implementation detail of a class, and only the more limited state subset is exposed.
s
The class wrapping/hiding the StateFlow could expose a property getting its StateFlow’s `value`….
z
Doesn’t that defeat the purpose of having
StateFlow
as a separate, read-only type in the first place?
s
I am playing the devil’s advocate here, I can see the usefulness, but it seems very very limited to me
z
I appreciate it, it also seems limited to me so I’m trying to play devils advocate on myself 😅
😀 1
I can also do something like this, but it feels verbose and over-complicated for a simple map:
Copy code
totalState.map { it.partialState }
  .stateIn(scope, initialValue = totalState.value.partialState)
It was just pointed out that this is a duplicate issue, and the original was closed with the rationale “Any chain of operators on a state flow can be materialized into another StateFlow using stateIn operator”, so that answers that. https://github.com/Kotlin/kotlinx.coroutines/issues/2008#issuecomment-632614208
b
will not it a bit misleading since StateFlow provides distinctUntilChanged behavior? I mean: let's say stateFlow contains 0,1,2,3,4,5
stateFlow.map { it / 2 }
will produce 0,1,2 which most likely unexpected there.
z
That seems like reasonable behavior. In my view, using
StateFlow
implies that the values being emitted represent “state”, and two states that are equivalent are not interesting.
☝🏼 1