[Pitch] Replay

Hi there,

Here is a pitch to introduce a replay operator for AsyncSequence.

The idea is to keep track of the latest elements produced by an async sequence and replay them every time a new iterator is created. To avoid uncontrolled memory footprint, the history should be bounded.

For instance with a replay count of 2:

a first iterator iterates 3 times over a base:
- base produces 'a': 'a'  is stacked in the history ['a'] and the iteration receives 'a'
- base produces 'b': 'b'  is stacked in the history ['a', 'b'] and the iteration receives 'b'
- base produces 'c': 'a' is removed from the history and 'c'  is stacked in the history ['b', 'c'] and the iteration receives 'c'

a second iterator iterates 3 times over the base:
- the history is replayed with ['b', 'c'], the iteration receives 'b' and 'c'
- the base produces 'd': 'b' is removed from the history and 'd'  is stacked in the history ['c', 'd'] and the iteration receives 'd'

This kind of operator could be useful applied after a broadcast operator. It would allow to replay the elements that were produced while all iterators were not yet iterating.

There is a draft PR here: [Replay] implement with a bounded buffer strategy by twittemb · Pull Request #245 · apple/swift-async-algorithms · GitHub

What do you think? (perhaps @tcldr, @FranzBusch or @Philippe_Hausler)

Thanks.

8 Likes

Would love to see something like this coming along with broadcast.

Unsurprisingly we need this right now and are going to use a customized version of Asynchrone's SharedAsyncSequence (for now just replaying the latest element).

1 Like