Reactive Extensions
-
Upload
dmitri-nesteruk -
Category
Technology
-
view
3.176 -
download
0
description
Transcript of Reactive Extensions
![Page 1: Reactive Extensions](https://reader034.fdocuments.us/reader034/viewer/2022050906/554d1fc9b4c905ca208b4abd/html5/thumbnails/1.jpg)
Dmitri [email protected]
http://activemesa.comhttp://spbalt.net
Based on materialsby Scott Weinstein
![Page 2: Reactive Extensions](https://reader034.fdocuments.us/reader034/viewer/2022050906/554d1fc9b4c905ca208b4abd/html5/thumbnails/2.jpg)
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
![Page 3: Reactive Extensions](https://reader034.fdocuments.us/reader034/viewer/2022050906/554d1fc9b4c905ca208b4abd/html5/thumbnails/3.jpg)
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
![Page 4: Reactive Extensions](https://reader034.fdocuments.us/reader034/viewer/2022050906/554d1fc9b4c905ca208b4abd/html5/thumbnails/4.jpg)
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
![Page 5: Reactive Extensions](https://reader034.fdocuments.us/reader034/viewer/2022050906/554d1fc9b4c905ca208b4abd/html5/thumbnails/5.jpg)
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
![Page 6: Reactive Extensions](https://reader034.fdocuments.us/reader034/viewer/2022050906/554d1fc9b4c905ca208b4abd/html5/thumbnails/6.jpg)
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
![Page 7: Reactive Extensions](https://reader034.fdocuments.us/reader034/viewer/2022050906/554d1fc9b4c905ca208b4abd/html5/thumbnails/7.jpg)
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)
![Page 8: Reactive Extensions](https://reader034.fdocuments.us/reader034/viewer/2022050906/554d1fc9b4c905ca208b4abd/html5/thumbnails/8.jpg)
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)
![Page 9: Reactive Extensions](https://reader034.fdocuments.us/reader034/viewer/2022050906/554d1fc9b4c905ca208b4abd/html5/thumbnails/9.jpg)
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()
![Page 10: Reactive Extensions](https://reader034.fdocuments.us/reader034/viewer/2022050906/554d1fc9b4c905ca208b4abd/html5/thumbnails/10.jpg)
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
![Page 11: Reactive Extensions](https://reader034.fdocuments.us/reader034/viewer/2022050906/554d1fc9b4c905ca208b4abd/html5/thumbnails/11.jpg)
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
![Page 12: Reactive Extensions](https://reader034.fdocuments.us/reader034/viewer/2022050906/554d1fc9b4c905ca208b4abd/html5/thumbnails/12.jpg)
![Page 13: Reactive Extensions](https://reader034.fdocuments.us/reader034/viewer/2022050906/554d1fc9b4c905ca208b4abd/html5/thumbnails/13.jpg)
New LINQ combinatorsTimePresence/absenceValue changesValue patternsUtility
Mirrored in IEnumerableEnumerableEx
AmbCombineLatestDelayRepeatTakeUntil/TakeWhileSkipUntil/SkipWhileScanTimerUsing
![Page 14: Reactive Extensions](https://reader034.fdocuments.us/reader034/viewer/2022050906/554d1fc9b4c905ca208b4abd/html5/thumbnails/14.jpg)
ScanYields running aggregate values
list = { 1, 2, 3 };o.Scan((a,b) => a+b)
yields
1, 3, 6
CombineLatestCombines each pair of latest values
![Page 15: Reactive Extensions](https://reader034.fdocuments.us/reader034/viewer/2022050906/554d1fc9b4c905ca208b4abd/html5/thumbnails/15.jpg)
MergeCollates two streams into one
ZipPairwise collection of items in stream
![Page 16: Reactive Extensions](https://reader034.fdocuments.us/reader034/viewer/2022050906/554d1fc9b4c905ca208b4abd/html5/thumbnails/16.jpg)
![Page 17: Reactive Extensions](https://reader034.fdocuments.us/reader034/viewer/2022050906/554d1fc9b4c905ca208b4abd/html5/thumbnails/17.jpg)
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));
![Page 18: Reactive Extensions](https://reader034.fdocuments.us/reader034/viewer/2022050906/554d1fc9b4c905ca208b4abd/html5/thumbnails/18.jpg)
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
![Page 19: Reactive Extensions](https://reader034.fdocuments.us/reader034/viewer/2022050906/554d1fc9b4c905ca208b4abd/html5/thumbnails/19.jpg)
Questions?