Reactive Extensions


Published on

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

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Reactive Extensions

  1. 1. Dmitri Nesteruk Based on materials by Scott Weinstein
  2. 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 file summing the numbers in an array iterating though a database query traversing a directory listing paging through an Amazon search
  3. 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. 4. In .Net Pulled data is exposed via a single core interface IEnumerable/IEnumerator Pushed data is exposed via Events Ad-hoc delegate callbacks Ad-hoc subscribe/callback interfaces BeginXXX/EndXXX Async pattern 3rd party attempts at Linq for Pushed data (Clinq, Slinq, PushLinq) Each implementation is unique is it’s own way
  5. 5. LINQ provides a composable, and standard way to do list manipulations The Reactive Extensions (RX) attempt to Provide a common interface for Pushed data IObservable/IObserver Enable Linq over Pushed data, providing composiblity and standard operators
  6. 6. Available for free from Domains MS DevLabs Events APM Support for Web service calls .Net 3.5 SP1 Async workflows .Net 4 Task<T>-based calls Silverlight 3 Applications JavaScript Web page d/l Not part of .Net 4 :( Async file IO Except the interfaces Streaming OLAP
  7. 7. 5 assemblies (including PFX) System.CoreEx Feel free to ILmerge Main features System.Interactive Interfaces (IObserver<T>, IObservable<T>) System.Observable Observer conversions LINQ support System.Reactive WinForms & WPF notification support System.Threading (PFX) Infrastructure No particular namespace
  8. 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. 9. Implemented by an object which can be observed IDisposable Subscribe( IObserver<T> obs); Called by the observer to subscribe Typical behavior Adds each observer to a List<IObserver<T>> Calls OnXxx() on each listed observer Dispose() called to unsubscribe More composable – no need for Unsubscribe()
  10. 10. 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 → Observable Observable.Interval Generates an event in an interval
  11. 11. Observable.FromXxx lets you create an anonymous observable Subscribe() lets you create an anonymous observer Parameters take An IObserver<T> A combination of Action<T>’s that correspond to OnNext, OnError and OnCompleted
  12. 12. New LINQ combinators Amb Time CombineLatest Presence/absence Delay Value changes Repeat Value patterns Utility TakeUntil/TakeWhile SkipUntil/SkipWhile Mirrored in IEnumerable Scan EnumerableEx Timer Using
  13. 13. Scan CombineLatest Yields running aggregate Combines each pair of values latest values list = { 1, 2, 3 }; o.Scan((a,b) => a+b) yields 1, 3, 6
  14. 14. Merge Zip Collates two streams into Pairwise collection of one items in stream
  15. 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. 16. Rx itself Rx Team Blog Lots of Rx videos Rx PowerToys Tracing Time machine scheduler Marble diagram generator (.Net 4) Shameless plug:
  17. 17. Questions?