1 of 40

Will tweet a link to slides and code

2 of 40

RxJS and Observables

By Duncan Hunter

Join the Conversation #angularhackday @DuncHunter

3 of 40

Duncan

Hunter

Architect at SSW

@DuncHunter

4 of 40

Agenda

Where does RxJS fit into Angular

What is RxJS

RxJS and Angular's HTTP module

 

Join the Conversation #angularhackday @DuncHunter

5 of 40

Who has used RxJS?

6 of 40

Where does RxJS fit into Angular

What is RxJS

RxJS and Angular's HTTP module

 

Join the Conversation #angularhackday @DuncHunter

7 of 40

An API for asynchronous programming

with observable streams

8 of 40

Observable�[1]

Observer 1

[1]

9 of 40

Observable�[1,4]

Observer 1

[1]

10 of 40

Observable�[1,4]

Observer 1

[1,4]

11 of 40

Observable�[1,4,7]

Observer 1

[1,4]

12 of 40

Observable�[1,4,7]

Observer 1

[1,4,7]

13 of 40

.map(x => x + 1)

Observable�[1]

Observer 1

[2]

14 of 40

.map(x => x + 1)

Observable�[1,4]

Observer 1

[2]

15 of 40

.map(x => x + 1)

Observable�[1,4]

Observer 1

[2,5]

16 of 40

.map(x => x + 1)

Observable�[1,4,7]

Observer 1

[2,5]

17 of 40

.map(x => x + 1)

Observable�[1,4,7]

Observer 1

[2,5,8]

18 of 40

.map(x => x + 1)

.filter(x => x > 2)

Observable[1,4,7]

Observer 1

[2,5,8]

Observer 2

[4,7]

19 of 40

.map(x => x + 1)

.filter(x => x > 2)

.reduce((x,y) => x + y)

Observable�[1,4,7]

Observer 1

[2,5,8]

Observer 2

[4,7]

20 of 40

.map(x => x + 1)

.filter(x => x > 2)

.reduce((x,y) => x + y)

Observable�[1,4,7]

Observer 1

[2,5,8]

Observer 2

[13]

21 of 40

.map(x => x + 1)

.filter(x => x > 2)

.reduce((x,y) => x + y)

Observable�[1,4,7]

Observer 1

[2,5,8]

Observer 2

[13]

22 of 40

There are a lot of operators!

23 of 40

buffer

bufferCount

bufferTime

bufferToggle

bufferWhen

combineAll

combineLatest

concat

concatAll

concatMap

concatMapTo

count

debounce

debounceTime

defaultIfEmpty

delay

delayWhen

dematerialize

distinctUntilChanged

do

every

expand

filter

first

groupBy

ignoreElements

last

let

map

mapTo

merge

mergeMap

partition

pluck

publish

race

repeat

retry

retryWhen

sample

scan

share

single

skip

skipUntil

skipWhile

startWith

switchMap

window

windowCount

windowTime

windowToggle

windowWhen

withLatestFrom

zip

24 of 40

Code Demo

Observers and Observables

Subscribe to a stream

Apply operators

25 of 40

  • An API for asynchronous programming with observable streams
  • Use operators handling asynchronous events as collections
  • Observables are lazy and need to be subscribed to

What is RxJS

26 of 40

Where does RxJS fit into Angular

What is RxJS

RxJS and Angular's HTTP module

 

Join the Conversation #angularhackday @DuncHunter

27 of 40

Observables are baked into Angular

28 of 40

getUsers() {

this.http.get(`http://swapi.co/api/people/`)

.subscribe(people => console.log(people));

}

29 of 40

@Component()export class SearchComponent implements OnInit {� searchControl = new FormControl();ngOnInit() {this.searchControl.valueChanges.subscribe(value => {// do something with value here});}}

30 of 40

ngOnInit() {this.route.params

.subscribe(params => this.id = params['id'])

}

31 of 40

ngOnInit() {this.route.params

.map(params => params['id'])

.switchMap(id => this.contactsService.getContact(id))

.subscribe(contact => this.contact = contact);

}

32 of 40

  • RxJS is baked into Angular
  • Used in reactive forms, HTTP and the router
  • You can start with just HTTP

Where does RxJS fit into Angular

33 of 40

Where does RxJS fit into Angular

What is RxJS

RxJS and Angular's HTTP module

 

Join the Conversation #angularhackday @DuncHunter

34 of 40

HTTP is not the best example of why to use observables over promises

35 of 40

@Injectable()export class PeopleService {

constructor(private http: Http) { }

getPeople() {

return this.http.get(`http://swapi.co/api/people/`)

.map(response => response.json());

.subscribe(people => console.log(people));

}

}

36 of 40

Code Demo

HTTP request

Call remote server

37 of 40

  • HTTP response is observable by default
  • Starting point for working with RxJS

Summary - Observables and Angular's HTTP module

38 of 40

Where do Observables fit into Angular 2

What is RxJS

Observables and Angular's HTTP module

Common Angular RxJS operators

 

Join the Conversation #angularhackday @DuncHunter

RxJS is a Journey

39 of 40

Summary

Where does RxJS fit into Angular

What is RxJS

RxJS and Angular's HTTP module

 

Join the Conversation #angularhackday @DuncHunter

40 of 40

@dunchunter

Thank you!

bit.ly/angularhackday-seattle2017-rxjs

bit.ly/bit.ly/angularhackday-seattle2017-rxjs-code