Real-world applications of the Reactive Extensions

Preview:

DESCRIPTION

Some concrete exemples of using the Reactive Extensions (Rx) for app development

Citation preview

Real-world applications of Reactive Extensions for .NET (Rx)

Jonas Chapuis, Ph.D., Nexthink S.A.

About me

jonas.chapuis@nexthink.com

www.nexthink.com/careers

“Functional programming allows developers to describe what they want to do, rather than forcing them to describe how they want to do it.”

Anders Hejlsberg, C# creator

Exercise

Versus:JAY

JANE

MARRY

HARRY

readablecompactsafe

IEnumerable & IEnumerator

Oh my…

TimeoutException!

Ok, how about this:

nice

IObservable & IObserver

Yin & Yang

Pull collections Push collections

MoveNext() OnNext()Rx

• Event streams• Asynchronous computations• Asynchronous queries• Asynchronous enumerations• …

• In memory collections• Database queries• Generated sequences• Message queues• …

Where does Rx fit?

Rx shines for– Declarative syntax & compositionality

– Easy time handling (timeout, throttling, etc.)

– Error handling

– Cancellation

– Testing

Example: search box

• Demo

search

Search pipeline

input

react

search

reactive

SelectMany

Input pipeline

input

r re rea reac reaci reac reactive

Throttle 200ms

Where length > 2

DistinctUntilChanged

search

search

Race condition

input

react reactive

SelectMany

TakeUntil

input

react reactive

SelectMany

search

TakeUntil(input)

search

TakeUntil(input)

Timeout

input

react

SelectMany

search

Timeout

Search example Rx network

throttle

timeout

distinct

selectmany

search

takeuntil

Observer

react

reactive

...

Property changed events

Timer

Timer

DictServiceTask<T>

OnError & OnComplete are terminating, i.e. trigger disposal of the network

OnError

OnNext

Example: stock quotes stream

• Demo

Buffer(5)

MSFT27.56

MSFT26.4

MSFT28.1

MSFT30.2

MSFT25.6

MSFT29.2

MSFT32.1

MSFT{27.56, 26.4, 28.1, 30.2, 25.6}

Buffer(5,1)

MSFT27.56

MSFT26.4

MSFT28.1

MSFT30.2

MSFT25.6

MSFT29.2

MSFT32.1

Example: buffer in time

• Demo

Interoperability with the TPL

• ToObservable()

• ToTask()

Example: asynchronous spellchecker

Demo

Rx on other platforms

• RxJs– Autocomplete, drag&drop, animation, etc.

• RxJava– Used for server-side concurrency (heavy requests

instead of multiple light requests)– “functional reactive is superior to futures and

callbacks”– using IObservable<T> allows choosing asynchronous

or synchronous execution & I/O at runtime

• RxRb• RxPy

Links

• http://www.introtorx.com/

• http://blogs.msdn.com/b/rxteam/

• Beginner's Guide to the Reactive Extensions

• http://www.reactivemanifesto.org/

“GitHub for Windows uses the Reactive Extensions for almost everything it does, including network requests, UI events, managing child processes […] while still having 100% deterministic, reliable unit tests. The desktop developers at GitHub loved Rx so much, that the Mac team created their own version of Rx and ReactiveUI, called ReactiveCocoa, and are now using it on the Mac to obtain similar benefits.”

Paul Betts, GitHub