SlideShare a Scribd company logo
Rxjs
Christoffer Noring
Google Developer Expert
@chris_noring
Why Rxjs?
We want to deal with async in a “synchronous looking way”
We want something better than promises
We want one paradigm for async to rule them all
nce upon a time in async land
There were callbacks
Callbacks turned into callback hell
Promises to the rescue
service
.getData()
.then(getMoreData)
.then(getEvenMore)
.then(andSomeMore)
Looks great right?
But promises were flawed
No cancellation
eal with other async concepts like mouse positions, clicks, use
No rich composition
And brexit happened
Cumbersome to retry
Only returns one value
Help us Observables
You’re my only hope
What is an observable
Observable is just a function
that takes an observer and returns a function
Observer: an object with next, error, complete methods
Rx.Observable.create((observer) => {
observer.next(1);
observer.error(‘error’);
observer.complete();
})
1 2 3 4 5 6 7
stream of value over time
Promise
vs Array
vs Observable
list
.map( x = > x.prop )
.filter( x => x > 2 )
.take( 2 )
Array
list
.map( x = > x.prop )
.filter( x => x > 2 )
.take( 2 )
.subscribe(
x => console.log(x),
err => console.log(err) )
Observable
Promise
service.get()
.then( x => console.log(x) )
.catch( err => console.log(err) ) but can also
- Cancelled
- Retried
Array like,
handles async
So pretty much linq with
async
Everything is a stream(s)
and we can bend it to our will
Rx.Observable.create(
fnValue,,
fnError,,
fnCompleted
)
Observable signature
var stream = Rx.Observable.create((observer) =>{
})})
Emits
stream
.subscribe(
(data) => { console.log( data ); }
)
1
next()
observer.next(1);
2
next()
observer.next(2);
3
next()
observer.next(3);
var stream = Rx.Observable.create((observer) =>{
})
stream
.subscribe(
(data) => { console.log( data ); }
(err) => { console.log(err); }
)
Emits 1
next()
observer.next(1);
error message
error()
observer.error(‘something went wrong’)
var stream = Rx.Observable.create((observer) =>{
})})
stream
.subscribe(
(data) => { console.log( data ); }
(err) => { console.log(err) },
() => { console.log(‘completed’) }
)
Emits 1
next()
observer.next(1);
complete()
observer.complete();
Disposing
cleaning up after us
var homemadeStream = Rx.Observable.create((observer) => {
var i=0;
});
var subscription2 = homemadeStream.subscribe((val) => {
console.log('Homemade val',val);
});
setTimeout(() => {
console.log('Cancelling homemadeStream');
subscription2.unsubscribe();
}, 1500); Calling dispose
Produce values
till someone calls unsubscribe
var handle = setInterval(() => {
observer.next( i++ );
}, 500);
Define whats to happen
on unsubscribe
return function(){
console.log('Disposing timeout');
clearTimeout( handle );
}
You will always create
an observable from something
Rx.Observable.fromArray([ 1,2,3,4 ])
Rx.Observable.fromEvent(element, ‘event’);
Rx.Observable.fromArray(eventEmitter, ‘data’, function(){})
Rx.Observable.fromNodeCallback(fs.createFile)
Rx.Observable.fromCallback(obj.callback)
Rx.Observable.fromPromise(promise)
Rx.Observable.fromIterable(function *() { yield 20 })
Rx.Observable.range(1,3)
Rx.Observable.interval(miliseconds)
Wrap an observable
next()
error()
complete()
var stream = Rx.Observable.create((observer) => {
var request = new XMLHttpRequest();
request.open( ‘GET’, ‘url’ );
request.onload =() =>{
if(request.status === 200) {
} else {
}
}
request.onerror = () => { }
request.send();
})
stream.subscribe(
)
observer.next( request.response );
(result) => { console.log( result ); }
Get our data
observer.complete();
() => { console.log(‘completed’); }
No more data, close stream
observer.error( new Error( request.statusText ) )
(err) => { console.log(err) },
observer.error( new Error(‘unknown error’) );
Error
Error
Hot vs Cold Observable
Cold Observable
recorded tv show
Hot observable
Live streaming
eg World Cup Final
Observables are cold by default,
unless you make them hot
var stream = Rx.Observable.interval(1000);
stream.subscribe(
(val) => { console.log('Subscriber 1', val);}
);
stream.subscribe(
(val) => { console.log('Subscriber 2', val); }
);
var stream = Rx.Observable
.interval(1000)
.publish();
stream.subscribe((val) => {
console.log('Subscriber 1', val);
});
setTimeout(function() {
stream.connect();
}, 2000);
setTimeout(() => {
stream.subscribe((val) => {
console.log('Value', val);
});
}, 4000);
Subscriber 2 Values 0 1 2 3 4
Subscriber 1 Values 0 1 2 3 4
0 1 2 3 4 5 6
3 4 5 6
You can create an observable
from almost any async concept
Operators however gives it
its power
Remember:
But:
Operators
makes your code look like linq
120+ operators Rxjs 4
60+ Rxjs 5
Combination
Conditional
Multicasting
Filtering
Transformation
Utility
Categories
in production
Marble diagram
how does that operator work
Operator
Most operators are covered at rxmarbles.com
Stream 1 2 3
Other stream 4 5
Resulting stream 1 2 3 4 5
Operator example
var stream = Rx.Observable.of(1,2,3,4,5);
stream
stream.subscribe((data) => { console.log(‘data’); })
Operators :
map()
filter()
3
Emits
6
.map((val) => {
return val + 1;
})
changes the value
.filter((val) => {
return val % 3 === 0;
})
filters out values
Operators
utility
Do
var stream = Rx.Observable.of(1,2,3,4,5);
var subscription = stream
.filter(function(val){
return val % 2 === 0;
});
subscription.subscribe(function(val){
console.log('Val',val);
})
Echos every value
without changing it,
used for logging
.do((val) => {
console.log('Current val', val);
})
Current val 1
Current val 2
Current val 3
Current val 4
Current val 5
Subscribe:
2
4
Delay
Delay,
the whole sequence
stream
.subscribe( (val) => {
var newTime = new Date().getTime();
console.log('Delayed', val + " " + (newTime - time));
})
Rx.Observable.merge(
).subscribe((val) => {
var newTime = new Date().getTime();
console.log('Merged Delay', val + " " + (newTime - time));
})
….. 1 second
1
2
3
….. 1 second
Marco
….. 2 second
Polo
.delay(1000)
Rx.Observable.of('Marco').delay(1000),
Rx.Observable.of('Polo').delay(2000)
Operators
combination
combineLatest
var stream = Rx.Observable.fromArray([1,2,3,4]);
var stream2 = Rx.Observable.fromArray([5,6,7,8]);
var combineLatest = Rx.Observable
combineLatest.subscribe( function(val) {
console.log( 'combine ', val )
})
[1,5], [2,6], [3,7], [4,8]
.combineLatest(
stream,
stream2
);
Operators
filtering
debounce
var debounceTime = Rx.Observable
.fromEvent(button,'click')
debounceTime.subscribe( function(){
console.log('mouse pressed');
})
waits x ms and
returns latest emitted
Ignores all generated
mouse click events
for 2 seconds.debounce(2000);
Clicking save button
2secclick click click click click
save()
takeUntil
.subscribe(
(err) => {},
)
var mouseUp = Rx.Observable.fromEvent(button,'mouseup');
Break condition
.takeUntil( mouseUp )
() =>{ console.log('condition fulfilled'); }
Generate numbers
var mousePressedTimer = Rx.Observable.interval(1000);
triggers every secondmousePressedTimer
(val) =>{ console.log('mouse up has NOT happened yet’,val); },
var throttle = Rx.Observable
.interval( 1000 )
.throttle( 1000 );
throttle.subscribe(
(val) => { console.log('Throttle', val );}
);
throttle
Delay every value
by x milliseconds
1 2 3 4
1 2
Operators
transformation
buffer
let numbers = Rx.Observable.interval(1000);
let bufferBy = Rx.Observable.fromEvent(document,'click');
let buffered = numbers.buffer( bufferBy );
buffered.subscribe(
);
Numbers are generated to a buffer
until condition is met,
then the buffer content is emitted
Generate value to buffer
Release all values
(values) => { console.log(‘Buffered',values);}
switchMap
Switch map,
complete something based on a condition
breakCondition = Rx.Observable.fromEvent(document,'click');
breakCondition.switchMap((val) => {
return Rx.Observable.interval(3000).mapTo(‘Do this');
})
breakCondition.subscribe((val) => {
console.log('Switch map', val);
})
Intended action is completed/restarted
by ‘breakCondition’
etc..
Do this
Do this
Do this
Do this
Do this
click
click
source.subscribe((data) => {
console.log( data );
})
flatMap
let source = Rx.DOM.getJSON( 'data2.json' )
return Rx.Observable.fromArray( data ).map((row) => {
return row.props.name;
});
return observable
.flatMap((data) => {
} );
We get an array response that we want to emit row by row
We use flatMap instead of map because :
We want to flatten our list to one stream
flatMap explained
when you create a list of observables flatMap flattens that list s
Great when changing from one type of stream to another
Without it you would have to listen to every single substream, w
eve
nt
eve
nt
eve
nt
eve
nt
ajax ajax ajax ajax
json json json json
flatMap
map
Problem : Autocomplete
Listen for keyboard presses
Filter so we only do server trip after x number of
chars are entered
Do ajax call based on filtered input
Cash responses,
don’t do unnecessary calls to http server
The procedural approach
let input = $(‘#input’);
input.bind(‘keyup’,() = >{
let val = input.val()
if(val.length >= 3 ) {
if( isCached( val ) ) { buildList( getFromCache(val) ); return; }
doAjax( val ).then( (response) => {
buildList( response.json() )
storeInCache( val, response.json() )
});
}
})
fetch if x characters long
return if cached
do ajax
Ok solution but NOT so fluent
We need 3 methods to deal with cache
The observable approach
Stream modeling
key key key key key key
FILTER
AJAX CALL
jso
n
jso
n
MAP
key key key key key key key
respons
e
respons
e
flatmapExample = Rx.Observable.fromEvent(input,'keyup')
flatmapExample.subscribe(
(result) =>{ console.log('Flatmap', result); buildList( result ) }
)
more fluent
Transform event to char.map((ev) => {
return ev.target.value;
})
Wait until we have 3 chars
.filter(function(text){
return text.length >=3;
})
Only perform search if this ‘search’ is unique.distinctUntilChanged()
Excellent to use when
coming from
one stream to another
.switchMap((val) => {
return Rx.DOM.getJSON( 'data3.json' );
})
Error handling
when streams fail
Error handling
kills the stream
var errStream = Rx.Observable.throw('Error');
var stream = Rx.Observable.create((observer) => {
observer.next(1);
})
var merged = Rx.Observable
.merge( errStream, stream )
merged.subscribe(
(val) => { console.log('Val', val);},
(err) => { console.log('Err', err);},
() => { console.log('completed');
})
Captured here but sequence
interrupted,
completed NOT reached
Error handling
better, but can be improved
var errStream = Rx.Observable.throw('Error');
var stream = Rx.Observable.create((observer) => {
observer.onNext(1);
})
var merged = Rx.Observable
.merge( errStream, stream )
merged.subscribe(
(val) => { console.log('Val', val); },
(err) => { console.log('Err', err); },
)
stream not processed though
can we improve it?
Completed reach, stream never
emits its values though
() => { console.log(‘completed'); }
.catch((err) => {
return Rx.Observable.of(err);
});
Error handling
wrap error stream before merge
var errStream = Rx.Observable
.throw('AAA thump..')
.catch(function(err){
return Rx.Observable.of(err);
});
merged.subscribe(
(val) => { console.log('Val', val); },
(err) => { console.log('Err', err); },
)
() => { console.log(‘completed'); }
all values are emitted
Error
ignoring
You have several sources and two terminates
You want the other normal source to work
var errStream = Rx.Observable
.throw('AAA thump..');
var errStreamWithFlattenedData =
Rx.Observable
.interval(500)
.take(3)
.flatMap((val) => {
if( val === 1 ) {
return Rx.Observable.throw('crashing');
}
else {
return Rx.Observable.return(val);
}
})
var normalStream = Rx.Observable.return('anything');
var handledErrorStream = Rx.Observable
.onErrorResumeNext(
errStream,
normalStream,
errStreamWithFlattenedData );
handledErrorStream.subscribe(
(val) =>{ console.log('error stream ignored', val);},
(err) => { console.log("error stream ignored, error",err);},
() => { console.log("completion of error stream ignored”);}
)
Error handling
retry
let stream = Rx.Observable.interval(1000)
.take(6);
.map((n) => {
if(n === 2) {
throw 'ex';
}
return n;
})
Produce error
.retry(2)
Number of tries
before hitting error callback
stream.subscribe(
(data) => console.log(data)
(error) => console.log(error)
1
Emits
3
Makes x attempts before error cb is called
Error handling
retryWhen, delay between attempts
let stream = Rx.Observable.interval(1000)
.take(6);
delay, 200 ms.retryWhen((errors) => {
return errors.delay(200);
})
.map((n) => {
if(n === 2) {
throw 'ex';
}
return n;
})
produce an error when
= 2
stream.subscribe(
(data) => console.log(data)
(error) => console.log(error)
Schedulers
bending time
What about schedulers and
testing?
Because scheduler has its own virtual clock
Anything scheduled on that scheduler
will adhere to time denoted on the clock
I.e we can bend time for ex unit testing
Schedulers
testingvar testScheduler =
new Rx.TestScheduler();
var stream =
Rx.Observable
.interval(1000, testScheduler)
.take(5)
.map((val) => {
return val + 1
})
.filter((i) => {
return i % 2 === 0
});
var result;
stream.subscribe((val) => result = val );
console.log('testing function’);
testScheduler.advanceBy(1000);
testScheduler.advanceBy(1000);
console.log('Should equal', result === 4);
increment operator
testScheduler.advanceBy(1000);
testScheduler.advanceBy(1000);
testScheduler.advanceBy(1000);
assert
console.log('Should equal', result === 2);
Comparing promises to Rxjs
.then vs .subscribe
getData()
.then(
)
getData().subscribe(
)
I will keep on streaming values
(data) => console.log(data),
(data) => console.log(data),
(err) => console.log(err) (err) => console.log(err)
Cascading calls
Response:
//getUser
stream
.subscribe((orderItem) => {
console.log('OrderItem',orderItem.id);
})
{ id: 11, userId : 1 }.then(getOrderByUser)
.switchMap((user) => {
//getOrder
return Rx.Observable.of({ id : 11, userId : user.id }).delay(3000)
})
{ id: 123, orderId : 11 }.then(getOrderItemByOrder)
.switchMap((order) => {
//getOrderItem
return Rx.Observable.of({ id: 114, orderId: order.id })
})
{ id: 1 }getUser()
var stream = Rx.Observable.of({ id : 1 });
So we can see the
first user observable
being dropped when
user 2 is emitted
Short word on switchMap
is to ensure we throw away the other calls when a new user is em
We don’t want
getUser
getOrderByUser
getOrderItemByOrder
to complete if a new user is emitted
1 2 3
2 4 5
Not continued
Replaces above
stream
Cascading call
wait for the first
.subscribe(
(data) => {
console.log( 'orders', data[0] );
console.log( 'messages', data[0] );
}
)
var stream = Rx.Observable.of([{ id : 1 }, { id : 2 }]);
getUser()
We wait for user
function getOrdersAndMessages(user){
return Promise.all([
getOrdersByUser( user.id ),
getMessagesByUser( user.id )
])
}
.then(getOrdersAndMessages)
stream.switchMap((user) => {
return Rx.Observable.forkJoin(
Rx.Observable.of([ { id: 1, userId : user.id } ]).delay(500), // orders
Rx.Observable.of([ { id: 100, userId : user.id } ]).delay(1500) //messages
)
})
Calls to orders and message
can happen in parallel
Orders,Messages
arrive at the same time
Rxjs
An elegant weapon for a
more civilized age
Remember..
Thank you

More Related Content

PPTX
Rxjs ngvikings
PDF
Rxjs vienna
PPTX
PPTX
Rxjs marble-testing
PPTX
Angular2 rxjs
PDF
React lecture
PPTX
Firebase ng2 zurich
PPTX
Typescript barcelona
Rxjs ngvikings
Rxjs vienna
Rxjs marble-testing
Angular2 rxjs
React lecture
Firebase ng2 zurich
Typescript barcelona

What's hot (20)

PDF
Angular and The Case for RxJS
PDF
Oop assignment 02
PDF
Introduction to RxJS
PDF
Intro to RxJava/RxAndroid - GDG Munich Android
PDF
ReactiveCocoa in Practice
PDF
Marble Testing RxJS streams
PDF
Practical RxJava for Android
PPTX
Introduction to rx java for android
PDF
Reactive Programming Patterns with RxSwift
PDF
Cascadia.js: Don't Cross the Streams
PPT
Expert JavaScript tricks of the masters
PPTX
Reactive Java (33rd Degree)
PDF
Quick start with React | DreamLab Academy #2
PDF
Solid principles in practice the clean architecture - Droidcon Italy
PDF
Learn You a ReactiveCocoa for Great Good
PDF
Higher Order Components and Render Props
PDF
RxJS - The Reactive extensions for JavaScript
PDF
RxJS Schedulers - Controlling Time
PDF
Recompacting your react application
PDF
Higher-Order Components — Ilya Gelman
Angular and The Case for RxJS
Oop assignment 02
Introduction to RxJS
Intro to RxJava/RxAndroid - GDG Munich Android
ReactiveCocoa in Practice
Marble Testing RxJS streams
Practical RxJava for Android
Introduction to rx java for android
Reactive Programming Patterns with RxSwift
Cascadia.js: Don't Cross the Streams
Expert JavaScript tricks of the masters
Reactive Java (33rd Degree)
Quick start with React | DreamLab Academy #2
Solid principles in practice the clean architecture - Droidcon Italy
Learn You a ReactiveCocoa for Great Good
Higher Order Components and Render Props
RxJS - The Reactive extensions for JavaScript
RxJS Schedulers - Controlling Time
Recompacting your react application
Higher-Order Components — Ilya Gelman
Ad

Viewers also liked (8)

PPTX
Angular modules in depth
PPTX
PPTX
Angular Workshop_Sarajevo2
PPTX
Finjs - Angular 2 better faster stronger
PPTX
Angular2 + rxjs
PDF
RxJava - introduction & design
PPTX
Performance Optimization In Angular 2
PPTX
Nativescript with angular 2
Angular modules in depth
Angular Workshop_Sarajevo2
Finjs - Angular 2 better faster stronger
Angular2 + rxjs
RxJava - introduction & design
Performance Optimization In Angular 2
Nativescript with angular 2
Ad

Similar to Rxjs swetugg (20)

PPTX
Functional Reactive Programming (FRP): Working with RxJS
PDF
Rxjs kyivjs 2015
PDF
Observables in Angular
PPTX
Introduction to RxJS
PPTX
RxJS In-Depth - AngularConnect 2015
PPTX
Functional Reactive Programming with RxJS
PPTX
Reactive programming with rx java
PDF
WebCamp:Front-end Developers Day. Александр Мостовенко "Rx.js - делаем асинхр...
PDF
rx.js make async programming simpler
PPTX
RxJS and Reactive Programming - Modern Web UI - May 2015
PDF
My Gentle Introduction to RxJS
PPTX
Intro to Functional Programming with RxJava
PPTX
Luis Atencio on RxJS
PPTX
From zero to hero with the reactive extensions for JavaScript
PDF
RxJava@Android
PPTX
Rx – reactive extensions
PPTX
Angular mix chrisnoring
PDF
Under the hood of RxJS (Dimitris Livas) - GreeceJS #27
PDF
DZone_RC_RxJS
PDF
Reactive programming in Angular 2
Functional Reactive Programming (FRP): Working with RxJS
Rxjs kyivjs 2015
Observables in Angular
Introduction to RxJS
RxJS In-Depth - AngularConnect 2015
Functional Reactive Programming with RxJS
Reactive programming with rx java
WebCamp:Front-end Developers Day. Александр Мостовенко "Rx.js - делаем асинхр...
rx.js make async programming simpler
RxJS and Reactive Programming - Modern Web UI - May 2015
My Gentle Introduction to RxJS
Intro to Functional Programming with RxJava
Luis Atencio on RxJS
From zero to hero with the reactive extensions for JavaScript
RxJava@Android
Rx – reactive extensions
Angular mix chrisnoring
Under the hood of RxJS (Dimitris Livas) - GreeceJS #27
DZone_RC_RxJS
Reactive programming in Angular 2

More from Christoffer Noring (17)

PPTX
Azure signalR
PPTX
Game dev 101 part 3
PPTX
Game dev 101 part 2
PPTX
Game dev workshop
PPTX
Deploying your static web app to the Cloud
PPTX
IaaS with ARM templates for Azure
PPTX
Learning Svelte
PPTX
PDF
Angular Schematics
PDF
Design thinking
PDF
Keynote ijs
PDF
Vue fundamentasl with Testing and Vuex
PDF
Ngrx slides
PDF
PDF
Nativescript angular
PDF
Graphql, REST and Apollo
PDF
Angular 2 introduction
Azure signalR
Game dev 101 part 3
Game dev 101 part 2
Game dev workshop
Deploying your static web app to the Cloud
IaaS with ARM templates for Azure
Learning Svelte
Angular Schematics
Design thinking
Keynote ijs
Vue fundamentasl with Testing and Vuex
Ngrx slides
Nativescript angular
Graphql, REST and Apollo
Angular 2 introduction

Recently uploaded (20)

PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPT
Teaching material agriculture food technology
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Encapsulation theory and applications.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Approach and Philosophy of On baking technology
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Machine learning based COVID-19 study performance prediction
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Modernizing your data center with Dell and AMD
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Review of recent advances in non-invasive hemoglobin estimation
Per capita expenditure prediction using model stacking based on satellite ima...
Spectral efficient network and resource selection model in 5G networks
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Building Integrated photovoltaic BIPV_UPV.pdf
Teaching material agriculture food technology
Mobile App Security Testing_ A Comprehensive Guide.pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Encapsulation theory and applications.pdf
Network Security Unit 5.pdf for BCA BBA.
Approach and Philosophy of On baking technology
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
The AUB Centre for AI in Media Proposal.docx
Machine learning based COVID-19 study performance prediction
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Modernizing your data center with Dell and AMD
20250228 LYD VKU AI Blended-Learning.pptx
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Chapter 3 Spatial Domain Image Processing.pdf
Review of recent advances in non-invasive hemoglobin estimation

Rxjs swetugg

  • 2. Why Rxjs? We want to deal with async in a “synchronous looking way” We want something better than promises We want one paradigm for async to rule them all
  • 3. nce upon a time in async land There were callbacks Callbacks turned into callback hell
  • 4. Promises to the rescue service .getData() .then(getMoreData) .then(getEvenMore) .then(andSomeMore) Looks great right?
  • 5. But promises were flawed No cancellation eal with other async concepts like mouse positions, clicks, use No rich composition And brexit happened Cumbersome to retry Only returns one value
  • 7. What is an observable Observable is just a function that takes an observer and returns a function Observer: an object with next, error, complete methods Rx.Observable.create((observer) => { observer.next(1); observer.error(‘error’); observer.complete(); }) 1 2 3 4 5 6 7 stream of value over time
  • 8. Promise vs Array vs Observable list .map( x = > x.prop ) .filter( x => x > 2 ) .take( 2 ) Array list .map( x = > x.prop ) .filter( x => x > 2 ) .take( 2 ) .subscribe( x => console.log(x), err => console.log(err) ) Observable Promise service.get() .then( x => console.log(x) ) .catch( err => console.log(err) ) but can also - Cancelled - Retried Array like, handles async
  • 9. So pretty much linq with async
  • 10. Everything is a stream(s) and we can bend it to our will
  • 12. var stream = Rx.Observable.create((observer) =>{ })}) Emits stream .subscribe( (data) => { console.log( data ); } ) 1 next() observer.next(1); 2 next() observer.next(2); 3 next() observer.next(3);
  • 13. var stream = Rx.Observable.create((observer) =>{ }) stream .subscribe( (data) => { console.log( data ); } (err) => { console.log(err); } ) Emits 1 next() observer.next(1); error message error() observer.error(‘something went wrong’)
  • 14. var stream = Rx.Observable.create((observer) =>{ })}) stream .subscribe( (data) => { console.log( data ); } (err) => { console.log(err) }, () => { console.log(‘completed’) } ) Emits 1 next() observer.next(1); complete() observer.complete();
  • 16. var homemadeStream = Rx.Observable.create((observer) => { var i=0; }); var subscription2 = homemadeStream.subscribe((val) => { console.log('Homemade val',val); }); setTimeout(() => { console.log('Cancelling homemadeStream'); subscription2.unsubscribe(); }, 1500); Calling dispose Produce values till someone calls unsubscribe var handle = setInterval(() => { observer.next( i++ ); }, 500); Define whats to happen on unsubscribe return function(){ console.log('Disposing timeout'); clearTimeout( handle ); }
  • 17. You will always create an observable from something
  • 18. Rx.Observable.fromArray([ 1,2,3,4 ]) Rx.Observable.fromEvent(element, ‘event’); Rx.Observable.fromArray(eventEmitter, ‘data’, function(){}) Rx.Observable.fromNodeCallback(fs.createFile) Rx.Observable.fromCallback(obj.callback) Rx.Observable.fromPromise(promise) Rx.Observable.fromIterable(function *() { yield 20 }) Rx.Observable.range(1,3) Rx.Observable.interval(miliseconds)
  • 20. var stream = Rx.Observable.create((observer) => { var request = new XMLHttpRequest(); request.open( ‘GET’, ‘url’ ); request.onload =() =>{ if(request.status === 200) { } else { } } request.onerror = () => { } request.send(); }) stream.subscribe( ) observer.next( request.response ); (result) => { console.log( result ); } Get our data observer.complete(); () => { console.log(‘completed’); } No more data, close stream observer.error( new Error( request.statusText ) ) (err) => { console.log(err) }, observer.error( new Error(‘unknown error’) ); Error Error
  • 21. Hot vs Cold Observable
  • 22. Cold Observable recorded tv show Hot observable Live streaming eg World Cup Final
  • 23. Observables are cold by default, unless you make them hot var stream = Rx.Observable.interval(1000); stream.subscribe( (val) => { console.log('Subscriber 1', val);} ); stream.subscribe( (val) => { console.log('Subscriber 2', val); } ); var stream = Rx.Observable .interval(1000) .publish(); stream.subscribe((val) => { console.log('Subscriber 1', val); }); setTimeout(function() { stream.connect(); }, 2000); setTimeout(() => { stream.subscribe((val) => { console.log('Value', val); }); }, 4000); Subscriber 2 Values 0 1 2 3 4 Subscriber 1 Values 0 1 2 3 4 0 1 2 3 4 5 6 3 4 5 6
  • 24. You can create an observable from almost any async concept Operators however gives it its power Remember: But:
  • 25. Operators makes your code look like linq
  • 26. 120+ operators Rxjs 4 60+ Rxjs 5 Combination Conditional Multicasting Filtering Transformation Utility Categories in production
  • 27. Marble diagram how does that operator work
  • 28. Operator Most operators are covered at rxmarbles.com Stream 1 2 3 Other stream 4 5 Resulting stream 1 2 3 4 5
  • 29. Operator example var stream = Rx.Observable.of(1,2,3,4,5); stream stream.subscribe((data) => { console.log(‘data’); }) Operators : map() filter() 3 Emits 6 .map((val) => { return val + 1; }) changes the value .filter((val) => { return val % 3 === 0; }) filters out values
  • 31. Do var stream = Rx.Observable.of(1,2,3,4,5); var subscription = stream .filter(function(val){ return val % 2 === 0; }); subscription.subscribe(function(val){ console.log('Val',val); }) Echos every value without changing it, used for logging .do((val) => { console.log('Current val', val); }) Current val 1 Current val 2 Current val 3 Current val 4 Current val 5 Subscribe: 2 4
  • 32. Delay Delay, the whole sequence stream .subscribe( (val) => { var newTime = new Date().getTime(); console.log('Delayed', val + " " + (newTime - time)); }) Rx.Observable.merge( ).subscribe((val) => { var newTime = new Date().getTime(); console.log('Merged Delay', val + " " + (newTime - time)); }) ….. 1 second 1 2 3 ….. 1 second Marco ….. 2 second Polo .delay(1000) Rx.Observable.of('Marco').delay(1000), Rx.Observable.of('Polo').delay(2000)
  • 34. combineLatest var stream = Rx.Observable.fromArray([1,2,3,4]); var stream2 = Rx.Observable.fromArray([5,6,7,8]); var combineLatest = Rx.Observable combineLatest.subscribe( function(val) { console.log( 'combine ', val ) }) [1,5], [2,6], [3,7], [4,8] .combineLatest( stream, stream2 );
  • 36. debounce var debounceTime = Rx.Observable .fromEvent(button,'click') debounceTime.subscribe( function(){ console.log('mouse pressed'); }) waits x ms and returns latest emitted Ignores all generated mouse click events for 2 seconds.debounce(2000); Clicking save button 2secclick click click click click save()
  • 37. takeUntil .subscribe( (err) => {}, ) var mouseUp = Rx.Observable.fromEvent(button,'mouseup'); Break condition .takeUntil( mouseUp ) () =>{ console.log('condition fulfilled'); } Generate numbers var mousePressedTimer = Rx.Observable.interval(1000); triggers every secondmousePressedTimer (val) =>{ console.log('mouse up has NOT happened yet’,val); },
  • 38. var throttle = Rx.Observable .interval( 1000 ) .throttle( 1000 ); throttle.subscribe( (val) => { console.log('Throttle', val );} ); throttle Delay every value by x milliseconds 1 2 3 4 1 2
  • 40. buffer let numbers = Rx.Observable.interval(1000); let bufferBy = Rx.Observable.fromEvent(document,'click'); let buffered = numbers.buffer( bufferBy ); buffered.subscribe( ); Numbers are generated to a buffer until condition is met, then the buffer content is emitted Generate value to buffer Release all values (values) => { console.log(‘Buffered',values);}
  • 41. switchMap Switch map, complete something based on a condition breakCondition = Rx.Observable.fromEvent(document,'click'); breakCondition.switchMap((val) => { return Rx.Observable.interval(3000).mapTo(‘Do this'); }) breakCondition.subscribe((val) => { console.log('Switch map', val); }) Intended action is completed/restarted by ‘breakCondition’ etc.. Do this Do this Do this Do this Do this click click
  • 42. source.subscribe((data) => { console.log( data ); }) flatMap let source = Rx.DOM.getJSON( 'data2.json' ) return Rx.Observable.fromArray( data ).map((row) => { return row.props.name; }); return observable .flatMap((data) => { } ); We get an array response that we want to emit row by row We use flatMap instead of map because : We want to flatten our list to one stream
  • 43. flatMap explained when you create a list of observables flatMap flattens that list s Great when changing from one type of stream to another Without it you would have to listen to every single substream, w eve nt eve nt eve nt eve nt ajax ajax ajax ajax json json json json flatMap map
  • 44. Problem : Autocomplete Listen for keyboard presses Filter so we only do server trip after x number of chars are entered Do ajax call based on filtered input Cash responses, don’t do unnecessary calls to http server
  • 46. let input = $(‘#input’); input.bind(‘keyup’,() = >{ let val = input.val() if(val.length >= 3 ) { if( isCached( val ) ) { buildList( getFromCache(val) ); return; } doAjax( val ).then( (response) => { buildList( response.json() ) storeInCache( val, response.json() ) }); } }) fetch if x characters long return if cached do ajax Ok solution but NOT so fluent We need 3 methods to deal with cache
  • 48. Stream modeling key key key key key key FILTER AJAX CALL jso n jso n MAP key key key key key key key respons e respons e
  • 49. flatmapExample = Rx.Observable.fromEvent(input,'keyup') flatmapExample.subscribe( (result) =>{ console.log('Flatmap', result); buildList( result ) } ) more fluent Transform event to char.map((ev) => { return ev.target.value; }) Wait until we have 3 chars .filter(function(text){ return text.length >=3; }) Only perform search if this ‘search’ is unique.distinctUntilChanged() Excellent to use when coming from one stream to another .switchMap((val) => { return Rx.DOM.getJSON( 'data3.json' ); })
  • 51. Error handling kills the stream var errStream = Rx.Observable.throw('Error'); var stream = Rx.Observable.create((observer) => { observer.next(1); }) var merged = Rx.Observable .merge( errStream, stream ) merged.subscribe( (val) => { console.log('Val', val);}, (err) => { console.log('Err', err);}, () => { console.log('completed'); }) Captured here but sequence interrupted, completed NOT reached
  • 52. Error handling better, but can be improved var errStream = Rx.Observable.throw('Error'); var stream = Rx.Observable.create((observer) => { observer.onNext(1); }) var merged = Rx.Observable .merge( errStream, stream ) merged.subscribe( (val) => { console.log('Val', val); }, (err) => { console.log('Err', err); }, ) stream not processed though can we improve it? Completed reach, stream never emits its values though () => { console.log(‘completed'); } .catch((err) => { return Rx.Observable.of(err); });
  • 53. Error handling wrap error stream before merge var errStream = Rx.Observable .throw('AAA thump..') .catch(function(err){ return Rx.Observable.of(err); }); merged.subscribe( (val) => { console.log('Val', val); }, (err) => { console.log('Err', err); }, ) () => { console.log(‘completed'); } all values are emitted
  • 54. Error ignoring You have several sources and two terminates You want the other normal source to work var errStream = Rx.Observable .throw('AAA thump..'); var errStreamWithFlattenedData = Rx.Observable .interval(500) .take(3) .flatMap((val) => { if( val === 1 ) { return Rx.Observable.throw('crashing'); } else { return Rx.Observable.return(val); } }) var normalStream = Rx.Observable.return('anything'); var handledErrorStream = Rx.Observable .onErrorResumeNext( errStream, normalStream, errStreamWithFlattenedData ); handledErrorStream.subscribe( (val) =>{ console.log('error stream ignored', val);}, (err) => { console.log("error stream ignored, error",err);}, () => { console.log("completion of error stream ignored”);} )
  • 55. Error handling retry let stream = Rx.Observable.interval(1000) .take(6); .map((n) => { if(n === 2) { throw 'ex'; } return n; }) Produce error .retry(2) Number of tries before hitting error callback stream.subscribe( (data) => console.log(data) (error) => console.log(error) 1 Emits 3 Makes x attempts before error cb is called
  • 56. Error handling retryWhen, delay between attempts let stream = Rx.Observable.interval(1000) .take(6); delay, 200 ms.retryWhen((errors) => { return errors.delay(200); }) .map((n) => { if(n === 2) { throw 'ex'; } return n; }) produce an error when = 2 stream.subscribe( (data) => console.log(data) (error) => console.log(error)
  • 58. What about schedulers and testing? Because scheduler has its own virtual clock Anything scheduled on that scheduler will adhere to time denoted on the clock I.e we can bend time for ex unit testing
  • 59. Schedulers testingvar testScheduler = new Rx.TestScheduler(); var stream = Rx.Observable .interval(1000, testScheduler) .take(5) .map((val) => { return val + 1 }) .filter((i) => { return i % 2 === 0 }); var result; stream.subscribe((val) => result = val ); console.log('testing function’); testScheduler.advanceBy(1000); testScheduler.advanceBy(1000); console.log('Should equal', result === 4); increment operator testScheduler.advanceBy(1000); testScheduler.advanceBy(1000); testScheduler.advanceBy(1000); assert console.log('Should equal', result === 2);
  • 61. .then vs .subscribe getData() .then( ) getData().subscribe( ) I will keep on streaming values (data) => console.log(data), (data) => console.log(data), (err) => console.log(err) (err) => console.log(err)
  • 62. Cascading calls Response: //getUser stream .subscribe((orderItem) => { console.log('OrderItem',orderItem.id); }) { id: 11, userId : 1 }.then(getOrderByUser) .switchMap((user) => { //getOrder return Rx.Observable.of({ id : 11, userId : user.id }).delay(3000) }) { id: 123, orderId : 11 }.then(getOrderItemByOrder) .switchMap((order) => { //getOrderItem return Rx.Observable.of({ id: 114, orderId: order.id }) }) { id: 1 }getUser() var stream = Rx.Observable.of({ id : 1 }); So we can see the first user observable being dropped when user 2 is emitted
  • 63. Short word on switchMap is to ensure we throw away the other calls when a new user is em We don’t want getUser getOrderByUser getOrderItemByOrder to complete if a new user is emitted 1 2 3 2 4 5 Not continued Replaces above stream
  • 64. Cascading call wait for the first .subscribe( (data) => { console.log( 'orders', data[0] ); console.log( 'messages', data[0] ); } ) var stream = Rx.Observable.of([{ id : 1 }, { id : 2 }]); getUser() We wait for user function getOrdersAndMessages(user){ return Promise.all([ getOrdersByUser( user.id ), getMessagesByUser( user.id ) ]) } .then(getOrdersAndMessages) stream.switchMap((user) => { return Rx.Observable.forkJoin( Rx.Observable.of([ { id: 1, userId : user.id } ]).delay(500), // orders Rx.Observable.of([ { id: 100, userId : user.id } ]).delay(1500) //messages ) }) Calls to orders and message can happen in parallel Orders,Messages arrive at the same time
  • 65. Rxjs An elegant weapon for a more civilized age Remember..