Intro to RX

Post on 26-Aug-2014

3.649 views 16 download

Tags:

description

Introduction to Reactive Extensions for .Net

Transcript of Intro to RX

Reactive Extensions for .Net

Scott WeinsteinPrincipal

Lab49weblogs.asp.net/sweinstein / @ScottWeinstein

What is it?•The Reactive Extensions for .Net are a

new API from MS Dev labs to enable “Linq over Events”

•Why should I care?▫Offers a better programming model then

Events

Some Concepts

Please sir, can I have some more?

Bid 20, bid 20.2, bid 20.8,…

Pull•For pull, data is processed at the leisure

of the consumer. •The consumer “pulls” the next item from

the producer•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

Push• For push, data is send via demands of the source. • The producer pushes the data to the consumer.• Common examples include:

▫ Device measurements such as time light heat

▫ User triggered data such as Mouse & Keyboard events UI events Sales transactions

▫ Asynchronous code

Push & Pull•In both scenarios, data moves from the

producer to the consumer

Push & Pull in .Net• In .Net Pulled data is exposed via common interface

pair▫ IEnumerable/IEnumerator▫ There is no other way

Foreach and Linq are build on these interfaces• 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 its own special way

LINQ•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

DemoSimple Temperature Alerts

Events to Observables Demo

Func and Action and Lambdas•Func<int> a;

•Func<int,int,int> add = (a,b) => a+b;

•Action<T> t;•Action <T,T> tt;

Some (Category) Theory•There is a formal basis for the RX

Duality, as such, is the assertion that truth is invariant under this operation on statements. In other words, if a statement is true about C, then its dual statement is true about Cop. Also, if a statement is false about C, then its dual has to be false about Cop.

Informally, these conditions state that the dual of a statement is formed by reversing arrows and compositions.

•What this means is that if we can create a Dual of IEnumerable then all of Linq will work over pushed data

Dualizing IEnumeratorinterface IEnumerator<T> { T Current { get; } // but can throw an exception bool MoveNext(); void Reset(); // not used}

• Reverse the arrows

interface IObserverDraft1<T> { SetCurrent(T value); // but need to handle an exception MoveNext(bool can) ;}

Dualizing IEnumerator (2/2)interface IObserverDraft2<T>{ OnNext(T value); OnError(Exception ex); MoveNext(bool can) ; //but called every time!}

interface IObserver<T> { OnNext(T value); OnError(Exception ex); OnCompleted(); // only called once}

Dualizing IEnumerableinterface IEnumerable<T> { IEnumerator<T> GetEnumerator();}

•Reverse the arrowspublic interface IObservableDraft1<T> {

// But how do I stop observing?

SetObserver(IObserver<T> obs); }

Dualizing IEnumerable (2/2)interface IObservableDraft2<T> { Subscribe(IObserver<T> obs);

// can I be more composable?

Unsubscribe(IObserver<T> obs); }

interface IObservable<T> { IDisposable Subscribe(IObserver<T> obs);}

Terse functional explanation•Enumerable:

▫() –> (() –> Option<T>)▫FuncEnumerable<T> –> Func<Func<Option<T>>>

•Observable▫(Option<T> –> ()) –> ()▫FuncObservable<T> –> Action<Action<Option<T>>>

Combinators• Demo

▫ Implement Where()

Some useful Combinators▫ CombineLatest▫ Do▫ ForkJoin▫ GroupBy▫ Scan▫ HoldUntilChanged

▫ Interval▫ Merge▫ ObserveOnDispatcher▫ Sample▫ Throttle▫ Select▫ SelectMany▫ Where▫ Zip

How to create Observables?▫Create▫CreateWithDisposable▫FromAsyncPattern▫FromEvent▫Generate▫ISubject

▫In general, it’s a mistake to create custom implementations of IObservable or IObserver When might it be ok to break this rule?

Streaming OLAP DemoScan, GroupBy, Where, Zip, Merge, SelectMany

Merge•IObservable<T> a

•IObservable<T> b

•A.Merge(B) == IObservable<T> ==

Zip•IObservable<T> a

•IObservable<Y> b

•a.Zip(b, selector) == IObservable<Z> ==

GroupingT3 T2 T1

IObservable<T>

T3 T2 T1

IGroupedObservable<TKey, TElement>

Key

IObservable<IGroupedObservable<Tkey,TElement>>

T3 T2 T1KeyA

T3 T2 T1KeyB

T3 T2 T1KeyC

Hot & Cold• Observables (and Enumerables) come in two

flavors• Hot

▫ Values exist outside of subscription• Cold

▫ Value only exist because of subscription

• Cold observables can be prone to side-effects• ToArray() is one method for making cold

Enumerables hot, Replay() and Publish for Observables (and now Enumerables)

Hot and Cold Demo

Code vs. Data•RX has an alternate method of

representing stream events•Interface defines a code-centric view

▫OnNext▫OnError▫OnCompleted

•Notification defines a data-centric view▫new Notification<T>.OnNext(T next)

•Where is this useful?

Schedulers•RX introduces Schedulers for explicit

control▫ Which thread do subscriptions run on▫How is time represented

▫ ControlScheduler CurrentThreadScheduler▫ DispatcherScheduler ImmediateScheduler▫ NewThreadScheduler SynchronizationContextScheduler▫ TaskPoolScheduler ThreadPoolScheduler

Topics not covered•Joins•Subjects

▫combine Observability and Enumerablility into a single class.

▫BehaviorSubject<T>▫AsyncSubject<T>▫ReplaySubject<T>▫Subject<T>

How to get it?•Download from the DevLabs: Reactive

Extensions for .NET (Rx) site▫.Net 4

In .Net 4 – IObservable/IObserver are part of the BCL

▫.Net 3.5 SP1▫Silverlight 3▫Javascript

•Current license is “go-live” however the API is still evolving

Delayed Expensive Search

DemoAsync webpage download