Reactive programming
-
Upload
nick-hodge -
Category
Software
-
view
1.058 -
download
0
description
Transcript of Reactive programming
Reactive ProgrammingNick Hodge
Developer, [email protected]
http://nickhodge.com/ https://gist.github.com/nickhodge for codesnippets
Who is Nick Hodge?
• 28 years in IT, mostly as non-Developer• Apple, Adobe, Microsoft• Many small-scale, rapidly built applications• Open source contributor• Functional Reactive Programming Fan
Group Experiment: Data-at-rest vs. Data-in-motion• LINQ to IEnumerable (or IQueryable)• “data at rest”• yield• What happens if data changes in midst of foreach() ?
• Reactive programming (IObservable)• “data in motion”• Data (or events) are pushed at you
Reactive Programmingdemo required
Reactive Programming vs. TPL
• Task Parallel Library (TPL) http://msdn.microsoft.com/en-us/concurrency • PLINQ
The core of LINQis the sequence
A sequence is just some stuff in a particular order
What about Rx?Let’s talk about events
Limitations of .NET Events
exchange.StockTick += (sender, args) =>{ if (args.Quote.Symbol == “MSFT”) { // Imperative code }};
exchange.StockTick -= /* what goes here? */;
Can’t pass around Hidden data source
Hard resource maintenance
Lack of composition
Observable Sequences to the Rescue
IObservable<Quote> stockQuotes = …;
var msft = stockQuotes
.Where(quote => quote.Symbol == “MSFT”);
var subscription = msft.Subscribe(quote => /* … */);
subscription.Dispose();
Objects can be passed Source of Quotes
Easy resource maintenance
Can define query operators
Are .NET Events Obsolete?• .NET Events
• Code centric
• Design-time experience
• Not first class
• Non-compositional
• Lightweight
• Rigid execution model (IL)
• Observables
• Data centric
• No design-time experience
• First class objects
• Rich composition
• Slightly more cost
• Translatable with expression trees
The Event Processing Landscape
Socialmedia
Stock tickers
RSS feeds
GPS
Server managementUI e
vents
Reactive Extensions Architecture
Concurrency
IScheduler TimeThreads Cloud Dispatchers
Event Streams
IObservable<T>
IObserver<T>ISubject<T>
LINQ to Events
from quote in stockwhere quote.Symbol == “MSFT”select quote.ValueProjectionFi
lterin
g
AggregatingG
roup
ing
Joins
Windowing
Sharing
Sampling
Throttling Timeout
Merging
Recovery
Event Streams
• Towards a unified programming model• Producers are observable sequences
• .NET events, WinRT events, sensor APIs, APM methods, tasks, etc.• Consumers are observers
• Hooking up “continuations” or handlers
Observable
Subscribe
Observer
Essential Interfacesnamespace System
{
public interface IObservable<out T>
{
IDisposable Subscribe(IObserver<T> observer);
}
public interface IObserver<in T>
{
void OnNext(T value);
void OnError(Exception error);
void OnCompleted();
}
}
Observable Sources
• Single items / empty Observable.Return()• Lists and arrays• UI Events• Async methods• Subjects (demo)• Property Changes
Cold / Hot
• Hot : example MouseMove• Cold: when you subscribe, something happens (it waits)
UI Example, Windows 8 Universal App
This Presentation’s Reference Presentations• Bart De Smet “Curing Your Event Processing Blues with Reactive
Extensions (Rx)” TechEd Europe 2012 http://channel9.msdn.com/events/TechEd/Europe/2012/DEV413
• Paul Betts “Introduction to Reactive Extensions” Norwegian Developer’s Conference 2012 http://vimeo.com/43659034
• Brendan Forster “Reactive UI – Turning MVVM up to 11” http://vimeo.com/97329155
Further References, Links
• https://rx.codeplex.com/ and https://github.com/Reactive-Extensions/Rx.NET as the starting point• RxJS (and other languages such as Ruby, C++, Python, Java, ObjC)
https://github.com/Reactive-Extensions/RxJS • http://www.introtorx.com/ • 101 Rx Examples
http://rxwiki.wikidot.com/101samples • http://amzn.to/programming-rx “Programming Reactive Extensions
and LINQ”
Further Research, Future
• “Scalable Information Stream Processing by Bing in Support of Cortana Scenarios” http://channel9.msdn.com/posts/Scalable-Information-Stream-Processing-by-Bing-in-Support-of-Cortana-Scenarios • Actor model, “Microsoft Research project Orleans simplify
development of scalable cloud services” http://channel9.msdn.com/Shows/Cloud+Cover/Episode-142-Microsoft-Research-project-Orleans-simplify-development-of-scalable-cloud-services