Fun With Reactive Extensions

16
Member of the stack since 2009

Transcript of Fun With Reactive Extensions

Page 1: Fun With Reactive Extensions

Member of the stack since 2009

Page 2: Fun With Reactive Extensions

Why Reactive Programming?

Page 3: Fun With Reactive Extensions

Events vs Rx: Why Rx is a better model.

• In C#, events have a curious interface. Some find the += and -= operators an unnatural way to register a callback

• Events are difficult to compose• Events do not offer the ability to be easily queried over time• Events are a common cause of accidental memory leaks• Events do not have a standard pattern for signaling completion• Events provide almost no help for concurrency or multithreaded applications.

For instance, raising an event on a separate thread requires you to do all of the plumbing

Events vs Rx

Page 4: Fun With Reactive Extensions

Events vs Rx

Page 5: Fun With Reactive Extensions

Events vs Rx

Page 6: Fun With Reactive Extensions

Events vs Rx

Page 7: Fun With Reactive Extensions

Creating an Observable

• Custom using a Subject<T>

• Based on Event (FromEventPattern)

• Several other options:

– ToObservable()

– Observable.Interval(TimeSpan ts)

– Observable.Create(…)

– Many more…

Page 8: Fun With Reactive Extensions

Demo - Query over Time

ApplicationLogger.Instance.Write(…)

event EventHandler<EvArgs> LogEntryWritten

Question:

How to get a signal when a certain log entry occurs more than (n) times in a given time period?

Page 9: Fun With Reactive Extensions

Demo – Composition

FileSystemWatcher

- Deleted event

- Created event

Question:

How would I scan for creates and deletes that have occurred every (n) minutes?

Page 10: Fun With Reactive Extensions

Demo – Threading

SomeEvent(Object sender, SomeEventArgs e){

PerformWorkTaking10Seconds (e)UpdateUi(e)

}_______________________________________________________________

OnSomeEvent(){

if(SomeEvent != null)SomeEvent(this, new SomeEventArgs());

}

With (n) subscribers, how long does it take to complete OnSomeEvent()?

Page 11: Fun With Reactive Extensions

Other Goodies

• React to UI (demo)

• Timeout (demo)

• Lots of Operators

– Where()

– Retry()

– Amb()

– OnErrorResumeNext()

– …

Page 12: Fun With Reactive Extensions

Usages in PlanCare 2

• Buffering Web Api Request logentries

• Handling Notifications (Buffer, Transform)

• As Event Substitute in Business Layer

Page 13: Fun With Reactive Extensions

And more: PHP, Pearl, Haskeel

Page 14: Fun With Reactive Extensions

http://nflx.it/1yR60gV

http://bit.ly/1LOtiH3

http://bit.ly/1E2WpqT

http://bit.ly/1E3dscb

Page 15: Fun With Reactive Extensions

What’s Next?

IQbservable<T> - Linq to ServicesSubscribe to changes somewhere else

Page 16: Fun With Reactive Extensions

Get started …

• http://www.introtorx.com/

• http://rxwiki.wikidot.com/101samples

• http://t.co/gB7SPIcZu3 (Must See!)

… play … and remember:Often it is not the technique that let's us down, but our implementation of it.