The And method matches when both observables have an available value. The Then method projects a value when all observables have an available value. Finally the When joins together the results from several patterns. The example contains an input observable which is created from the Generate method. You should achieve the same result with the Interval method instead as you can see with the second observable, but I wanted to show you how you can create a sequence generator with a time selector as a last argument. The results from the first observable are Timestamped so we can see when was the value generated. The results from both observables are joined together every 2 seconds and sent to the subscriber.
var input = Observable
.Generate(0, _ => true, x => ++x, x => x, _ => TimeSpan.FromSeconds(2))
.Timestamp()
.Take(5);
var pattern = input.And(Observable.Interval(TimeSpan.FromSeconds(1)));
var source =
Observable.When(
pattern
.Then((left, right) => String.Format("{0} - {1}", left, right))
);
var observer = Observer.Create<string>(
x => Console.WriteLine("OnNext: {0}", x),
ex => Console.WriteLine("OnError: {0}", ex.Message),
() => Console.WriteLine("OnCompleted")
);
source.Subscribe(observer);
==========
OnNext: 0@22.12.2011 20:16:37 +01:00 - 0
OnNext: 1@22.12.2011 20:16:39 +01:00 - 1
OnNext: 2@22.12.2011 20:16:41 +01:00 - 2
OnNext: 3@22.12.2011 20:16:43 +01:00 - 3
OnNext: 4@22.12.2011 20:16:45 +01:00 - 4
OnCompleted
==========
No comments:
Post a Comment