|
| 1 | +// Demonstrate Observable principles discussed in the doc |
| 2 | +// #docplaster |
| 3 | +import { Injectable } from '@angular/core'; |
| 4 | +import { Http } from '@angular/http'; |
| 5 | + |
| 6 | +import { Observable } from 'rxjs/Observable'; |
| 7 | + |
| 8 | +import 'rxjs/add/observable/fromPromise'; |
| 9 | +import 'rxjs/add/observable/interval'; |
| 10 | + |
| 11 | +import 'rxjs/add/operator/do'; |
| 12 | +import 'rxjs/add/operator/filter'; |
| 13 | +import 'rxjs/add/operator/map'; |
| 14 | +import 'rxjs/add/operator/take'; |
| 15 | +import 'rxjs/add/operator/toPromise'; |
| 16 | + |
| 17 | +import { Hero } from './hero'; |
| 18 | +import { InMemoryDataService } from './in-memory-data.service'; |
| 19 | +import { EventAggregatorService } from './event-aggregator.service'; |
| 20 | + |
| 21 | + |
| 22 | +@Injectable() |
| 23 | +export class ObservablePrinciples { |
| 24 | + private heroesUrl = 'api/heroes'; |
| 25 | + |
| 26 | + constructor( |
| 27 | + private http: Http, |
| 28 | + private eventService: EventAggregatorService) { } |
| 29 | + |
| 30 | + functionalArray() { |
| 31 | + // #docregion functional-array |
| 32 | + // double the odd numbers in the array. |
| 33 | + const numbers = [0, 1, 2, 3, 4, 5]; |
| 34 | + return numbers.filter(n => n % 2 === 1).map(n => n * 2); |
| 35 | + // #enddocregion functional-array |
| 36 | + } |
| 37 | + |
| 38 | + functionalEvents() { |
| 39 | + // #docregion functional-events |
| 40 | + // double the next odd integer every tick ... forever. |
| 41 | + const numbers = Observable.interval(0); |
| 42 | + return numbers.filter(n => n % 2 === 1).map(n => n * 2); |
| 43 | + // #enddocregion functional-events |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Call the functional array and event example methods |
| 48 | + * and write their results to the EventAggregatorService |
| 49 | + * for display in AppComponent. |
| 50 | + */ |
| 51 | + callFunctionalExamples() { |
| 52 | + |
| 53 | + this.eventService.add({ |
| 54 | + type: 'array', |
| 55 | + message: `array of numbers: ${this.functionalArray()}`} |
| 56 | + ); |
| 57 | + |
| 58 | + // Stop after 3 |
| 59 | + this.functionalEvents().take(3).subscribe( |
| 60 | + result => this.eventService.add({ |
| 61 | + type: 'number stream', |
| 62 | + message: `stream of numbers: ${result}`} |
| 63 | + ) |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + ///////////////// |
| 68 | + |
| 69 | + /** |
| 70 | + * A `fromPromise` example that converts the `Promise` result |
| 71 | + * of the `fetch` API into an Observable of heroes. |
| 72 | + */ |
| 73 | + fetchHeroes(): Observable<Hero[]> { |
| 74 | + |
| 75 | + // #docregion fromPromise |
| 76 | + // JavaScript fetch returns a Promise |
| 77 | + let promise = fetch(this.heroesUrl) |
| 78 | + .then(resp => resp.json() as Promise<Hero[]>) |
| 79 | + .then(heroes => { console.log(heroes); return heroes; }); |
| 80 | + |
| 81 | + // return an Observable |
| 82 | + return Observable.fromPromise(promise); |
| 83 | + // #enddocregion fromPromise |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * A `toPromise` example that converts the `Observable` result |
| 88 | + * of the Angular `http` API into a Promise of heroes. |
| 89 | + */ |
| 90 | + getHeroes(): Promise<Hero[]> { |
| 91 | + |
| 92 | + // #docregion toPromise |
| 93 | + // Angular http.get returns an Observable |
| 94 | + let observable = this.http.get(this.heroesUrl) |
| 95 | + .map(resp => resp.json().data as Hero[]) |
| 96 | + .do(heroes => console.log(heroes)); |
| 97 | + |
| 98 | + // return a Promise |
| 99 | + return observable.toPromise(); |
| 100 | + // #enddocregion toPromise |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Call the fromPromise and toPromise example methods |
| 105 | + * and write their results to the EventAggregatorService |
| 106 | + * for display in AppComponent. |
| 107 | + */ |
| 108 | + callPromiseExamples() { |
| 109 | + |
| 110 | + this.fetchHeroes() |
| 111 | + .subscribe( |
| 112 | + heroes => this.eventService.add({type: 'fetch', message: 'fetched heroes'}), |
| 113 | + error => this.eventService.add({type: 'fetch', message: 'fetchHeroes failed'}) |
| 114 | + ); |
| 115 | + |
| 116 | + this.getHeroes() |
| 117 | + .then( |
| 118 | + heroes => this.eventService.add({type: 'get', message: 'got heroes'}), |
| 119 | + error => this.eventService.add({type: 'get', message: 'getHeroes failed'}) |
| 120 | + ); |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +// Fake the JavaScript fetch API (https://fetch.spec.whatwg.org/) because |
| 125 | +// don't want to add another polyfill for browsers that don't support fetch |
| 126 | +// and it's not important for this example. |
| 127 | +function fetch(url: string) { |
| 128 | + const heroes = new InMemoryDataService().createDb().heroes; |
| 129 | + const resp = { json: () => Promise.resolve(heroes) as Promise<any>}; |
| 130 | + return new Promise<typeof resp>(resolve => { |
| 131 | + setTimeout(() => resolve(resp), 500); // respond after half second |
| 132 | + }); |
| 133 | +} |
0 commit comments