2

I am using Buffer to collect all events over one minute, and present them into a list:

this.GetServiceAvailablityRxStream                  
    .Buffer(TimeSpan.FromMinutes(1))
    .Subscribe(
        serviceAvailableList =>
        {
           ...
        }

Currently, it buffers at T=1 min, then T=2 min, etc.

I'm wondering if its possible to shift the buffering back by 10 seconds, e.g. make it buffer at T=10 sec, then T=1 min+10 sec, then T=2 min+10 sec, etc.

What I have tried

I have tried all combinations of .Delay, .DelaySubscription, overloads on .Buffer, putting .Delay before and after the .Buffer command, etc, and nothing seems to work.

I'm wondering if this is even possible in RX?

Contango
  • 76,540
  • 58
  • 260
  • 305

2 Answers2

3

Yes. The key insight is the fact that time can be represented as an Observable. So use the overload of Buffer that accepts an observable as the boundary signal and pass it your own signal using Observable.Timer:

var first = TimeSpan.FromSeconds(10);
var remainder = TimeSpan.FromMinutes(1);
var boundaries = Observable.Timer(first, remainder);
source.Buffer(boundaries).Subscribe(...);
Brandon
  • 38,310
  • 8
  • 82
  • 87
2

You need to create a stream to define the buffer boundaries you want. Fortunately this is quite easy. The following will define the 10 second initial buffer interval, followed by subsequent one minute intervals:

var bufferBoundaries =
    Observable.Timer(TimeSpan.FromSeconds(10), TimeSpan.FromMinutes(1));   

Then assuming a source stream of xs you can do:

var bufferedEvents = xs.Buffer(bufferBoundaries);
James World
  • 29,019
  • 9
  • 86
  • 120