Rx Framework Reactive Extensions for.Net and JavaScript Andrea Nasoni & Diego Guidi
What is Rx? "Rx is a library for composing asynchronous and event-based programs using observable collections." (cit.) Asynchronous and event-based o No more 'Dispatcher.Invoke' calls o subscribe/unsubscribe made easy Composition o filters/projections o combining events When.OnMouseDown.Then.OnMouseMove.DoWork.Until.OnMouseUp Observable collections o your mouse as a database
IObservable to the rescue IObservable : a data source that can be observed public interface IObservable IDisposable Subscribe(IObserver observer); IObserver : an object that observes public interface IObserver void OnNext(T value); void OnCompleted(); void OnError(Exception error);
Push vs Pull IEnumerable : a data source that can be iterated public interface IEnumerable IEnumerator GetEnumerator(); IEnumerator : an object that iterates public interface IEnumerator : IDisposable bool MoveNext(); T Current { get; } void Reset(); Duality : data source pushes data at its observers enumerable data sources are being pulled by an enumerator IObservable : powered by Linq!
Async programming is Hard Many kinds of sources, many ways to handle them o Timers o Events o Calling services o Custom invokes o Dispatcher.Invoke... BeginCall...EndCall...IAsyncResult? Error management? Cancelation?
Push sources (1) actually... Events are a push source for EventArgs objects Timers are a push source for time passing Asynchronous Services are a push source for your result...
Push sources (2) Every async operation callback can be a push "data" source! IEnumerable (and IQueryable ) o is a data source o can be queried, projected, filtered with Linq Here comes IObservable (and IQbservable ) o interface in.Net Framework 4.0 We can use Linq to query our timebased datasource Here comes rx framework
IEnumerable vs. IObservable Pull: IEnumerable (inline code) foreach (T value in...) foreach loop ends try/catch exception handling Push: IObservable (IObserver ) OnNext(T value) OnCompleted() OnError(Exception ex)
Rx in.Net Framework 3.5, 4.0, Silverlight, Windows Phone, Xna... Assemblies: o System.CoreEx (Scheduler, Disposable, Event,...) General purpose classes o System.Reactive rx framework extension methods and helper methods depends on System.CoreEx, IObservable implementation o (System.Interactive) IEnumerable extensions o (System.Reactive.Testing) For testing purposes
Rx in.Net Async/Push based notification, starting from: o Events Observable.FromEvent(obj, "EventName") o AsyncPattern Observable.FromAsyncPattern(BeginMethod, EndMethod) o Functions var async = func.ToAsync(); async(param1).Subscribe(...) o Custom implementation Implementing IObservable interface using ISubject
Features Linq based approach Projections Filtering Composition Dispose-based un-registration Error management
Observable sources From Events o IObservable > From Async Pattern o Web/WCF service async invoke Helper methods o Observable.{Method}(...) o es. Observable.Interval(TimeSpan.FromSeconds(1))
Observable Manipulation Filtering o observable.Where(x => x < 10) Projection o observable.Select(x => x.GetPosition(this)) Observable Composition o Starting from my left mouse button down and until I don't release it, draw a polyline with a point every 20 milliseconds o TakeWhile, SkipUntil...
Examples Observable.Interval Observable.ToAsync (Async function call) Observable.FromAsyncPattern (Wcf async call) OnError/OnCompleted Polling a value/Dispose subscription Observable.FromEvent ObserveOnDispatcher Wpf Drag & Drop Wpf Polyline? CancellationToken
More on Rx Framework Hot and cold observables Side-effects Publish/Connect/RefCount Run/Do ISubject and its implementations Schedulers Disposables implementations Observable.Create/CreateWithDisposable Generated code for Extension methods for rx events
Rx extensions for JavaScript code, code, code...
Finally... Rx Hands-On Labs: published.aspx Rx Wiki: Matthew Podwysocki's blog:
Thats all ;) Rx rocks!