1 | import { BehaviorSubject, queueScheduler, Observable } from 'rxjs';
|
2 | import { observeOn, scan, map, distinctUntilChanged } from 'rxjs/operators';
|
3 |
|
4 | class MiniState extends BehaviorSubject {
|
5 | constructor(_initialState, actionsDispatcher$, reducer) {
|
6 | super(_initialState);
|
7 | const actionInQueue$ = actionsDispatcher$.pipe(observeOn(queueScheduler));
|
8 | const state$ = actionInQueue$.pipe(scan((state, action) => {
|
9 | if (!action) {
|
10 | return state;
|
11 | }
|
12 | return reducer(state, action);
|
13 | }, _initialState));
|
14 | state$.subscribe((value) => this.next(value));
|
15 | }
|
16 | }
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | class MiniStore extends Observable {
|
22 | constructor(_dispatcher,
|
23 | // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
24 | _reducer,
|
25 | // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
26 | state$) {
|
27 | super();
|
28 | this._dispatcher = _dispatcher;
|
29 | this._reducer = _reducer;
|
30 | this.source = state$;
|
31 | }
|
32 | select(pathOrMapFn) {
|
33 |
|
34 |
|
35 | const mapped$ = this.source?.pipe(map(pathOrMapFn)) || new Observable().pipe(map(pathOrMapFn));
|
36 | return mapped$.pipe(distinctUntilChanged());
|
37 | }
|
38 | lift(operator) {
|
39 | const store = new MiniStore(this._dispatcher, this._reducer, this);
|
40 | store.operator = operator;
|
41 | return store;
|
42 | }
|
43 | dispatch(action) {
|
44 | this._dispatcher.next(action);
|
45 | }
|
46 | next(action) {
|
47 | this._dispatcher.next(action);
|
48 | }
|
49 |
|
50 | error(err) {
|
51 | this._dispatcher.error(err);
|
52 | }
|
53 | complete() {
|
54 |
|
55 | }
|
56 | }
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 | export { MiniState, MiniStore };
|
63 |
|