KEMBAR78
Angular & RXJS: examples and use cases | PDF
ANGULAR&RXJS
FABIOBIONDI.io
SEP2018
fabiobiondi.io
TRAINING AND MENTORING
ANGULAR JAVASCRIPT REACT
OPPORTUNITÀ per DEVELOPER ITALIANI

(jobs / collaborations / training and more)
FACEBOOK GROUPS
developer italianideveloper italianideveloper italiani
RXJS-6-INTRO
interval(value)
returns an observable sequence that produces a
value after each period.
map(fn)
transform the items emitted by an Observable by
applying a function to each item
<fb-clock>
ANGULAR & RXJS
You don't need subscribe(fn)
{{myObs$ | async}}
the async Angular pipe “I” 

automatically subscribes an observer
<fb-clock>
WHERE CAN YOU USE RXJS IN ANGULAR?
ALMOST EVERYWHERE!

In components, directives, pipes, services,
XHR, router, guards, interceptors ...
ASync Pipes
{{'Milano' | temperature | async }} 

// output: 20°
Router EVENTS
FILTER Router EVENTS
Reactive FORMSTemplate-driven FORMS
RxJS operators used in forms
Create Component at Runtime
HttpClient
HttpClient & map(fn)
Leanne Graham (Sincere@april.biz)
Transform HTTP response
forkJoin(array)
Combine Multiple HTTP requests
When all observables complete, 

emit the last emitted value from each.
switchMap
Map to observable, complete previous inner observable, 

emit values.
!
NESTED XHR with switchMap(fn)
Solution
Sequential HTTP requests
exhaustMap(array)
Abort new HTTP requests until the previous one has been completed
Services
UserService: fetch data View: request and use data
perform actions or side-effects, such as logging.
tap(fn)
“Debug” and side effects with tap()
AUTHENTICATION
A value that changes over time
BehaviorSubject
Store authentication token
UI: SignInComponent
SignIn by using LoginService
LoginService
Invoke login REST Service and save token
SECURITY
*ifLogged Structural Directive
Render DOM if user is logged
Usage
AuthGuard
Disable route access if user is not logged
Usage
HTTP Interceptors
Inject token to each XHR and handle errors
NGRX (Redux)
NGRX Store
! Too many 

subscriptions
*ngIf…as
Only one 

subscription
NGRX Effects
DATA MANIPULATION
7: telly.hoeger@billy.biz
8: sherwood@rosamond.me
9: chaim_mcdermott@dana.io
10: rey.padberg@karina.bizt
FILTER ARRAY with map: easiest way
Get user ID > 6 and email lowercase
X
FILTER ARRAY WITH RXJS (1/3) 

Wrong Way
FILTER ARRAY WITH RXJS (2/3): 

flatten in single emissions
Get user ID > 6
!
Too many. I just need one!
we only need to emit one value (3/3)
Get user ID > 6 and email lowercase
scan & reduce
scan(fn)
applies an accumulator function over an observable
sequence and returns each intermediate result
reduce(fn)
apply a function to each item emitted by an
Observable, sequentially, and emit the final value
scan
!
Too many. 

I just need one!
Display the total of likes
Given an array
Display the total of likes
scan
Display the total of likes
Given an array
Display the total of likes
Yo! :)
REAL-WORLD-EXAMPLES with
scan(fn)
<Clock> Picker
<ImageGallery>
A simple carousel with timer and NEXT / PREV buttons
!
!
<ImageGallery>
Enhanced version: get Token, HTTP requests and completely “pure”
ANGULAR JAVASCRIPT REACT
OPPORTUNITÀ per DEVELOPER ITALIANI

(jobs / collaborations / training and more)
FACEBOOK GROUPS
developer italianideveloper italianideveloper italiani
fabiobiondi.io

Angular & RXJS: examples and use cases