saying I have a data class MyHeavyDataClass(...)
“heavy” data class with multiple fields / nested fields / anything that would cause MyHeavyDataClass::equals()
to run in non-trivial perfomance-hitting duration.
now let’s say I (as the feature developer) want to expose a StateFlow<MyHeavyDataClass>
using MutableStateFlow under the hood.
Let’s also say that I know what I’m emitting, and want to exclude the == checks on the state flow.
currently the best thing I came up with was:
data class Wrapper(val uniqueId: Int, val data: MyHeavyDataClass)
private var uniqueIdGenerator = 0
// when emitting:
val someDataClassInstance = ... // compute it
_stateFlow.value = Wrapper(uniqueIdGenerator++, someDataClassInstance)
using the uniqueId field as breaker for the .equals() so the state flow won’t get into calculating .equals for the actual heavy data class.
this adds complexity to both the emitting code (need to syncronize uniquness) and collecting code (need to access extra field .data
)
Ideally I would like something like android-jetpack-compose added to their State
class (which is basically an observable with getter for current value, very similar to StateFlow)
something like this:
fun <T> MutableStateFlow(firstValue: T, equalsPolicy: (T, T) -> Boolean = T::equals)
then I can do custom equality checks from the creator the the StateFlow instance, so I can control the distinctiveness of the stateFlow’s emits.
in my example would just say
val stateFlow = MutableStateFlow(..., equalsPolicy = {_,_ -> false}) // never equals
but I can also see situations where ppl would use the equals policy to only look on fields like “uuid” or similar in situations where a given object with uuid is just a snapshot and same uuid filed on 2 instances would mean same content of data, and the new equalsPolicy{}
can support such scenarios as well.
WDYT?