Angular 2 observables
-
Upload
geoffrey-filippi -
Category
Technology
-
view
1.216 -
download
0
Transcript of Angular 2 observables
Angular 2Observables, Change Detection, AsyncPipe,
Forms, Rx.js / Geoff Filippi @geofffilippi
Geoff FilippiApplication Architect
Experience
Worked on streaming media (Voice over IP), 6 years5 million phone customers
Experience
Worked on video and streaming video, 4 years
Projectstwctv.com
Video streaming websitebackbone.js
Video streaming Set-Top Box (STB) web application
Oildex ProjectsRewrite 10+-year-old appsAngular 1.4
New routerES5
No Angular 2, yetNo ES6 or Typescript, yet
We will coverFundamentalsRelated conceptsAngular 2
FundamentalsA brief overview of reactive fundamentals
Reactive programmingFunctional Reactive Programming (FRP)Observer design pattern
Related conceptsObject.observePromises
Other Async conceptsRxJS
Observables in Angular 2FormsHttpAsyncPipe
Fundamentals
Overview of Reactive ProgrammingData flowsPropagation of change
Reactive ManifestoResponsiveResilientElasticMessage Driven
(FRP) Functional ReactiveProgramming
Reactive Programming
and
Functional Programming
Related Concepts
Observer Design PatternSubject tracks observersSubject notifies observers of state changes
TerminologySubject, Observable, ProducerObserver, Consumer
Object.observeNot the same as Observables
Formerly a Stage 2 ProposalProxy
Proposal withdrawn
Criticisms of Object.observeBig changes to Object internalsBad performanceNot well-supported or adopted
But really,
Ecosystem moved in different direction
ObservableStage 1 Current ProposalDescriptionSpecification
Differences between Observablesand Object.observe
Creating an ObservableThe hard way
let myObservable = new Observable(observer => { const token = doAsyncAction((err, value) => { if (err) } observer.error(err); } else { observer.next(value); observer.complete(); } });
return () => { cancelAsyncAction(token); }; {);
CompareObservablesPromisesEventscallbacks
Promises vs. ObservablesPromise is like a async variableObservable is like a async array
Promises vs. ObservablesPromises
Single valueCannot be cancelledNot lazyGood for some AJAX calls
Unless you want to cancel themCatch, Finally
Promises vs. ObservablesObservables
StreamsCan be unsubscribedLazy, until you call .subscribe()Better for events, WebSockets
Animations, AJAX you want to cancelCan by retriedCatch, Finally
Bridging ObservablesEasy way to get an observable
You can turn other async concepts into an observable usinga helper
Promises.from()
Generator.from()
Callbacks.fromCallback()
Bridging ObservablesEasy way to get an observable
Events.fromEvent()
DOMAJAXWebSockets .fromWebSocket()
Other observable implementationsbacon.jskefir.js
Bridging ObservablesYou can even turn non-async concepts into an observable
using a helper
Arrays.of()
Working with Promises vs.Observables
.then() becomes .subscribe()
Working with Promises vs.Observables
Observables do not need to complete, but...
If you need to do something when an observable completes,pass an optional complete handler
RxJSv5.0.0-beta.1ObservableSubjectImplementation used in Angular 2
Angular 2
Angular 2FormsHttpAsyncPipeChange detection
FormsForms are observable
this.myForm = fb.group({ 'sku': ['', Validators.required] });
this.sku = this.myForm.controls['sku'];
this.sku.valueChanges.subscribe( (value: string) => { console.log('sku changed to: ', value); } );
this.myForm.valueChanges.subscribe( (value: string) => { console.log('form changed to: ', value); } );
HttpCancel requestsStream results
"type ahead"Returns observable
HttpgetRandomQuote() { this.http.get('http://localhost:3001/api/random-quote') .retry(3) .map(res => res.text()) .subscribe( data => this.randomQuote = data, err => this.logError(err), () => console.log('Random Quote Complete') ); }
PipesLike Angular 1 filter
AsyncPipeCan subscribe to observables
timerAsync = Observable.interval(1000).startWith(0);
<h2>Current Total (Async Pipe): {{timerAsync | async}}</h2>
Change DetectionNew in Angular 2:
Apps are reactiveChange detection is directionalYou can skip walking parts of the tree
Immutable objectsObservables
ngrxReactive Extensions for Angular 2
ngrx/store
Can be used to implement elm/redux-style applications inAngular 2
Questions?