StateFlow
works using content equality to control conflation. If you set
value
of a MutableStateFlow
to a new object that is equal to the existing value, the "new" value
is not emitted to consumers.
In this example, our state is a State
wrapper around an Int
. State
is a data class
, so we get an equals()
implementation that compares those
Int
values.
Initially, we populate our MutableStateFlow
with State(123)
. We then
update value
with State(456)
25 times. We create new State
objects for
each of those passes, rather than reuse a single State
. Yet, our collector
will only receive the State(456)
and the first of the State(123)
states,
not the remaining 24. Each of the latter 24 State(123)
objects equals()
the
original one, so the collector is not given any of those 24.