|
1 | | -import { inject, Injectable, Signal } from '@angular/core'; |
2 | | -import { exhaustMap, Observable, of } from 'rxjs'; |
3 | | -import { UpdateFlag } from '../models/store.options'; |
| 1 | +import { inject, Injectable, linkedSignal } from '@angular/core'; |
| 2 | +import { distinctUntilChanged, exhaustMap, filter, map, Observable, of } from 'rxjs'; |
| 3 | +import { isUpdateFlag, UpdateFlag } from '../models/store.options'; |
4 | 4 | import { IStoreState } from '../models/store.state'; |
5 | | -import { DefaultActions } from '../shared/store.enums'; |
6 | | -import { ActionKey, DeepPartial, DispatchArguments, DispatchResponse, State, StateActionNames, StateData, StateFormatter, StateKey } from '../shared/store.types'; |
| 5 | +import { ActionStatus, DefaultActions } from '../shared/store.enums'; |
| 6 | +import { ActionKey, DeepPartial, DispatchArguments, DispatchResponse, FacadeSelection, State, StateActionNames, StateData, StateFormatter, StateKey } from '../shared/store.types'; |
7 | 7 | import { isEmpty } from '../shared/store.utils'; |
8 | 8 | import { StoreDispatcher } from './store.dispatcher'; |
9 | 9 | import { StoreManager } from './store.manager'; |
10 | 10 |
|
11 | | - |
12 | | - |
13 | 11 | @Injectable({ providedIn: 'root' }) |
14 | 12 | export class StoreFacade<States extends readonly IStoreState[] = any, Keys extends string = StateKey<States>> { |
15 | | - cache: any = {}; |
16 | | - |
17 | | - manager = inject(StoreManager); |
18 | | - dispatcher = inject(StoreDispatcher); |
19 | | - |
20 | | - selectAsync<K extends Keys, S extends IStoreState = State<States, K>>(stateKey: K): Observable<StateData<S>> { |
21 | | - return this.select(stateKey, true); |
22 | | - } |
23 | | - |
24 | | - select<K extends Keys, S extends IStoreState = State<States, K>>(stateKey: K): StateData<S>; |
25 | | - select<K extends Keys, S extends IStoreState = State<States, K>>(stateKey: K, async: false): Signal<StateData<S>>; |
26 | | - select<K extends Keys, S extends IStoreState = State<States, K>>(stateKey: K, async: true): Observable<StateData<S>>; |
27 | | - select<K extends Keys, S extends IStoreState = State<States, K>>(stateKey: K, async: boolean | null = null) { |
28 | | - if (async === null) { |
29 | | - return this.manager.value(stateKey); |
30 | | - } else if (async) { |
31 | | - return this.manager.observable(stateKey); |
32 | | - } else { |
33 | | - return this.manager.signal<K>(stateKey); |
| 13 | + cache: any = {}; |
| 14 | + |
| 15 | + manager = inject(StoreManager); |
| 16 | + dispatcher = inject(StoreDispatcher); |
| 17 | + |
| 18 | + select<K extends Keys, S extends State<States, K>, D extends StateData<S>, P extends keyof D>(stateKey: K): FacadeSelection<false, D, ''>; |
| 19 | + select<K extends Keys, S extends State<States, K>, D extends StateData<S>, P extends keyof D>(stateKey: K, arg1: false): FacadeSelection<false, D, ''>; |
| 20 | + select<K extends Keys, S extends State<States, K>, D extends StateData<S>, P extends keyof D>(stateKey: K, arg1: true): FacadeSelection<true, D, ''>; |
| 21 | + select<K extends Keys, S extends State<States, K>, D extends StateData<S>, P extends keyof D>(stateKey: K, arg1: P): FacadeSelection<false, D, P>; |
| 22 | + select<K extends Keys, S extends State<States, K>, D extends StateData<S>, P extends keyof D>(stateKey: K, arg1: P, arg2: false): FacadeSelection<false, D, P>; |
| 23 | + select<K extends Keys, S extends State<States, K>, D extends StateData<S>, P extends keyof D>(stateKey: K, arg1: P, arg2: true): FacadeSelection<true, D, P>; |
| 24 | + select(stateKey: string, arg1?: string | boolean, arg2?: boolean) { |
| 25 | + let state = this.manager.value<any>(stateKey); |
| 26 | + const propertKey = typeof arg1 === 'string' && arg1; |
| 27 | + const full = arg1 === true || arg2 === true; |
| 28 | + |
| 29 | + if (full) { |
| 30 | + state = { |
| 31 | + ...state, |
| 32 | + asObservable: () => this.manager.observable(stateKey), |
| 33 | + asSignal: () => this.manager.signal(stateKey), |
| 34 | + }; |
| 35 | + } |
| 36 | + |
| 37 | + if (propertKey) { |
| 38 | + const value = state[propertKey]; |
| 39 | + if (full) { |
| 40 | + return { |
| 41 | + value, |
| 42 | + asObservable: () => |
| 43 | + state.asObservable().pipe( |
| 44 | + map((data: any) => data?.[arg1]), |
| 45 | + filter(value => !!(value ?? false)), |
| 46 | + distinctUntilChanged(), |
| 47 | + ), |
| 48 | + asSignal: () => linkedSignal(() => state.asSignal()()[arg1]), |
| 49 | + } as any; |
| 50 | + } |
| 51 | + return value; |
| 52 | + } |
| 53 | + return state; |
| 54 | + } |
| 55 | + |
| 56 | + dispatch<K extends Keys, S extends IStoreState = State<States, K>, N extends string = StateActionNames<S>>( |
| 57 | + stateKey: K, |
| 58 | + actionKey: ActionKey<S, N>, |
| 59 | + ...[payload, flag]: DispatchArguments<S, N> |
| 60 | + ): Observable<DispatchResponse<S, N>> { |
| 61 | + const action = this.dispatcher.dispatch(stateKey, actionKey, payload, flag); |
| 62 | + return this.manager.observable(stateKey, action); |
| 63 | + } |
| 64 | + |
| 65 | + on<K extends Keys, S extends IStoreState = State<States, K>, N extends string = StateActionNames<S>>(stateKey: K, actionKey: ActionKey<S, N>, status = ActionStatus.SUCCESS, observe?: 'state' | 'action'): Observable<DispatchResponse<S, N>> { |
| 66 | + return this.manager.listen(stateKey, actionKey, status, observe); |
| 67 | + } |
| 68 | + |
| 69 | + set<K extends Keys, S extends State<States, K>, D extends StateData<S>, P extends keyof D>(stateKey: K, arg1: P | DeepPartial<D>, arg2?: DeepPartial<D>[P] | UpdateFlag, arg3?: UpdateFlag) { |
| 70 | + const payload = (typeof arg1 === 'string' ? { [arg1]: arg2 } : arg1) as DeepPartial<D>; |
| 71 | + const flag = isUpdateFlag(arg2) ? arg2 : arg3; |
| 72 | + return this.dispatch(stateKey, DefaultActions.SET, payload, flag); |
34 | 73 | } |
35 | | - } |
36 | | - |
37 | | - dispatch<K extends Keys, S extends IStoreState = State<States, K>, N extends string = StateActionNames<S>>(stateKey: K, actionKey: ActionKey<S, N>, ...[payload, flag]: DispatchArguments<S, N>): Observable<DispatchResponse<S, N>> { |
38 | | - const action = this.dispatcher.dispatch(stateKey, actionKey, payload, flag); |
39 | | - return this.manager.observable(stateKey, action); |
40 | | - } |
41 | | - |
42 | | - get<K extends Keys>(stateKey: K) { |
43 | | - return this.dispatch(stateKey, DefaultActions.GET); |
44 | | - } |
45 | | - |
46 | | - set<K extends Keys, S extends State<States, K>>(stateKey: K, payload: DeepPartial<StateData<S>>, flag?: UpdateFlag) { |
47 | | - return this.dispatch(stateKey, DefaultActions.SET, payload, flag); |
48 | | - } |
49 | | - |
50 | | - unset<K extends Keys>(stateKey: K) { |
51 | | - return this.dispatch(stateKey, DefaultActions.UNSET); |
52 | | - } |
53 | | - |
54 | | - extend<K extends Keys, S extends IStoreState = State<States, K>>(stateKey: K, payload: DeepPartial<StateData<S>>, flag?: UpdateFlag) { |
55 | | - return this.dispatch(stateKey, DefaultActions.EXTEND, payload, flag); |
56 | | - } |
57 | | - |
58 | | - init<K extends Keys, S extends IStoreState = State<States, K>>(stateKey: K, getter: Observable<StateData<S>>, formatter: StateFormatter<S>, force = false) { |
59 | | - formatter = formatter || ((payload: StateData<S>) => payload); |
60 | | - const stateData = this.select(stateKey); |
61 | | - if (!isEmpty(stateData) && !force) { |
62 | | - getter = of(stateData); |
| 74 | + |
| 75 | + unset<K extends Keys>(stateKey: K) { |
| 76 | + return this.dispatch(stateKey, DefaultActions.UNSET); |
| 77 | + } |
| 78 | + |
| 79 | + extend<K extends Keys, S extends IStoreState = State<States, K>>(stateKey: K, payload: DeepPartial<StateData<S>>, flag?: UpdateFlag) { |
| 80 | + return this.dispatch(stateKey, DefaultActions.EXTEND, payload, flag); |
| 81 | + } |
| 82 | + |
| 83 | + init<K extends Keys, S extends IStoreState = State<States, K>>(stateKey: K, getter: Observable<StateData<S>>, formatter?: StateFormatter<S>, force = false) { |
| 84 | + formatter = formatter || ((payload: StateData<S>) => payload); |
| 85 | + const stateData = this.select(stateKey); |
| 86 | + if (!isEmpty(stateData) && !force) { |
| 87 | + getter = of(stateData); |
| 88 | + } |
| 89 | + |
| 90 | + return getter.pipe( |
| 91 | + exhaustMap((payload: DeepPartial<StateData<S>>) => { |
| 92 | + return this.set(stateKey, formatter(payload as any)); |
| 93 | + }), |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + clear<K extends Keys>(exclude: K[] = []) { |
| 98 | + Object.keys(this.dispatcher.states).forEach(stateKey => { |
| 99 | + if (exclude.includes(stateKey as K)) { |
| 100 | + this.cache[stateKey as K] = this.select(stateKey as Keys); |
| 101 | + } else { |
| 102 | + this.unset(stateKey as K); |
| 103 | + } |
| 104 | + }); |
63 | 105 | } |
64 | 106 |
|
65 | | - return getter.pipe(exhaustMap((payload: DeepPartial<StateData<S>>) => { |
66 | | - return this.set(stateKey, formatter(payload)); |
67 | | - })); |
68 | | - } |
69 | | - |
70 | | - clear<K extends Keys>(exclude: K[] = []) { |
71 | | - Object.keys(this.dispatcher.states).forEach((stateKey) => { |
72 | | - if (exclude.includes(stateKey as K)) { |
73 | | - this.cache[stateKey as K] = this.select(stateKey as Keys); |
74 | | - } else { |
75 | | - this.unset(stateKey as K); |
76 | | - } |
77 | | - }); |
78 | | - } |
79 | | - |
80 | | - restore() { |
81 | | - Object.keys(this.cache).forEach((stateKey) => { |
82 | | - this.set(stateKey as Keys, this.cache[stateKey]); |
83 | | - delete this.cache[stateKey]; |
84 | | - }); |
85 | | - } |
| 107 | + restore() { |
| 108 | + Object.keys(this.cache).forEach(stateKey => { |
| 109 | + this.set(stateKey as Keys, this.cache[stateKey]); |
| 110 | + delete this.cache[stateKey]; |
| 111 | + }); |
| 112 | + } |
86 | 113 | } |
87 | 114 |
|
88 | 115 | export { StoreFacade as _StoreFacade }; |
|
0 commit comments