RxSwiftCommunity / RxSwiftExt

A collection of Rx operators & tools not found in the core RxSwift distribution
MIT License
1.33k stars 213 forks source link

[WIP] overridingScan operator #198

Closed sergeysmagleev closed 3 years ago

sergeysmagleev commented 5 years ago

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 way scan does, but so that an accumulated value can be reset to some other value that comes from a different observable, like so:

a       -----------------------(10)----------------->
                                |
b       -----(1)---(2)---(3)----|----(4)---(5)------>
              |     |     |     |     |     |
              \/    \/    \/    \/    \/    \/
scan(+) -----(1)---(3)---(6)---(10)--(14)--(19)--->

Here, the intermediate sum produced by applying the accumulation function of the scan operator over the sequence b is overridden by a value coming from the sequence a. 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 from b. This idea came to my mind when I wanted to achieve the behavior when the new items can be appended to an array using the scan 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 using a.flatMapLatest { b.scan(...).startWith($0) }, but in this scenario a should always come before b. A possible workaround is a.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!

rxswiftcommunity[bot] commented 5 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