real-world applications of the reactive extensions

26
Real-world applications of Reactive Extensions for .NET (Rx) Jonas Chapuis, Ph.D., Nexthink S.A.

Upload: jonas-chapuis

Post on 10-May-2015

1.600 views

Category:

Technology


1 download

DESCRIPTION

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

TRANSCRIPT

Page 1: Real-world applications of the Reactive Extensions

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

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

Page 2: Real-world applications of the Reactive Extensions

About me

[email protected]

www.nexthink.com/careers

Page 3: Real-world applications of the Reactive Extensions

“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

Page 4: Real-world applications of the Reactive Extensions

Exercise

Versus:JAY

JANE

MARRY

HARRY

readablecompactsafe

Page 5: Real-world applications of the Reactive Extensions

IEnumerable & IEnumerator

Page 6: Real-world applications of the Reactive Extensions

Oh my…

TimeoutException!

Page 7: Real-world applications of the Reactive Extensions

Ok, how about this:

nice

Page 8: Real-world applications of the Reactive Extensions

IObservable & IObserver

Page 9: Real-world applications of the Reactive Extensions

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

Page 10: Real-world applications of the Reactive Extensions

Where does Rx fit?

Rx shines for– Declarative syntax & compositionality

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

– Error handling

– Cancellation

– Testing

Page 11: Real-world applications of the Reactive Extensions

Example: search box

• Demo

Page 12: Real-world applications of the Reactive Extensions

search

Search pipeline

input

react

search

reactive

SelectMany

Page 13: Real-world applications of the Reactive Extensions

Input pipeline

input

r re rea reac reaci reac reactive

Throttle 200ms

Where length > 2

DistinctUntilChanged

Page 14: Real-world applications of the Reactive Extensions

search

search

Race condition

input

react reactive

SelectMany

Page 15: Real-world applications of the Reactive Extensions

TakeUntil

input

react reactive

SelectMany

search

TakeUntil(input)

search

TakeUntil(input)

Page 16: Real-world applications of the Reactive Extensions

Timeout

input

react

SelectMany

search

Timeout

Page 17: Real-world applications of the Reactive Extensions

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

Page 18: Real-world applications of the Reactive Extensions

Example: stock quotes stream

• Demo

Page 19: Real-world applications of the Reactive Extensions

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}

Page 20: Real-world applications of the Reactive Extensions

Buffer(5,1)

MSFT27.56

MSFT26.4

MSFT28.1

MSFT30.2

MSFT25.6

MSFT29.2

MSFT32.1

Page 21: Real-world applications of the Reactive Extensions

Example: buffer in time

• Demo

Page 22: Real-world applications of the Reactive Extensions

Interoperability with the TPL

• ToObservable()

• ToTask()

Page 23: Real-world applications of the Reactive Extensions

Example: asynchronous spellchecker

Demo

Page 24: Real-world applications of the Reactive Extensions

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

Page 25: Real-world applications of the Reactive Extensions

Links

• http://www.introtorx.com/

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

• Beginner's Guide to the Reactive Extensions

• http://www.reactivemanifesto.org/

Page 26: Real-world applications of the Reactive Extensions

“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