Created
August 9, 2018 20:49
-
-
Save whisher/5af6bcddbe6f3fc923bdc5a66db87f94 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, BehaviorSubject } from 'rxjs'; | |
| import { pluck, distinctUntilChanged } from 'rxjs/operators'; | |
| import { User } from './auth/shared/services/auth/auth.service'; | |
| import { Meal } from './health/shared/services/meals/meals.service'; | |
| export interface State { | |
| user: User, | |
| meals: Meal[], | |
| [key: string]: any | |
| } | |
| const state: State = { | |
| user: undefined, | |
| meals: undefined, | |
| }; | |
| export class Store { | |
| private subject = new BehaviorSubject<State>(state); | |
| private store = this.subject.asObservable() | |
| .pipe( | |
| distinctUntilChanged() | |
| ); | |
| get value() { | |
| return this.subject.value; | |
| } | |
| select<T>(name: string): Observable<T> { | |
| return this.store | |
| .pipe( | |
| 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