Download presentation
Presentation is loading. Please wait.
1
Rx Framework Reactive Extensions for.Net and JavaScript Andrea Nasoni & Diego Guidi
2
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
3
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);
4
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!
5
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?
6
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...
7
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
8
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)
9
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
10
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
11
Features Linq based approach Projections Filtering Composition Dispose-based un-registration Error management
12
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))
13
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...
14
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
15
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
16
Rx extensions for JavaScript code, code, code...
17
Finally... Rx @DevLavs: http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx Rx Hands-On Labs: http://blogs.msdn.com/b/rxteam/archive/2010/07/15/rx-hands-on-labs- published.aspx Rx Wiki: http://rxwiki.wikidot.com Matthew Podwysocki's blog: http://codebetter.com/blogs/matthew.podwysocki
18
Thats all ;) Rx rocks!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.