Reactive Extensions

  • View

  • Download

Embed Size (px)


Slides from the 20th meeting of the St. Petersburg Alt.Net User Group

Text of Reactive Extensions

  • 1.Dmitri Nesteruk dmitri@activemesa.com http://spbalt.netBased on materials by Scott Weinstein

2. 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 3. In a push paradigm, data is processed via demands of the source. Common examples include: Device measurements such as time light heat User triggered data such as Mouse & Keyboard events UI events Sales transactions Asynchronous code 4. In .Net Pulled data is exposed via a single core interfaceIEnumerable/IEnumerator Pushed data is exposed viaEventsAd-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 its own way 5. LINQ provides a composable, and standard way to do list manipulationsThe Reactive Extensions (RX) attempt toProvide a common interface for Pushed dataIObservable/IObserverEnable Linq over Pushed data, providingcomposiblity and standard operators 6. Available for free from Domains MS DevLabsEvents APM Support for Web service calls .Net 3.5 SP1Async workflows .Net 4Task-based calls Silverlight 3 Applications JavaScriptWeb page d/l Not part of .Net 4 :( Async file IO Except the interfaces Streaming OLAP 7. 5 assemblies (including PFX)System.CoreEx Feel free to ILmerge Main features System.Interactive Interfaces (IObserver, IObservable) System.Observable Observer conversions LINQ supportSystem.Reactive WinForms & WPF notification supportSystem.Threading (PFX) Infrastructure No particular namespace 8. Implemented by the object that wants to receive notifications OnNext(T value) Called when a value has been provided OnError(Exception ex) Called in case of an error OnCompleted() When done The contract is OnNext* (OnError|OnCompleted) 9. Implemented by an object which can be observed IDisposable Subscribe( IObserver obs); Called by the observer to subscribe Typical behavior Adds each observer to a List> Calls OnXxx() on each listed observer Dispose() called to unsubscribe More composable no need for Unsubscribe() 10. Typically, you would not implement these interfaces Use helper methods to get Observable Use Subscribe() to act as an Observer Observable.FromEvent() Observable.FromAsyncPattern() EnumerableEx.ToObservable() Enumerable Observable Observable.Interval Generates an event in an interval 11. Observable.FromXxx lets you create an anonymous observable Subscribe() lets you create an anonymous observer Parameters take An IObserver A combination of Actions that correspond to OnNext, OnError and OnCompleted 12. New LINQ combinatorsAmb TimeCombineLatest Presence/absenceDelay Value changes Repeat Value patterns Utility TakeUntil/TakeWhile SkipUntil/SkipWhile Mirrored in IEnumerable Scan EnumerableExTimer Using 13. Scan CombineLatest Yields running aggregate Combines each pair of values latest valueslist = { 1, 2, 3 }; o.Scan((a,b) => a+b)yields1, 3, 6 14. Merge Zip Collates two streams into Pairwise collection of one items in stream 15. 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)); 16. Rx itself Rx Team Blog of Rx videos Rx PowerToys machine schedulerMarble diagram generator (.Net 4) Shameless plug: 17. Questions?