reactive extensions ye olde introduction and walk-through, with plenty o’ code
Post on 29-Dec-2015
219 Views
Preview:
TRANSCRIPT
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
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
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.
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
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
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
The assemblies
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
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
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:
Code Demo – Creating ObservablesFrom raw dataFrom eventsFrom async patternsCustom observables
Error handling(you be illin’)
● Dissecting the OnError lambda● Error handling strategy
● Keep it local
Code Demo – Error Handling
Schedulers(time is of the essence)
● Calls are asynchronous, so…● Where do observables run?● Observable.Return(42) OnNext(42) OnCompleted
Code Demo – Schedulers
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
Code Demo – Testing Schedulers
Resource Disposal(I must clean)
● In Essence: how and when do you Unsubscribe?
– OnCompleted AutoDispose
● What happens if you don’t?● Best practices
Code Demo – Unsubscribe
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
Code Demo – Sharing resources• hot and cold• subjects
Code Demo• Search Twitter• Common Operators
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
Further Reading
● introtorx.com
● http://social.msdn.microsoft.com/Forums/en-US/rx/threads
● http://rxx.codeplex.com/
● http://www.reactiveui.net/
top related