Created
June 15, 2018 12:29
-
-
Save whisher/6c79487e256ee97223b2755de9905835 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import { Observable } from 'rxjs/Observable'; | |
| import { BehaviorSubject } from 'rxjs/BehaviorSubject'; | |
| import 'rxjs/add/operator/pluck'; | |
| import 'rxjs/add/operator/distinctUntilChanged'; | |
| import { User } from './auth/shared/services/auth/auth.service'; | |
| import { Meal } from './health/shared/services/meals/meals.service'; | |
| import { Workout } from './health/shared/services/workouts/workouts.service'; | |
| export interface State { | |
| user: User, | |
| meals: Meal[], | |
| workouts: Workout[], | |
| [key: string]: any | |
| } | |
| const state: State = { | |
| user: undefined, | |
| meals: undefined, | |
| workouts: undefined, | |
| }; | |
| export class Store { | |
| private subject = new BehaviorSubject<State>(state); | |
| private store = this.subject.asObservable().distinctUntilChanged(); | |
| get value() { | |
| return this.subject.value; | |
| } | |
| select<T>(name: string): Observable<T> { | |
| return this.store.pluck(name); | |
| } | |
| set(name: string, state: any) { | |
| this.subject.next({ ...this.value, [name]: state }); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment