Skip to content

Instantly share code, notes, and snippets.

@LironHazan
Created October 8, 2020 10:08
Show Gist options
  • Save LironHazan/b7b91683a5325b798ed60ec53dc57c58 to your computer and use it in GitHub Desktop.
Save LironHazan/b7b91683a5325b798ed60ec53dc57c58 to your computer and use it in GitHub Desktop.
rxjs_snippets_for_blog_post
import {BehaviorSubject, Observable} from 'rxjs';
export class StoreService<T> {
private readonly state$: BehaviorSubject<T>;
protected get state(): T {
return this.state$.getValue();
}
constructor(initialState: T) {
this.state$ = new BehaviorSubject<T>(initialState);
}
protected getState(): Observable<T> {
return this.state$;
}
protected setState(newState: Partial<T>): void {
this.state$.next({
...this.state,
...newState,
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment