RockingChair
RockingChair

Reputation: 97

Pair Rx Sequences with one sequence as the master who controls when a new output is published

I'd like to pair two sequences D and A with Reactive Extensions in .NET. The resulting sequence R should pair D and A in a way that whenever new data appears on D, it is paired with the latest value from A as visualized in the following diagram:

D-1--2---3---4---
A---a------b-----
R----2---3---4---
     a   a   b   

CombineLatest or Zip does not exactly what I want. Any ideas on how this can be achieved?

Thanks!

Upvotes: 0

Views: 63

Answers (1)

Timothy Shields
Timothy Shields

Reputation: 79461

You want Observable.MostRecent:

var R = A.Publish(_A => D.SkipUntil(_A).Zip(_A.MostRecent(default(char)), Tuple.Create));

Replace char with whatever the element type of your A observable.

Conceptually, the query above is the same as the following query.

var R = D.SkipUntil(A).Zip(A.MostRecent(default(char)), Tuple.Create));

The problem with this query is that subscribing to R subscribes to A twice. This is undesirable behavior. In the first (better) query above, Publish is used to avoid subscribing to A twice. It takes a mock of A, called _A, that you can subscribe to many times in the lambda passed to Publish, while only subscribing to the real observable A once.

Upvotes: 1

Related Questions