Reactive applications are designed to handle asynchronous events in a way that maximizes responsiveness, resiliency and elasticity. Reactive Extensions (Rx) is a library created by Microsoft that abstracts away the sources of events and provides tools to handle them in a reactive way. With Rx, filtering events, composing event sources, transforming events and dealing with errors all become much simpler than with traditional tools. Reactive Extensions requires a mind change, thinking about your application as a message hub that knows how to react to messages.
2. • Software Architect, Consultant and Instructor
• Software Engineering Lecturer @ Ruppin
Academic Center
@tamir_dresher
tamirdr@codevalue.net
http://www.TamirDresher.com.
3. Your headache relief pill to Asynchronous and
Event based applications
Async
Push
Triggers
Events
Reactive Extensions
4. Reactive Programming concentrates
on the propagation of changes and
their effects
simply put, how to react to
changes and create dataflows
that depend on them
Reactive Programming
19. Filtering Projection Partitioning Joins Grouping Set
Element Generation Quantifiers Aggregation Error HandlingTime and
Concurrency
Where
OfType
Select
SelectMany
Materialize
Skip
Take
TakeUntil
CombineLatest
Concat
join
GroupBy
GroupByUntil
Buffer
Distinct
DistinctUntilChanged
Timeout
TimeInterval
ElementAt
First
Single
Range
Repeat
Defer
All
Any
Contains
Sum
Average
Scan
Catch
OnErrorResumeNext
Using
20. Operator2 Creates an IObservable<T3>
IObservable parameters
Operator
1
Subscribe( )
observer
observable
… Operator
N
Operator1
Creates an IObservable<T2>
IObservable<T>
Original source
observer
Subscribe
Composability
23. 1. At least 3 characters
2. Don’t overflow server (0.5 sec delay)
3. Don’t send the same string again
4. Discard results if another search was
requested
Reactive Search - Rules
24.
25. Var textChanged =
Observable.FromEventPattern(txt, “TextChanged”)
.Select(_=>txt.Text);
textChanged
.Where(text=>text.Length>3)
.Throttle(TimeSpan.FromSeconds(0.5))
.DistinctUntilChanged()
.SelectMany(text => SearchAsync(text))
.Switch()
.Subscribe(/*handle the results*/);
26. var $input = $('#input'),
$results = $('#results');
Rx.Observable.fromEvent($input, 'keyup')
.map(e => e.target.value)
.filter(text => text.length > 3)
.throttle(500 /* ms */)
.distinctUntilChanged()
.flatMapLatest(searchWikipedia)
.subscribe(data => {
var res = data[1];
$results.empty();
$.each(res, (_, value) => $('<li>' + value + '</li>').appendTo($results));
}, error => {
/* handle any errors */
$results.empty();
$('<li>Error: ' + error + '</li>').appendTo($results);
});
30. //
// Runs a timer on the default scheduler
//
IObservable TimeSpan
//
// Every operator that introduces concurrency
// has an overload with an IScheduler
//
IObservable T TimeSpan
IScheduler scheduler
Parameterizing Concurrency
32. //
// runs the observer callbacks on the specified
// scheduler.
//
IObservable T T IScheduler
//
// runs the observer subscription and unsubsciption on
// the specified scheduler.
//
IObservable T T IScheduler
Changing Execution Context