Created
October 8, 2020 10:08
-
-
Save LironHazan/b7b91683a5325b798ed60ec53dc57c58 to your computer and use it in GitHub Desktop.
rxjs_snippets_for_blog_post
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'; | |
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