I\'m trying out Rx because it seems like a good fit for our domain but the learning curve has taken me by surprise.
I need to knit together historical price data wit
How about something like:
public static IObservable CombineWithHistory(this IObservable live, IObservable history, Func selector)
{
var replaySubject = new ReplaySubject();
live.Subscribe(replaySubject);
return history.Concat(replaySubject).Distinct(selector);
}
This uses a sequence id and distinct to filter the duplicates.
And the corresponding tests:
var testScheduler = new TestScheduler();
var history = testScheduler.CreateColdObservable(
OnNext(1L, new PriceTick { PriceId = 1 }),
OnNext(2L, new PriceTick { PriceId = 2 }),
OnNext(3L, new PriceTick { PriceId = 3 }),
OnNext(4L, new PriceTick { PriceId = 4 }),
OnCompleted(new PriceTick(), 5L));
var live = testScheduler.CreateHotObservable(
OnNext(1L, new PriceTick { PriceId = 3 }),
OnNext(2L, new PriceTick { PriceId = 4 }),
OnNext(3L, new PriceTick { PriceId = 5 }),
OnNext(4L, new PriceTick { PriceId = 6 }),
OnNext(5L, new PriceTick { PriceId = 7 }),
OnNext(6L, new PriceTick { PriceId = 8 }),
OnNext(7L, new PriceTick { PriceId = 9 })
);
live.Subscribe(pt => Console.WriteLine("Live {0}", pt.PriceId));
history.Subscribe(pt => Console.WriteLine("Hist {0}", pt.PriceId), () => Console.WriteLine("C"));
var combined = live.CombineWithHistory(history, t => t.PriceId);
combined.Subscribe(pt => Console.WriteLine("Combined {0}", pt.PriceId));
testScheduler.AdvanceTo(6L);
If you execute this test, combined emits price ticks with ids 1 to 8.