Skip to content
This repository was archived by the owner on Dec 4, 2017. It is now read-only.

Commit 1875c8a

Browse files
committed
docs(rxjs): [WIP] Ward's contributions
1 parent b023ab0 commit 1875c8a

File tree

8 files changed

+435
-63
lines changed

8 files changed

+435
-63
lines changed

public/docs/_examples/rxjs/ts/src/app/app.component.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// #docregion
33
import { Component, OnInit } from '@angular/core';
44
import { EventAggregatorService } from './event-aggregator.service';
5+
import { ObservablePrinciples } from './observable-principles';
56

67
@Component({
78
selector: 'my-app',
@@ -19,12 +20,17 @@ import { EventAggregatorService } from './event-aggregator.service';
1920
`
2021
})
2122
export class AppComponent implements OnInit {
22-
constructor(private eventService: EventAggregatorService) {}
23+
constructor(
24+
private eventService: EventAggregatorService,
25+
private principles: ObservablePrinciples) {}
2326

2427
ngOnInit() {
2528
this.eventService.add({
2629
type: 'init',
2730
message: 'Application Initialized'
2831
});
32+
33+
this.principles.callFunctionalExamples();
34+
this.principles.callPromiseExamples();
2935
}
3036
}

public/docs/_examples/rxjs/ts/src/app/app.module.ts

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { MessageLogComponent } from './message-log.component';
1212
import { LoadingComponent } from './loading.component';
1313
import { AddHeroComponent } from './add-hero.component';
1414

15+
import { ObservablePrinciples } from './observable-principles';
1516
import { LoadingService } from './loading.service';
1617
import { HeroService } from './hero.service';
1718

@@ -40,6 +41,7 @@ import { InMemoryDataService } from './in-memory-data.service';
4041
AddHeroComponent
4142
],
4243
providers: [
44+
ObservablePrinciples,
4345
HeroService,
4446
LoadingService,
4547
EventAggregatorService
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[
2+
{"id": 1, "name": "Mr. Nice"},
3+
{"id": 2, "name": "Narco"},
4+
{"id": 3, "name": "Bombasto"},
5+
{"id": 4, "name": "Celeritas"},
6+
{"id": 5, "name": "Magneta"},
7+
{"id": 6, "name": "RubberMan"},
8+
{"id": 7, "name": "Dynama"},
9+
{"id": 8, "name": "Dr IQ"},
10+
{"id": 9, "name": "Magma"},
11+
{"id": 10, "name": "Tornado"}
12+
]
+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
// #docregion
21
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
32

43
import { AppModule } from './app/app.module';
54

6-
platformBrowserDynamic().bootstrapModule(AppModule);
5+
// #docregion promise
6+
platformBrowserDynamic().bootstrapModule(AppModule)
7+
.then(() => console.log('The app was bootstrapped.'));
8+
// #enddocregion promise

public/docs/ts/latest/guide/_data.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@
169169

170170
"rxjs": {
171171
"title": "RxJS in Angular",
172-
"intro": "Using Observables to manage application streams."
172+
"intro": "Using Observables to manage asynchronous application events."
173173
},
174174

175175
"security": {

public/docs/ts/latest/guide/change-log.jade

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ block includes
55
The Angular documentation is a living document with continuous improvements.
66
This log calls attention to recent significant changes.
77

8+
## NEW: _RxJS in Angular_ guide (2017-03-13)
9+
The new [_Rxjs in Angular_](rxjs.htm) guide explains why and how to use RxJS `Observables` to handle asynchronous application events.
10+
811
## NEW: Downloadable examples for each guide (2017-02-28)
912
Now you can download the sample code for any guide and run it locally.
1013
Look for the new download links next to the "live example" links.

0 commit comments

Comments
 (0)