Skip to content

Instantly share code, notes, and snippets.

@ova2
Created April 1, 2019 21:38
Show Gist options
  • Save ova2/8b21f8166589ca9580a3b8f7db3f0df2 to your computer and use it in GitHub Desktop.
Save ova2/8b21f8166589ca9580a3b8f7db3f0df2 to your computer and use it in GitHub Desktop.
import { BehaviorSubject, Observable } from 'rxjs';
import { distinctUntilChanged, map } from 'rxjs/operators';
export abstract class Store<T> {
private _state$: BehaviorSubject<T>;
protected constructor(initialState: T) {
this._state$ = new BehaviorSubject(initialState);
}
public get state(): T {
return this._state$.getValue();
}
public set state(nextState: T) {
this._state$.next(nextState);
}
public select<S>(selectFn: (state: T) => S): Observable<S> {
return this._state$.pipe(
map(selectFn),
distinctUntilChanged()
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment