Skip to content

Instantly share code, notes, and snippets.

@splincode
Last active May 9, 2020 15:15
Show Gist options
  • Save splincode/980c4f4c25ea631a1a9480980b836e97 to your computer and use it in GitHub Desktop.
Save splincode/980c4f4c25ea631a1a9480980b836e97 to your computer and use it in GitHub Desktop.
import { BehaviorSubject, Observable } from 'rxjs';
import { Injectable } from '@angular/core';
@Injectable()
export abstract class Store<T> {
private _state$: BehaviorSubject<T>;
protected constructor(defaults: T) {
this.state$ = new BehaviorSubject(defaults);
}
public get state$(): Observable<T> {
return this._state$.asObservable();
}
public getState(): T {
return this._state$.getValue();
}
public setState(nextState: T): void {
this._state$.next(nextState);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment