Reactive Extensions

19
Dmitri Nesteruk [email protected] http://activemesa.com http://spbalt.net Based on materials by Scott Weinstein

description

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

Transcript of Reactive Extensions

Page 1: Reactive Extensions

Dmitri [email protected]

http://activemesa.comhttp://spbalt.net

Based on materialsby Scott Weinstein

Page 2: Reactive Extensions

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

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

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

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

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

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

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

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

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

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
Page 13: Reactive Extensions

New LINQ combinatorsTimePresence/absenceValue changesValue patternsUtility

Mirrored in IEnumerableEnumerableEx

AmbCombineLatestDelayRepeatTakeUntil/TakeWhileSkipUntil/SkipWhileScanTimerUsing

Page 14: Reactive Extensions

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

MergeCollates two streams into one

ZipPairwise collection of items in stream

Page 16: Reactive Extensions
Page 17: Reactive Extensions

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

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

Questions?