This repository has been archived by the owner on Dec 4, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 880
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cf36dc9
commit 394b494
Showing
8 changed files
with
283 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
public/docs/_examples/rxjs/ts/src/app/hero-list.component.2.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// #docplaster | ||
// #docregion | ||
// #docregion retry-operator | ||
import 'rxjs/add/operator/retry'; | ||
// #enddocregion retry-operator | ||
import 'rxjs/add/observable/of'; | ||
// #docregion failed-heroes | ||
import { Component, OnInit } from '@angular/core'; | ||
import { Observable } from 'rxjs/Observable'; | ||
|
||
import { HeroService } from './hero.service'; | ||
import { Hero } from './hero'; | ||
|
||
@Component({ | ||
template: ` | ||
<h2>HEROES</h2> | ||
<ul class="items"> | ||
<li *ngFor="let hero of heroes$ | async"> | ||
<span class="badge">{{ hero.id }}</span> {{ hero.name }} | ||
</li> | ||
</ul> | ||
` | ||
}) | ||
export class HeroListComponent implements OnInit { | ||
heroes$: Observable<Hero[]>; | ||
|
||
constructor( | ||
private service: HeroService | ||
) {} | ||
|
||
ngOnInit() { | ||
// #docregion failed-heroes | ||
this.heroes$ = this.service.getFailedHeroes() | ||
// #enddocregion failed-heroes | ||
.catch((error: any) => { | ||
console.log(`An error occurred: ${error}`); | ||
|
||
return Observable.of([]); | ||
}); | ||
// #docregion failed-heroes | ||
} | ||
} | ||
// #enddocregion |
40 changes: 40 additions & 0 deletions
40
public/docs/_examples/rxjs/ts/src/app/hero-list.component.3.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// #docplaster | ||
// #docregion | ||
// #docregion retry-operator | ||
import 'rxjs/add/operator/retry'; | ||
// #enddocregion retry-operator | ||
import 'rxjs/add/observable/of'; | ||
import { Component, OnInit } from '@angular/core'; | ||
import { Observable } from 'rxjs/Observable'; | ||
|
||
import { HeroService } from './hero.service'; | ||
import { Hero } from './hero'; | ||
|
||
@Component({ | ||
template: ` | ||
<h2>HEROES</h2> | ||
<ul class="items"> | ||
<li *ngFor="let hero of heroes$ | async"> | ||
<span class="badge">{{ hero.id }}</span> {{ hero.name }} | ||
</li> | ||
</ul> | ||
` | ||
}) | ||
export class HeroListComponent implements OnInit { | ||
heroes$: Observable<Hero[]>; | ||
|
||
constructor( | ||
private service: HeroService | ||
) {} | ||
|
||
ngOnInit() { | ||
this.heroes$ = this.service.getHeroes() | ||
.retry(3) | ||
.catch(error => { | ||
console.log(`An error occurred: ${error}`); | ||
|
||
return Observable.of([]); | ||
}); | ||
} | ||
} | ||
// #enddocregion |
52 changes: 52 additions & 0 deletions
52
public/docs/_examples/rxjs/ts/src/app/hero-list.component.4.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// #docplaster | ||
// #docregion | ||
// #docregion retry-when-operator | ||
import 'rxjs/add/operator/retryWhen'; | ||
// #enddocregion retry-when-operator | ||
import 'rxjs/add/observable/of'; | ||
import { Component, OnInit } from '@angular/core'; | ||
import { Observable } from 'rxjs/Observable'; | ||
|
||
import { HeroService } from './hero.service'; | ||
import { Hero } from './hero'; | ||
|
||
@Component({ | ||
template: ` | ||
<h2>HEROES</h2> | ||
<ul class="items"> | ||
<li *ngFor="let hero of heroes$ | async"> | ||
<span class="badge">{{ hero.id }}</span> {{ hero.name }} | ||
</li> | ||
</ul> | ||
` | ||
}) | ||
export class HeroListComponent implements OnInit { | ||
heroes$: Observable<Hero[]>; | ||
|
||
constructor( | ||
private service: HeroService | ||
) {} | ||
|
||
ngOnInit() { | ||
this.heroes$ = this.service.getFailedHeroes() | ||
.retryWhen((errors: any) => { | ||
return errors.scan((errorCount, err) => { | ||
if (errorCount >= 2) { | ||
throw err; | ||
} | ||
|
||
if (err.status !== 500) { | ||
return errorCount; | ||
} else { | ||
return errorCount + 1; | ||
} | ||
}, 0); | ||
}) | ||
.catch(error => { | ||
console.log(`An error occurred: ${error}`); | ||
|
||
return Observable.of([]); | ||
}); | ||
} | ||
} | ||
// #enddocregion |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// #docplaster | ||
// #docregion | ||
import 'rxjs/add/operator/map'; | ||
import 'rxjs/add/operator/delay'; | ||
import 'rxjs/add/operator/catch'; | ||
import { Injectable } from '@angular/core'; | ||
import { Http, Response, Headers } from '@angular/http'; | ||
import { Observable } from 'rxjs/Observable'; | ||
|
||
import { Hero } from './hero'; | ||
import { ApiError, ApiErrorHandlerService } from './api-error-handler.service'; | ||
|
||
@Injectable() | ||
export class HeroService { | ||
private headers = new Headers({'Content-Type': 'application/json'}); | ||
private heroesUrl = 'api/heroes'; | ||
|
||
constructor( | ||
private http: Http, | ||
private errorHandler: ApiErrorHandlerService | ||
) {} | ||
|
||
addHero(hero: Hero) { | ||
return this.http.post(this.heroesUrl, JSON.stringify({ name: hero.name }), { headers: this.headers }); | ||
} | ||
|
||
getHeroes(): Observable<Hero[]> { | ||
return this.http.get(this.heroesUrl) | ||
.map(response => response.json().data as Hero[]); | ||
} | ||
|
||
getFailedHeroes(): Observable<any> { | ||
return this.http.get(`${this.heroesUrl}/failed`) | ||
} | ||
|
||
getHero(id: number): Observable<Hero> { | ||
const url = `${this.heroesUrl}/${id}`; | ||
return this.http.get(url) | ||
.map(response => response.json().data as Hero); | ||
} | ||
|
||
search(term: string): Observable<Hero[]> { | ||
return this.http | ||
.get(`${this.heroesUrl}/?name=${term}`) | ||
.map((r: Response) => r.json().data as Hero[]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.