Reactive Extensions
-
Upload
dmitri-nesteruk -
Category
Technology
-
view
3.175 -
download
0
description
Transcript of Reactive Extensions
Dmitri [email protected]
http://activemesa.comhttp://spbalt.net
Based on materialsby Scott Weinstein
Multiple data items can be “processed” in either a push or a pull fashion.In a pull paradigm, data is processed at the leisure of the destination. Common examples include:
reading from a filesumming the numbers in an arrayiterating though a database querytraversing a directory listingpaging through an Amazon search
In a push paradigm, data is processed via demands of the source. Common examples include:
Device measurements such astimelight heat
User triggered data such as Mouse & Keyboard eventsUI eventsSales transactions
Asynchronous code
In .Net Pulled data is exposed via a single core interface
IEnumerable/IEnumeratorPushed data is exposed via
EventsAd-hoc delegate callbacksAd-hoc subscribe/callback interfacesBeginXXX/EndXXX Async pattern3rd party attempts at Linq for Pushed data(Clinq, Slinq, PushLinq)
Each implementation is unique is it’s own way
LINQ provides a composable, and standard way to do list manipulations
The Reactive Extensions (RX) attempt toProvide a common interface for Pushed data
IObservable/IObserverEnable Linq over Pushed data, providing composiblity and standard operators
Available for free from MS DevLabshttp://bit.ly/reextSupport for
.Net 3.5 SP1
.Net 4Silverlight 3JavaScript
Not part of .Net 4 :(Except the interfaces
DomainsEventsAPMWeb service callsAsync workflowsTask<T>-based calls
ApplicationsWeb page d/lAsync file IOStreaming OLAP
5 assemblies (including PFX)
Feel free to ILmergeMain features
Interfaces (IObserver<T>, IObservable<T>)Observer conversionsLINQ supportWinForms & WPF notification supportInfrastructure
No particular namespace
System.CoreEx
System.Interactive
System.Observable
System.Reactive
System.Threading (PFX)
Implemented by the object that wants to receive notifications
OnNext(T value)Called when a value has been providedOnError(Exception ex)Called in case of an errorOnCompleted()When done
The contract isOnNext* (OnError|OnCompleted)
Implemented by an object which can be observedIDisposable Subscribe(IObserver<T> obs);
Called by the observer to subscribeTypical behavior
Adds each observer to a List<IObserver<T>>Calls OnXxx() on each listed observer
Dispose() called to unsubscribeMore composable – no need for Unsubscribe()
Typically, you would not implement these interfaces
Use helper methods to get Observable<T>Use Subscribe() to act as an Observer<T>
Observable.FromEvent()Observable.FromAsyncPattern()EnumerableEx.ToObservable()
Enumerable → ObservableObservable.Interval
Generates an event in an interval
Observable.FromXxx lets you create an anonymous observableSubscribe() lets you create an anonymous observerParameters take
An IObserver<T>A combination of Action<T>’s that correspond to OnNext, OnError and OnCompleted
New LINQ combinatorsTimePresence/absenceValue changesValue patternsUtility
Mirrored in IEnumerableEnumerableEx
AmbCombineLatestDelayRepeatTakeUntil/TakeWhileSkipUntil/SkipWhileScanTimerUsing
ScanYields running aggregate values
list = { 1, 2, 3 };o.Scan((a,b) => a+b)
yields
1, 3, 6
CombineLatestCombines each pair of latest values
MergeCollates two streams into one
ZipPairwise collection of items in stream
How observables produce values even when not subscribed
var timer = new Timer(1000);var hot = Observable.FromEvent(timer, “Elapsed”);
Cold observable produces values onl y on subscriptions
var cold = Obsevable.Interval(TimeSpan.FromSeconds(1));
Rx itself http://bit.ly/reextRx Team Blog http://blogs.msdn.com/RxTeam/
Lots of Rx videosRx PowerToyshttp://rxpowertoys.codeplex.com/
TracingTime machine schedulerMarble diagram generator (.Net 4)
Shameless plug: http://bit.ly/rxrus
Questions?