Skip to content

Instantly share code, notes, and snippets.

@whisher
Created July 9, 2018 19:38
Show Gist options
  • Save whisher/97d211e54b290d255ee14482459585ad to your computer and use it in GitHub Desktop.
Save whisher/97d211e54b290d255ee14482459585ad to your computer and use it in GitHub Desktop.
import { Injectable } from '@angular/core';
import { Observable, BehaviorSubject } from 'rxjs';
import { distinctUntilChanged, pluck } from 'rxjs/operators';
import { Profile } from './admin/features/profile/models/profile';
export interface State {
profile: Profile
}
const state: State = {
profile: undefined
};
@Injectable({
providedIn: 'root'
})
export class AppStore {
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