Reactive Extensions Ye olde introduction and walk-through, with plenty o’ code.

26
Reactive Extensions Ye olde introduction and walk- through, with plenty o’ code

Transcript of Reactive Extensions Ye olde introduction and walk-through, with plenty o’ code.

Page 1: Reactive Extensions Ye olde introduction and walk-through, with plenty o’ code.

Reactive Extensions

Ye olde introduction and walk-through,

with plenty o’ code

Page 2: Reactive Extensions Ye olde introduction and walk-through, with plenty o’ code.

OK, what is it?

● A library to compose asynchronous and event-based programs using observable collections and LINQ-style query operators.

● Keywords: Compose, LINQ, Asynchronous,Compose, LINQ, Asynchronous

Compose, LINQ, Asynchronous

Compose, LINQ, Asynchronous

Compose, LINQ, Asynchronous

Page 3: Reactive Extensions Ye olde introduction and walk-through, with plenty o’ code.

Reactive what?

● Wikipedia: a programming paradigm oriented around data flows and the propagation of change

● Interactive: a = b + c

Meaning: a will store the current values of b and c (added), but future updates to b and c will not affect a

● Reactive:

Meaning: a will be updated when either b or c (or both) changes in the future

Page 4: Reactive Extensions Ye olde introduction and walk-through, with plenty o’ code.

Reactive vs Interactive, part II(push vs pull)

● Interactive (pull, compare with polling)

● Reactive (push, compare with notifications)

● Key concept: create a query now, receive updates in the future.

Page 5: Reactive Extensions Ye olde introduction and walk-through, with plenty o’ code.

The library

● Created by Microsoft● Built on Parallel extensions library (PFX,

meaning fully multi-threaded from day one)● Open source as of November 2012● Based on functional programming● Runs on Windows Forms, WPF, Silverlight,

WP7, WP8, JavaScript, ASP.NET● Is written using extension methods

Page 6: Reactive Extensions Ye olde introduction and walk-through, with plenty o’ code.

Why should I use it?

● Today’s applications are complex

● Managing applications with complex message flows, threads and states tend to get real messy real soon

● An asynchronous and responsive UI is a necessity for the demanding users (and apps) of today

● Using a library that handles threading and synchronization for you leads to using fewer locks (and having fewer dead-locks or bottlenecks)

● Allows you to orchestrate message flows in an easy manner

Page 7: Reactive Extensions Ye olde introduction and walk-through, with plenty o’ code.

Why should I use it?

Events are high-maintenance

Enumerating collections is a blocking operation

Observables are asynchronous by nature,

Observables introduce the notion of time

Page 8: Reactive Extensions Ye olde introduction and walk-through, with plenty o’ code.

The assemblies

Page 9: Reactive Extensions Ye olde introduction and walk-through, with plenty o’ code.

What you need to know(show me the code)

● The Interfaces

● The ContractOnNext* (OnError | OnCompleted)?

Publish next value

An Error occured

And we’re done

Page 10: Reactive Extensions Ye olde introduction and walk-through, with plenty o’ code.

Migrating into the Observable

● Strategy: move tricky problem into observable domain, solve problem easily, move back to regular domain

● Methods exist for converting existing objects to observable objects

Page 11: Reactive Extensions Ye olde introduction and walk-through, with plenty o’ code.

Creating Observables( - what is it really?)

● Think 'streams' (of events, data, etc)● Do not implement your own IObservable, use

available combinators, or the factory methods:

Page 12: Reactive Extensions Ye olde introduction and walk-through, with plenty o’ code.

Code Demo – Creating ObservablesFrom raw dataFrom eventsFrom async patternsCustom observables

Page 13: Reactive Extensions Ye olde introduction and walk-through, with plenty o’ code.

Error handling(you be illin’)

● Dissecting the OnError lambda● Error handling strategy

● Keep it local

Page 14: Reactive Extensions Ye olde introduction and walk-through, with plenty o’ code.

Code Demo – Error Handling

Page 15: Reactive Extensions Ye olde introduction and walk-through, with plenty o’ code.

Schedulers(time is of the essence)

● Calls are asynchronous, so…● Where do observables run?● Observable.Return(42) OnNext(42) OnCompleted

Page 16: Reactive Extensions Ye olde introduction and walk-through, with plenty o’ code.

Code Demo – Schedulers

Page 17: Reactive Extensions Ye olde introduction and walk-through, with plenty o’ code.

Unit Testing(you better check yourself, before you wreck yourself)

● TestScheduler - allows testing of asynchronous code in real-time

● Helps you learn and understand the inner workings of the Rx libraries.

● This is what’s available:● NewThreadScheduler

● TaskPoolScheduler

● DispatcherScheduler

● ImmediateScheduler

● ThreadPoolScheduler

Page 18: Reactive Extensions Ye olde introduction and walk-through, with plenty o’ code.

Code Demo – Testing Schedulers

Page 19: Reactive Extensions Ye olde introduction and walk-through, with plenty o’ code.

Resource Disposal(I must clean)

● In Essence: how and when do you Unsubscribe?

– OnCompleted AutoDispose

● What happens if you don’t?● Best practices

Page 20: Reactive Extensions Ye olde introduction and walk-through, with plenty o’ code.

Code Demo – Unsubscribe

Page 21: Reactive Extensions Ye olde introduction and walk-through, with plenty o’ code.

Hot vs Cold

● There are two types of streams:● Hot – producing values all the time, regardless of

subscribers– Examples: mouse events, streaming data

● Cold – producing values only when someone is subscribing– Examples: web requests

● Important to know the difference, especially when it comes to side effects

Page 22: Reactive Extensions Ye olde introduction and walk-through, with plenty o’ code.

Code Demo – Sharing resources• hot and cold• subjects

Page 23: Reactive Extensions Ye olde introduction and walk-through, with plenty o’ code.

Code Demo• Search Twitter• Common Operators

Page 24: Reactive Extensions Ye olde introduction and walk-through, with plenty o’ code.

Why do I recommend Rx?

● Allows me to write modular, reusable code, which is composable

● Allows me to orchestrate message flows ● Relieves me of a lot of state management● Allows me to write fully asynchronous UI’s from

day one.● Provides full unit testing of timeouts, delays, etc● Is feature rich => less work for me

Page 25: Reactive Extensions Ye olde introduction and walk-through, with plenty o’ code.

Further Reading

● introtorx.com

● http://social.msdn.microsoft.com/Forums/en-US/rx/threads

● http://rxx.codeplex.com/

● http://www.reactiveui.net/

Page 26: Reactive Extensions Ye olde introduction and walk-through, with plenty o’ code.

The Endwww.capsor.se

@capsorab