supertopi
supertopi

Reputation: 3488

Rx.NET: Take until specified count or timer elapsed

I have a solution for receiving Observable notifications until specified count arrived or time threshold elapsed. Also, I need to know which one happened.

Wondering if there is a simpler way (maybe other than GroupByUntil) to achieve this functionality

_values.
.GroupByUntil(_ => true,
              i => Observable.Timer(Threshold, _scheduler)
                             .Amb(i.Buffer(SpecifiedCount).Select(_ => SpecifiedCount)))
// this is for figuring out which one happened: interval elapsed or count filled
.SelectMany(g => g.Count())
// Let's say if count filled first, call Foo()
.Where( i => i == SpecifiedCount ) 
.Subscribe( _ => Foo() )

Upvotes: 1

Views: 907

Answers (1)

Panagiotis Kanavos
Panagiotis Kanavos

Reputation: 131728

Are you looking for Observable.Buffer Method (IObservable, TimeSpan, Int32)? According to the docs

Indicates each element of an observable sequence into a buffer that’s sent out when either it’s full or a given amount of time has elapsed.

You should be able to write:

var myObservable=_values.Buffer(Threshold,SpecifiedCount);

There's a similar overload for Window as well.

Upvotes: 3

Related Questions