Skip to content

Instantly share code, notes, and snippets.

@whisher
Created August 9, 2018 20:49
Show Gist options
  • Save whisher/5af6bcddbe6f3fc923bdc5a66db87f94 to your computer and use it in GitHub Desktop.
Save whisher/5af6bcddbe6f3fc923bdc5a66db87f94 to your computer and use it in GitHub Desktop.
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