Rx Extensions


Links

 

Notes 

Additional Notes:

RX.NET

 

Linq is good if you have the data ahead of time (i.e. compiler)

 

[1,2,3].Select(x => x* 5)  >>> [5,10,15]

 

RX is good if you do not know the data yet (i.e. stock ticker).

 

[Series of mouse events].Where(m =>InsideWindow(m))

 

Example

 

Iobservable

OnNext(T value)

OnCompleted()

OnError(Exception)

 

Write an Async Functions

 

Download a web page as a string

 

Iobservable<string> DownloadWebSite(string uri);

 

DownloadWebSite("…").First();

 

First in RX is blocking

 

String[] sites = { "http://foo", "http://bar","http://baz" }

 

Subscribe will trigger when each download happens

Sites.ToObservable().SelectMany(site => site.DownloadWebSite(site)).Subscribe(siteString => console.WriteLine(siteString ));

 

Subscribe will trigger when all downloads are done (Aggregate)

Sites.ToObservable().SelectMany(site => site.DownloadWebSite(site)).Aggregate(new List<string>(), (acc, x) => acc.Add(x)).Subscribe();

 

Hot and Cold Observable

 

Hot are things like  mouse move (they happen if you subscribe or not).

 

Cold are things like the code above where the download does not happen until subscribed.

 

AsyncSubject - solve the race condition if you subscribe after the "event" is done.

 

Finally is a function that is called when done.

 

Retry(X) will retry if event of fails

 

Timeout()

 

DoWhile() continue retry until some event.

 

Repeat() continual re-subscribe.

 

Defer() for hot observable allow data to be made cold.

 

Ischeduler is where things happen (example TaskPoolScheduler), what context.

 

ObserveOn(Scheduler.Dispatcher) - guaranteed on the UI thread.

 

Reactive UI (MVVM OSS project).

 

Example:

 

someObj.WhenAny(x => x.foo, x=> x.bar, (foo, barr) => {}).ToProperty(this, x => x.Baz);

 

Color picker (Red, Green, Blue)

 

someObj.WhenAny(x => x.foo, x=> x.bar, (foo, barr) => {}).ToProperty(this, x => x.Baz).SelectMany(WebService).ToProperty());

 

Cancellation:

Unsubscribe will stop listening.

 

.SelectMany( x=> webService).Switch()

 

 ------------------------------>

   ------->

      ---------------------------------->

             --------->

 

Switch select latest thing.

 

If you write an Observable, must have an action when it is disposed (could do nothing).

 

RX was written by Hascal programmers.

 

Subject is an observable that you control.

 

Observable.Create((subj) => {

Subj.OnNext(1);

Subj.OnNext(2);

Subj.OnNext(3);

Subj.OnComplete();

Return() => subj;

});

 

Func<IObserver<T>, Action>

 

Simpler

 

Subject<int> foo;

foo.Subscribe(Console.WriteLine);

foo.OnNext(3);

….

foo.OnComplete();

 

LinqPad can understand RX (copy binaries into).