-
-
Save kuncevic/8b8e1a9a8a6a466830e52568ce6b1026 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, of } from 'rxjs'; | |
import { distinctUntilChanged, concatMap, map, take } from 'rxjs/operators'; | |
export class StateDef<T> extends BehaviorSubject<T> { | |
setState(setter: (state: T) => T) { | |
this.pipe(take(1)).subscribe(state => { | |
super.next(setter(state)); | |
}); | |
} | |
select<R>(selector: (state?: T) => R) { | |
return this.pipe(map(selector), distinctUntilChanged()); | |
} | |
selectOnce<R>(selector: (state?: T) => R) { | |
return this.select(selector).pipe(take(1)); | |
} | |
next() { | |
throw new Error('Nice Try, Use setState instead of next.'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment