RxJS 5 RxJS 5 RxJS 5 RxJS 5 In-depth In-depth by Gerard Sans - - PowerPoint PPT Presentation

rxjs 5 rxjs 5 rxjs 5 rxjs 5 in depth in depth
SMART_READER_LITE
LIVE PREVIEW

RxJS 5 RxJS 5 RxJS 5 RxJS 5 In-depth In-depth by Gerard Sans - - PowerPoint PPT Presentation

RxJS 5 RxJS 5 RxJS 5 RxJS 5 In-depth In-depth by Gerard Sans (@gerardsans) A little about me A little about me a bit more... a bit more... A A A A synchronous Data Streams synchronous Data Streams synchronous Data Streams synchronous


slide-1
SLIDE 1

RxJS 5 RxJS 5 RxJS 5 RxJS 5 In-depth In-depth

by Gerard Sans (@gerardsans)

slide-2
SLIDE 2

A little about me A little about me

slide-3
SLIDE 3

a bit more... a bit more...

slide-4
SLIDE 4
slide-5
SLIDE 5

A A A Asynchronous Data Streams

synchronous Data Streams synchronous Data Streams synchronous Data Streams

slide-6
SLIDE 6
slide-7
SLIDE 7

Asynchronous Asynchronous Data Data Streams Streams

will happen some time in the future

slide-8
SLIDE 8

Asynchronous Asynchronous Data Data Streams Streams

raw information

slide-9
SLIDE 9

Asynchronous Data Asynchronous Data Streams Streams

values made available over time

slide-10
SLIDE 10

Examples Examples

1 1 2 2 3 3

[ , , ]

Stream Array

slide-11
SLIDE 11

Pull vs Push Pull vs Push

Pull Push Arrays, Generators, Iterables DOM Events Promises Observables synchronous asynchronous

slide-12
SLIDE 12

Pull Example Pull Example

// Iterable/Iterator let iterator = [1, 2, 3].values(); console.log(iterator.next()); // {"value":1,"done":false} console.log(iterator.next()); // {"value":2,"done":false} console.log(iterator.next()); // {"value":3,"done":false} console.log(iterator.next()); // {"done":true} for (let x of [1, 2, 3]) { console.log(x); }

slide-13
SLIDE 13

Push Examples Push Examples

// DOM Events var image = document.getElementById('avatar'); image.addEventListener('load', successHandler); image.addEventListener('error', errorHandler); // Promise (single value) get('languages.json') .then(successHandler, errorHandler);

slide-14
SLIDE 14

Streams timeline Streams timeline

(Unix 3, 1973) pipes

slide-15
SLIDE 15

(Node.js, 2009) streams

slide-16
SLIDE 16

(Microsoft, 2009)

  • bservables
slide-17
SLIDE 17

(Angular 2, 2014)

  • bservables
slide-18
SLIDE 18

101 Arrays 101 Arrays

Array Extras (ES5) . (), . (), . () and . () forEach map filter reduce Composition

slide-19
SLIDE 19

forEach forEach

var team = [ { name: "Igor Minar", commits: 259 }, { name: "Jeff Cross", commits: 105 }, { name: "Brian Ford", commits: 143 } ]; for(var i=0, ii=team.length; i<ii; i+=1){ console.log(team[i].name); } team.forEach( member => console.log(member.name) ); // Igor Minar // Jeff Cross // Brian Ford

slide-20
SLIDE 20

map map

var team = [ { name: "Igor Minar", commits: 259 }, { name: "Jeff Cross", commits: 105 }, { name: "Brian Ford", commits: 143 } ]; var newTeam = []; for(var i=0, ii=team.length; i<ii; i+=1){ newTeam.push({ name: team[i].name }); } var onlyNames = team.map( member => ({ name: member.name }) );

slide-21
SLIDE 21

fjlter fjlter

var team = [ { name: "Igor Minar", commits: 259 }, { name: "Jeff Cross", commits: 105 }, { name: "Brian Ford", commits: 143 } ]; var onlyOver120Commits = []; for(var i=0, ii=team.length; i<ii; i+=1){ if (team[i].commits>120) {

  • nlyOver120Commits.push(team[i]);

} } var onlyOver120Commits = team.filter( member => member.commits>120 );

slide-22
SLIDE 22

reduce reduce

var team = [ { name: "Igor Minar", commits: 259 }, { name: "Jeff Cross", commits: 105 }, { name: "Brian Ford", commits: 143 } ]; var total = 0; // initial value for(var i=0, ii=team.length; i<ii; i+=1){ total = total + team[i].commits; } var total = team.reduce( (total, member) => total + member.commits , 0); // initial value // 507

slide-23
SLIDE 23

Composition Composition

var over120Commits = x => x.commits>120; var memberName = x => x.name; var toUpperCase = x => x.toUpperCase(); var log = x => console.log(x); team .filter(over120Commits) .map(memberName) .map(toUpperCase) .forEach(log); // IGOR MINAR // BRIAN FORD

slide-24
SLIDE 24
slide-25
SLIDE 25

RxJS 5 RxJS 5

beta2

slide-26
SLIDE 26

Main Contributors Main Contributors

@BenLesh @AndreStaltz @_ojkwon @trxcllnt @robwormald

Paul Taylor

slide-27
SLIDE 27

//Observable constructor let obs$ = new Observable(observer => { try { //pushing values

  • bserver.next(1);
  • bserver.next(2);
  • bserver.next(3);

//complete stream

  • bserver.complete();

} catch(e) { //error handling

  • bserver.error(e);

} });

Observable Observable

slide-28
SLIDE 28

Basic Stream Basic Stream

//ASCII Marble Diagram

  • ---0----1----2----3----> Observable.interval(1000);
  • ---1----2----3| Observable.fromArray([1,2,3]);
  • ---# Observable.of(1,2).do(x => throw
  • --> is the timeline

0, 1, 2, 3 are emitted values # is an error | is the 'completed' signal

RxMarbles

slide-29
SLIDE 29

Observable helpers Observable helpers

//Observable creation helpers Observable.of(1); // 1| Observable.of(1,2,3).delay(100); // ---1---2---3| Observable.from(promise); Observable.from(numbers$); Observable.fromArray([1,2,3]); // ---1---2---3| Observable.fromEvent(inputDOMElement, 'keyup');

slide-30
SLIDE 30

Subscribe Subscribe

Observable.subscribe( /* next */ x => console.log(x), /* error */ x => console.log('#'), /* complete */ () => console.log('|') ); Observable.subscribe({ next: x => console.log(x), error: x => console.log('#'), complete: () => console.log('|') });

slide-31
SLIDE 31

Hot vs Cold Hot vs Cold

Hot Cold

  • bs.shared()

default broadcasted pre-recorded subscribers synced subscribers not synced

slide-32
SLIDE 32

Unsubscribe Unsubscribe

var subscriber = Observable.subscribe( twit => feed.push(twit), error => console.log(error), () => console.log('done') ); subscriber.unsubscribe();

slide-33
SLIDE 33

Operators Operators

// simple operators map(), filter(), reduce(), scan(), first(), last(), single(), elementAt(), toArray(), isEmpty(), take(), skip(), startWith() // merging and joining merge(), mergeMap(flatMap), concat(), concatMap(), switch switchMap(), zip() // spliting and grouping groupBy(), window(), partition() // buffering buffer(), throttle(), debounce(), sample()

slide-34
SLIDE 34
slide-35
SLIDE 35

Schedulers Schedulers

// Synchronous (default: Scheduler.queue) Observable.of(1) .subscribe({ next: (x) => console.log(x) complete: () => console.log('3') }); console.log('2'); // a) 1 2 3 // b) 2 1 3 // c) 1 3 2 // d) 3 2 1

slide-36
SLIDE 36

Schedulers Schedulers

// Asynchronous Observable.of(1) .observeOn(Scheduler.asap) .subscribe({ next: (x) => console.log(x) complete: () => console.log('3') }); console.log('2'); // a) 1 2 3 // b) 2 1 3 // c) 1 3 2 // d) 3 2 1

slide-37
SLIDE 37
slide-38
SLIDE 38

Debugging RxJS Debugging RxJS

No debugger support yet

  • bs.do(x => console.log(x))

Drawing a marble diagram

slide-39
SLIDE 39

RxJS 5 use cases RxJS 5 use cases

Asynchronous processing Http Forms: controls, validation Component events EventEmitter

slide-40
SLIDE 40

Wikipedia Search Wikipedia Search

Http (Jsonp) Form: control (valueChanges) Async pipe RxJS operators flatMap/switchMap retryWhen

plunker

slide-41
SLIDE 41

Why Observables? Why Observables?

Flexible: sync or async Powerful operators Less code

slide-42
SLIDE 42
slide-43
SLIDE 43

Want more? Want more?

RxJS 5 ( ) Wikipedia Search ( ) RxJS 5 Koans ( ) github Reactive Extensions plunker plunker

slide-44
SLIDE 44

Thanks! Thanks!