An Introduction to the Reactive Extensions - Flatlander
Download
Report
Transcript An Introduction to the Reactive Extensions - Flatlander
An Introduction to the
Reactive Extensions
Ivan Towlson
Mindscape
Reintroducing LINQ
Composable operators for working with
sets of data
IEnumerable:
the little interface that could
IQueryable:
the little interface that takes
twelve years to implement
Extends IEnumerable to support
translation of LINQ queries for processing
by another engine
Pulling and pushing
IEnumerable is a pull model
Unpredictable streams are better handled
by a push model
Introducing IObservable
interface IEnumerable<T> {
IEnumerator<T> GetEnumerator();
}
interface IObservable<T> {
IDisposable Subscribe(IObserver<T>);
}
interface IEnumerator<T> {
bool MoveNext();
T Current;
}
interface IObservable<T> {
void OnNext(T);
void OnError(Exception);
void OnCompleted();
}
Introducing the
Reactive Extensions
An implementation of the LINQ standard
query operators (and more) for the
IObservable interface
Translatable
(expression trees)
IQueryable
Fixed
(MSIL)
How?
IEnumerable
IObservable
LINQ to Objects
“LINQ to Events”
e.g LINQ to SQL
Pull
(interactive)
What?
Push
(reactive)
Introducing the
Reactive Extensions
A unified, declarative, composable way of
working with asynchronous sources
Builds on existing sources of asynchrony,
rather than replacing them
Introducing the
Reactive Extensions
Like LINQ, Rx doesn’t do anything you
couldn’t do in your own code, but it wraps
up a lot of boilerplate and ad hoc code
into convenient, composable operators
Where does asynchronous
data come from?
Events as asynchronous data
Revisiting the asynchronous
method pair pattern
Primitive constructors and
generators
Not useful in themselves, but useful for
composition
Observable.Create: explicitly spelling out
the event sequence (like an iterator)
The pulls push
and the pushers pull
with apologies to P J Harvey
Fixed
(MSIL)
IQueryable
e.g LINQ to SQL
AsXxx
Translatable
(expression trees)
How?
IEnumerable ToXxx IObservable
LINQ to Objects
Pull
(interactive)
“LINQ to Events”
What?
Push
(reactive)
What can we do with
asynchronous data
once we’ve got a source?
LINQ operators
Where
Select / SelectMany
Count / Any / All
and all the rest
And a whole lot more besides
Scan
Buffer (with time and count options)
TakeUntil
Merge / Amb
And a whole lot more besides
Sample
Throttle / Timeout
Timestamp / TimeInterval
DistinctUntilChanged
What about timing?
What about threading?
Translatable
(expression trees)
IQueryable
Fixed
(MSIL)
How?
IEnumerable
IObservable
LINQ to Objects
“LINQ to Events”
When?
e.g LINQ to SQL
Pull
(interactive)
What?
Push
(reactive)
Schedulers
Immediate
Thread pool, task pool
WPF or WinForms UI thread
Custom
Or we could just do it
right here on the UI thread
ObserveOn
SubscribeOn
A brief interlude for the
obligatory eggheadtude
Sorry, Visual Basic fans, but
the doors are now locked
Fixed homoiconic Translatable
(MSIL)
(expression trees)
How?
IQueryable
e.g LINQ to SQL
IEnumerable
IObservable
LINQ to Objects
“LINQ to Events”
Pull
(interactive)
dual
What?
Push
(reactive)
Orthogonality fail?
Translatable
(expression trees)
IQueryable
Fixed
(MSIL)
How?
IEnumerable
IObservable
LINQ to Objects
“LINQ to Events”
e.g LINQ to SQL
Pull
(interactive)
What?
Push
(reactive)
Introducing IQbservable
Also known as IQueryableObservable or
IShouldntChooseNamesWhileStoned
Introducing IQbservable
Allows queries to be translated for
processing by external push sources
Like IQueryable, filtering, aggregation, etc.
can be performed at the source
How?
Translatable
(expression trees)
IQueryable
IQbservable
e.g LINQ to SQL
e.g. LINQ to WMI
Fixed
(MSIL)
LINQ to *
IEnumerable
IObservable
LINQ to Objects
“LINQ to Events”
Pull
(interactive)
What?
Push
(reactive)
Availability
.NET, Silverlight: download from MSDN
Windows Phone 7: included in ROM but
can download updated version
JavaScript: download from MSDN
Resources
MSDN DevLabs – Projects > Rx
PDC talk – FT10, Bart de Smet
http://blogs.msdn.com/b/rxteam/
Rx Design Guidelines document
Thanks!
Ivan Towlson
[email protected] | [email protected]
http://hestia.typepad.com/flatlander/