KEMBAR78
Reactive Extensions | PDF
Dmitri Nesteruk
dmitri@activemesa.com

http://activemesa.com
http://spbalt.net

Based on materials
by 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 file
     summing the numbers in an array
     iterating though a database query
     traversing a directory listing
     paging through an Amazon search
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
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
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
Available for free from   Domains
MS DevLabs                  Events
http://bit.ly/reext         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
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
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)
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()
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
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
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
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
Merge                       Zip
Collates two streams into   Pairwise collection of
one                         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/reext
Rx Team Blog http://blogs.msdn.com/RxTeam/
   Lots of Rx videos
Rx PowerToys
http://rxpowertoys.codeplex.com/
   Tracing
   Time machine scheduler
   Marble diagram generator (.Net 4)
Shameless plug: http://bit.ly/rxrus
Questions?

Reactive Extensions

  • 1.
  • 2.
    Multiple data itemscan 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.
    In a pushparadigm, 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 Pulleddata 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.
    LINQ provides acomposable, 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.
    Available for freefrom Domains MS DevLabs Events http://bit.ly/reext 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.
    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.
    Implemented by theobject 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 anobject 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.
    Typically, you wouldnot 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.
    Observable.FromXxx lets youcreate 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
  • 13.
    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
  • 14.
    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
  • 15.
    Merge Zip Collates two streams into Pairwise collection of one items in stream
  • 17.
    How observables producevalues 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));
  • 18.
    Rx itself http://bit.ly/reext RxTeam Blog http://blogs.msdn.com/RxTeam/ Lots of Rx videos Rx PowerToys http://rxpowertoys.codeplex.com/ Tracing Time machine scheduler Marble diagram generator (.Net 4) Shameless plug: http://bit.ly/rxrus
  • 19.