Dimitri
Dimitri

Reputation: 23

Keeping track of the completions in a repeating sequence in Rx

Could you please let me know if there's a way of keeping track of the completion of the current repetition in a repeating sequence? My end goal is to keep track of the current count and rate of completed repetitions which I can do once I have the base. Thank you for your help.

sequence.Repeat().Publish().RefCount()

I would like to capture the completions of "sequence" in the structure above.

Upvotes: 2

Views: 102

Answers (2)

Daniel C. Weber
Daniel C. Weber

Reputation: 1011

Use Materialize to convert your observable of T into an observable of Notification<T>. This allows you to handle values, completion and error just like ordinary sequence values. You may then just filter out the completions. However, I did not quite understand what you intend to do with them...

var completions = sequence
    .Materialize()
    .Where(notification => notification.Kind == NotificationKind.OnCompleted)
    .Repeat();

Upvotes: 3

Gluck
Gluck

Reputation: 2962

Daniel's answer is valid, although it'll hide the stream errors, and I'd not sure it's what you wanted. Alternatively you can simply concat your stream with a single-element-stream before repeat, and that special element will be your completion marker:

var completions = sequence
    .Concat(Observable.Return(42))
    .Repeat().Publish().RefCount();

If you need this element to have some context (your question doesn't say), combien it with Observable.Defer:

var completions = sequence
    .Concat(Observable.Defer(() => Observable.Return(someExpression))
    .Repeat().Publish().RefCount();

Upvotes: 1

Related Questions