ReactiveX - observable like Buffer with a trigger, but only remembers last value

43 views Asked by At

In C#, using Rx.net I have a source observable, and another observable that acts as a trigger. I'm looking for an operator that will transmit the last value from the source observable whenever it receives a value from the trigger - but only if the source observable fired at least once since the last time I transmitted a value.

This can be achieved using the Buffer operator, by filtering only those transmissions in which the buffered values count is greater than 0. This, however, can be very inefficient memory-wise because the buffer will keep all values and I only care about the last.

dataObservable.Buffer(triggerObservable).Where(x => x.Count > 0).Select(x => x.Last()).Register(_ => Trigger());

Is there another way to do this using existing operators, or do I have to create my own?

1

There are 1 answers

0
Oleg Dok On

Try to use Window() operator for this - it will not keep the full list in memory:

dataObservable.Window(triggerObservable).SelectMany(x => x.Last()).Register(_ => Trigger());