private static void MergingObservableOfObservables() { Demo.DisplayHeader("The Merge operator - allows also to merge observables emitted from another observable"); IObservable <string> texts = ObservableExSSSS.FromValues("Hello", "World"); texts .Select(txt => Observable.Return(txt + "-Result")) .Merge() .SubscribeConsole("Merging from observable"); }
private static void ZippingTwoObservables() { Demo.DisplayHeader("The Zip operator - combines values with the same index from two observables"); //temperatures from two sensors (in celsius) IObservable <double> temp1 = ObservableExSSSS.FromValues(20.0, 21, 22); IObservable <double> temp2 = ObservableExSSSS.FromValues(22.0, 21, 24); temp1 .Zip(temp2, (t1, t2) => (t1 + t2) / 2) .SubscribeConsole("Avg Temp."); }
private static void DelayingEachNotificationIndependently() { Demo.DisplayHeader("Throttle operator - each notification can be shifted independently by providing observable that signals the delay is over"); var observable = ObservableExSSSS.FromValues(4, 1, 2, 3); observable .Timestamp() .Delay(x => Observable.Timer(TimeSpan.FromSeconds(x.Value))) .Timestamp() .RunExample("Independent Delay"); }
public static void SubscribeOnAndObserveOn() { Demo.DisplayHeader("using SubscribeOn and ObserveOn together and their effect"); ObservableExSSSS.FromValues(0, 1, 2, 3, 4, 5) .Take(3) .LogWithThread("A") .Where(x => x % 2 == 0) .LogWithThread("B") .SubscribeOn(NewThreadScheduler.Default) .LogWithThread("C") .Select(x => x * x) .LogWithThread("D") .ObserveOn(TaskPoolScheduler.Default) .LogWithThread("E") .RunExample("squares by time"); }
private static void AddToCompositeDisposable() { Demo.DisplayHeader("AddToCompositeDisposable extensions method - useful for keeping your observable pipeline fluent"); var compositeDisposable = new CompositeDisposable(); IObservable <string> observable = ObservableExSSSS.FromValues("Rx", "For", "The", "Win"); observable.Where(x => x.Length % 2 == 0) .Select(x => x.ToUpper()) .Subscribe(x => Console.WriteLine(x)) .AddToCompositeDisposable(compositeDisposable); observable.Where(x => x.Length % 2 == 1) .Select(x => x.ToLower()) .Subscribe(x => Console.WriteLine(x)) .AddToCompositeDisposable(compositeDisposable); }
private static void AggreagateResultInAWindow() { Demo.DisplayHeader("The Window operator - each window is an observable that can be used with an aggregation function"); var donationsWindow1 = ObservableExSSSS.FromValues(50M, 55, 60); var donationsWindow2 = ObservableExSSSS.FromValues(49M, 48, 45); IObservable <decimal> donations = donationsWindow1.Concat(donationsWindow2.DelaySubscription(TimeSpan.FromSeconds(1.5))); var windows = donations.Window(TimeSpan.FromSeconds(1)); var donationsSums = from window in windows.Do(_ => Console.WriteLine("New Window")) from sum in window.Scan((prevSum, donation) => prevSum + donation) select sum; donationsSums.RunExample("donations in shift"); }