Marc Knaup
10/14/2020, 3:31 PM(Mutable)CacheFlow
that extends (Mutable)StateFlow
.
The main difference is that it can be recursive (normal MutableStateFlow) or non-recursive. When it’s non-recursive then providing a new value to the MutableCacheFlow
will not emit it to the same flow, thereby avoiding recursive scenarios.
Collectors of the recursive cache behind the non-recursive cache will still receive newly emitted value. That means if we have a cache
and two non-recursive `MutableCacheFlow`s flow1
and flow2
then updates to flow1
would only be collected by flow2
and updates to flow2
would only be collected by flow1
.
This is especially useful when values coming out of the cache are transformed downstream and then committed to the cache again. With a regular MutableStateFlow
that would cause an endless recursion.
https://gist.github.com/fluidsonic/a06419b1c129f3045558c63cbafecca4
Thoughts?Marc Knaup
10/14/2020, 3:33 PMelizarov
10/14/2020, 3:40 PMMarc Knaup
10/14/2020, 3:50 PMMutableStateFlow
that’s used upstream?
How would would you design that use case instead?
1. read input from cache
2. use latest input to repeatedly emit new values fetched from server
3. commit latest value to cache
The upstream cache flow typically emits only one value (null
or data).
It may in rare cases emit another null
to reset the cache and force new data to be loaded.
In even rarer cases another part of the app may cache a newer value it somehow obtained and this flow needs to operate the refetching logic on the new value.