Closed sergeysmagleev closed 3 years ago
Warnings | |
---|---|
:warning: | It looks like code was changed without adding anything to the Changelog. If this is a trivial PR that doesn't need a changelog, add #trivial to the PR title or body. |
Generated by :no_entry_sign: dangerJS
Greetings!
I came up with an operator I would like to propose. The WIP name is
overridingScan
, but I would gladly consider better names you might suggest. The idea behind it is to make an operator that works the same wayscan
does, but so that an accumulated value can be reset to some other value that comes from a different observable, like so:Here, the intermediate sum produced by applying the accumulation function of the
scan
operator over the sequenceb
is overridden by a value coming from the sequencea
. Once this happens, this value becomes the new "seed" and the following accumulating operation uses it as the last sum for all the subsequent values fromb
. This idea came to my mind when I wanted to achieve the behavior when the new items can be appended to an array using thescan
operator, but so that the array can also be refreshed and filled with completely new values, e.g. scrolling down a table view loads new cells, but scrolling it up refreshes it entirely. Normally this is achieved usinga.flatMapLatest { b.scan(...).startWith($0) }
, but in this scenarioa
should always come beforeb
. A possible workaround isa.startWith(seed)
, but it will emit an initial value that might be unwanted. The usefulness of this operator might seem questionable, but I decided to propose it anyway. This PR doesn't include a readme section or a playground demo yet.Any feedback is greatly appreciated!