Skip to content

Instantly share code, notes, and snippets.

@whisher
Created September 14, 2018 20:29
Show Gist options
  • Save whisher/941533ee84736d058aa1b5d5303a0bae to your computer and use it in GitHub Desktop.
Save whisher/941533ee84736d058aa1b5d5303a0bae to your computer and use it in GitHub Desktop.
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';
import { ScheduleItem } from './health/shared/services/schedule/schedule.service';
export interface State {
user: User,
meals: Meal[],
schedule: ScheduleItem[],
date: Date,
workouts: Workout[],
[key: string]: any
}
const state: State = {
user: undefined,
meals: undefined,
schedule: undefined,
date: 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