https://kotlinlang.org logo
#flow
Title
# flow
m

Marc Knaup

10/13/2020, 11:20 PM
I was hoping that I can use
stateIn
plus
distinctUntilChanged
to avoid expensive re-computations after a Flow was temporarily cold. Unfortunately that doesn’t work because
distinctUntilChanged
doesn’t maintain state when the Flow turns cold. Does anybody have an idea how I can implement the following scenario with existing operators? Easy part: 1. My shared flow turns hot because of first subscriber 2. Upstream emits its latest state and periodically emits new states 3. Ignore value if same as previous value 4. Perform expensive transformation (recompute a graph index when vertices and/or edges change) 5. Share expensive value with current and future subscribers (i.e. replay = 1) Difficult part: 6. My shared flow turns cold because all subscribers are gone 7. My shared flow turns hot again because of first subscriber 8. Upstream emits its latest state and periodically emits new states 9. Ignore value if same as previous value ⬅️ doesn’t work for initial value b/c Flow was cold and operator was reset 10. Perform expensive transformation ⬅️ unnecessarily applied again for a value that was already transformed and cached in replay buffer 11. Share expensive value with current and future subscribers (i.e. replay = 1) I kinda need either • a
distinctUntilChanged
that survives a Flow turning cold or • a
stateIn(…, replay = false)
that ignores the upstream value because it’s the same as its current state but that doesn’t replay it.
Here’s one concrete instance of my use cases: https://gist.github.com/fluidsonic/e5e12e3940e89639719d4f390e41a3ca Building the navigation data for a graph of systems is expensive and can take up to 30 minutes. Therefor the Flow is shared and the upstream value made distinct. Unfortunately when the entire Flow was temporarily code the upstream will re-sent its value next time this Flow turns hot again.
distinctUntilChanged
won’t catch that and the expensive computation is repeated.
Looks like I’m looking for a way to pause Flows 🤔
I.e. “maintain your state, but stop any work for now until I need updates again”
2 Views