Programming with asynchronous observables data streamsAndré Staltz, Reactive programming introduction
Observable.from([1,2,3])
.map(num => num + 1)
.filter(num => num % 2)
.subscribe(num => console.log(num))
Immutability, in/out, testeability
const obs = Observable.from([1,2,3])
.map(num => num + 1)
.filter(num => num % 2)
obs.subscribe(num => console.log("From 1", num))
obs.subscribe(num => console.log("From 2", num))
One stream, multiple subscriptions
const obs1 = Observable.from([1,2,3])
const obs2 = Observable.from([4,5,6])
Observable.merge(obs1, obs2)
.subscribe(num => console.log(num))
Multiple streams composition
const items$ = Rx.Observable.from([1,2,3,4,5])
.partition((x) => x % 2 == 0)
const evens = items$[0]
const odds = items$[1]
evens.subscribe(data => console.log(`even ${data}`))
odds.subscribe(data => console.log(`odd ${data}`))
Streams separation
Observable
.from // Promise, array or iterable
.fromEvent // DOM event, NodeJS EventEmmiter
.of
.bindCallback
.bindNodeCallback
.map/.mapTo
.mergeMap
.switchMap
.filter
.first/.last
.take
.merge
.combineLatest
.concat
.zip
.forkJoin
const obs1 = Observable.fromEvent(input, 'keyup')
.map(e => e.target.value)
.filter(value => value.length > 2) // min 3 characters
.distinctUntilChanged()
.debounceTime(500) // ms
.mergeMap(word => this.http.get('...'))
.retry(2)
.subscribe(res => console.log(res))
const click$ = Rx.Observable.fromEvent(document, 'click')
click$.bufferWhen(() => click$.delay(350))
.map(clicks => clicks.length)
.filter(clicks => clicks > 1)
.subscribe(clicks => console.log(clicks))
How would you do a drag event?
Suscribete! goo.gl/rPdx4f