Created
April 1, 2019 21:38
-
-
Save ova2/8b21f8166589ca9580a3b8f7db3f0df2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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