|
| 1 | +import { Injectable } from '@angular/core'; |
| 2 | +import { Http, Response } from '@angular/http'; |
| 3 | +import { Observable } from 'rxjs/Observable'; |
| 4 | +import { BehaviorSubject } from 'rxjs/BehaviorSubject'; |
| 5 | +import 'rxjs/add/operator/map'; |
| 6 | +import 'rxjs/add/operator/filter'; |
| 7 | + |
| 8 | +// Example of a custom Data Type for our Notes model |
| 9 | +export interface User { |
| 10 | + firstName: string; |
| 11 | + lastName: string; |
| 12 | + email: string; |
| 13 | + gender: string; |
| 14 | +} |
| 15 | + |
| 16 | +/** |
| 17 | + * Statefull service which provides an Observable-based API with Observable Data |
| 18 | + * |
| 19 | + * Main Features: |
| 20 | + * 1. Statefull: |
| 21 | + * - This service is statefull and exposes a state variable which is the Observable called user$ |
| 22 | + * |
| 23 | + * 2. Observable-based API: |
| 24 | + * - For any component which is using this service, the service ensures isolation |
| 25 | + * from how the data is retrieved - in this case, Angular HTTP service. |
| 26 | + * |
| 27 | + * When to use: |
| 28 | + * - when we want service act as a permanent storage for our fetched data |
| 29 | + * |
| 30 | + * |
| 31 | + * Example usage: |
| 32 | + * |
| 33 | + * @Component({ |
| 34 | + * selector: 'home', |
| 35 | + * template: ` |
| 36 | + * <div class='main'> |
| 37 | + * <user-detail [user]="user$ | async"></user-detail> |
| 38 | + * </div>` |
| 39 | + + }) |
| 40 | + * public class HomeComponent { |
| 41 | + * user$: Observable<User> |
| 42 | + * constructor(private userService: StatefullObservableDataService) { |
| 43 | + * this.user$ = userService.user$; |
| 44 | + * } |
| 45 | + * } |
| 46 | + * |
| 47 | + */ |
| 48 | +@Injectable() |
| 49 | +export class StatefullObservableDataService { |
| 50 | + static API_URL = `/api/v1/user/`; |
| 51 | + |
| 52 | + // Subject implements Observer and the Observable interfaces. |
| 53 | + // This means we can emit values and also use it as an Observable. |
| 54 | + // In the following case our subject is a Behavior Subject, meaning it stores the |
| 55 | + // last value emitted and sends it immediately upon subscription. |
| 56 | + private subject: BehaviorSubject<User> = new BehaviorSubject<User>(null); |
| 57 | + |
| 58 | + // Our Observable data which is exposed as a state variable. |
| 59 | + // This Observable is derived from a Subject, and a filter is |
| 60 | + // applied to prevent the null value from being propagated. |
| 61 | + public user$: Observable<User> = this.subject.asObservable().filter(user => !!user); |
| 62 | + |
| 63 | + constructor(private http: Http) { |
| 64 | + // The user data is retrieved from the backend using the HTTP service. |
| 65 | + // Once the data is available, we emit a new value of the subject. |
| 66 | + this.getUserInfo() |
| 67 | + .subscribe((user: User) => this.subject.next(user)); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Get user information from backend API service |
| 72 | + * |
| 73 | + * @return {Observable<User>} |
| 74 | + */ |
| 75 | + private getUserInfo(): Observable<User> { |
| 76 | + return this.http |
| 77 | + .get(StatefullObservableDataService.API_URL) |
| 78 | + .map((res: Response) => res.json()); |
| 79 | + } |
| 80 | + |
| 81 | +} |
0 commit comments