|
1 | | -import type { UncheckedIndexedAccess } from '../uncheckedindexed' |
2 | | -import type { PayloadAction } from '../createAction' |
3 | | -import type { GetSelectorsOptions } from './state_selectors' |
4 | | -import type { CastAny, Id as Compute } from '../tsHelpers' |
5 | | - |
6 | | -/** |
7 | | - * @public |
8 | | - */ |
9 | | -export type EntityId = number | string |
10 | | - |
11 | | -/** |
12 | | - * @public |
13 | | - */ |
14 | | -export type Comparer<T> = (a: T, b: T) => number |
15 | | - |
16 | | -/** |
17 | | - * @public |
18 | | - */ |
19 | | -export type IdSelector<T, Id extends EntityId> = (model: T) => Id |
20 | | - |
21 | | -/** |
22 | | - * @public |
23 | | - */ |
24 | | -export type Update<T, Id extends EntityId> = { id: Id; changes: Partial<T> } |
25 | | - |
26 | | -/** |
27 | | - * @public |
28 | | - */ |
29 | | -export interface EntityState<T, Id extends EntityId> { |
30 | | - ids: Id[] |
31 | | - entities: Record<Id, T> |
32 | | -} |
33 | | - |
34 | | -/** |
35 | | - * @public |
36 | | - */ |
37 | | -export interface EntityDefinition<T, Id extends EntityId> { |
38 | | - selectId: IdSelector<T, Id> |
39 | | - sortComparer: false | Comparer<T> |
40 | | -} |
41 | | - |
42 | | -export type PreventAny<S, T, Id extends EntityId> = CastAny< |
43 | | - S, |
44 | | - EntityState<T, Id> |
45 | | -> |
46 | | - |
47 | | -/** |
48 | | - * @public |
49 | | - */ |
50 | | -export interface EntityStateAdapter<T, Id extends EntityId> { |
51 | | - addOne<S extends EntityState<T, Id>>( |
52 | | - state: PreventAny<S, T, Id>, |
53 | | - entity: T |
54 | | - ): S |
55 | | - addOne<S extends EntityState<T, Id>>( |
56 | | - state: PreventAny<S, T, Id>, |
57 | | - action: PayloadAction<T> |
58 | | - ): S |
59 | | - |
60 | | - addMany<S extends EntityState<T, Id>>( |
61 | | - state: PreventAny<S, T, Id>, |
62 | | - entities: readonly T[] | Record<Id, T> |
63 | | - ): S |
64 | | - addMany<S extends EntityState<T, Id>>( |
65 | | - state: PreventAny<S, T, Id>, |
66 | | - entities: PayloadAction<readonly T[] | Record<Id, T>> |
67 | | - ): S |
68 | | - |
69 | | - setOne<S extends EntityState<T, Id>>( |
70 | | - state: PreventAny<S, T, Id>, |
71 | | - entity: T |
72 | | - ): S |
73 | | - setOne<S extends EntityState<T, Id>>( |
74 | | - state: PreventAny<S, T, Id>, |
75 | | - action: PayloadAction<T> |
76 | | - ): S |
77 | | - setMany<S extends EntityState<T, Id>>( |
78 | | - state: PreventAny<S, T, Id>, |
79 | | - entities: readonly T[] | Record<Id, T> |
80 | | - ): S |
81 | | - setMany<S extends EntityState<T, Id>>( |
82 | | - state: PreventAny<S, T, Id>, |
83 | | - entities: PayloadAction<readonly T[] | Record<Id, T>> |
84 | | - ): S |
85 | | - setAll<S extends EntityState<T, Id>>( |
86 | | - state: PreventAny<S, T, Id>, |
87 | | - entities: readonly T[] | Record<Id, T> |
88 | | - ): S |
89 | | - setAll<S extends EntityState<T, Id>>( |
90 | | - state: PreventAny<S, T, Id>, |
91 | | - entities: PayloadAction<readonly T[] | Record<Id, T>> |
92 | | - ): S |
93 | | - |
94 | | - removeOne<S extends EntityState<T, Id>>( |
95 | | - state: PreventAny<S, T, Id>, |
96 | | - key: Id |
97 | | - ): S |
98 | | - removeOne<S extends EntityState<T, Id>>( |
99 | | - state: PreventAny<S, T, Id>, |
100 | | - key: PayloadAction<Id> |
101 | | - ): S |
102 | | - |
103 | | - removeMany<S extends EntityState<T, Id>>( |
104 | | - state: PreventAny<S, T, Id>, |
105 | | - keys: readonly Id[] |
106 | | - ): S |
107 | | - removeMany<S extends EntityState<T, Id>>( |
108 | | - state: PreventAny<S, T, Id>, |
109 | | - keys: PayloadAction<readonly Id[]> |
110 | | - ): S |
111 | | - |
112 | | - removeAll<S extends EntityState<T, Id>>(state: PreventAny<S, T, Id>): S |
113 | | - |
114 | | - updateOne<S extends EntityState<T, Id>>( |
115 | | - state: PreventAny<S, T, Id>, |
116 | | - update: Update<T, Id> |
117 | | - ): S |
118 | | - updateOne<S extends EntityState<T, Id>>( |
119 | | - state: PreventAny<S, T, Id>, |
120 | | - update: PayloadAction<Update<T, Id>> |
121 | | - ): S |
122 | | - |
123 | | - updateMany<S extends EntityState<T, Id>>( |
124 | | - state: PreventAny<S, T, Id>, |
125 | | - updates: ReadonlyArray<Update<T, Id>> |
126 | | - ): S |
127 | | - updateMany<S extends EntityState<T, Id>>( |
128 | | - state: PreventAny<S, T, Id>, |
129 | | - updates: PayloadAction<ReadonlyArray<Update<T, Id>>> |
130 | | - ): S |
131 | | - |
132 | | - upsertOne<S extends EntityState<T, Id>>( |
133 | | - state: PreventAny<S, T, Id>, |
134 | | - entity: T |
135 | | - ): S |
136 | | - upsertOne<S extends EntityState<T, Id>>( |
137 | | - state: PreventAny<S, T, Id>, |
138 | | - entity: PayloadAction<T> |
139 | | - ): S |
140 | | - |
141 | | - upsertMany<S extends EntityState<T, Id>>( |
142 | | - state: PreventAny<S, T, Id>, |
143 | | - entities: readonly T[] | Record<Id, T> |
144 | | - ): S |
145 | | - upsertMany<S extends EntityState<T, Id>>( |
146 | | - state: PreventAny<S, T, Id>, |
147 | | - entities: PayloadAction<readonly T[] | Record<Id, T>> |
148 | | - ): S |
149 | | -} |
150 | | - |
151 | | -/** |
152 | | - * @public |
153 | | - */ |
154 | | -export interface EntitySelectors<T, V, Id extends EntityId> { |
155 | | - selectIds: (state: V) => Id[] |
156 | | - selectEntities: (state: V) => Record<Id, T> |
157 | | - selectAll: (state: V) => T[] |
158 | | - selectTotal: (state: V) => number |
159 | | - selectById: (state: V, id: Id) => Compute<UncheckedIndexedAccess<T>> |
160 | | -} |
161 | | - |
162 | | -/** |
163 | | - * @public |
164 | | - */ |
165 | | -export interface EntityAdapter<T, Id extends EntityId> |
166 | | - extends EntityStateAdapter<T, Id> { |
167 | | - selectId: IdSelector<T, Id> |
168 | | - sortComparer: false | Comparer<T> |
169 | | - getInitialState(): EntityState<T, Id> |
170 | | - getInitialState<S extends object>(state: S): EntityState<T, Id> & S |
171 | | - getSelectors( |
172 | | - selectState?: undefined, |
173 | | - options?: GetSelectorsOptions |
174 | | - ): EntitySelectors<T, EntityState<T, Id>, Id> |
175 | | - getSelectors<V>( |
176 | | - selectState: (state: V) => EntityState<T, Id>, |
177 | | - options?: GetSelectorsOptions |
178 | | - ): EntitySelectors<T, V, Id> |
179 | | -} |
| 1 | +import type { UncheckedIndexedAccess } from '../uncheckedindexed' |
| 2 | +import type { Draft } from 'immer' |
| 3 | +import type { PayloadAction } from '../createAction' |
| 4 | +import type { GetSelectorsOptions } from './state_selectors' |
| 5 | +import type { CastAny, Id as Compute } from '../tsHelpers' |
| 6 | + |
| 7 | +/** |
| 8 | + * @public |
| 9 | + */ |
| 10 | +export type EntityId = number | string |
| 11 | + |
| 12 | +/** |
| 13 | + * @public |
| 14 | + */ |
| 15 | +export type Comparer<T> = (a: T, b: T) => number |
| 16 | + |
| 17 | +/** |
| 18 | + * @public |
| 19 | + */ |
| 20 | +export type IdSelector<T, Id extends EntityId> = (model: T) => Id |
| 21 | + |
| 22 | +/** |
| 23 | + * @public |
| 24 | + */ |
| 25 | +export type Update<T, Id extends EntityId> = { id: Id; changes: Partial<T> } |
| 26 | + |
| 27 | +/** |
| 28 | + * @public |
| 29 | + */ |
| 30 | +export interface EntityState<T, Id extends EntityId> { |
| 31 | + ids: Id[] |
| 32 | + entities: Record<Id, T> |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * @public |
| 37 | + */ |
| 38 | +export interface EntityDefinition<T, Id extends EntityId> { |
| 39 | + selectId: IdSelector<T, Id> |
| 40 | + sortComparer: false | Comparer<T> |
| 41 | +} |
| 42 | + |
| 43 | +export type PreventAny<S, T, Id extends EntityId> = CastAny< |
| 44 | + S, |
| 45 | + EntityState<T, Id> |
| 46 | + > |
| 47 | + |
| 48 | +export type DraftableEntityState<T, Id extends EntityId> = EntityState<T, Id> | Draft<EntityState<T, Id>> |
| 49 | + |
| 50 | +/** |
| 51 | + * @public |
| 52 | + */ |
| 53 | +export interface EntityStateAdapter<T, Id extends EntityId> { |
| 54 | + addOne<S extends DraftableEntityState<T, Id>>( |
| 55 | + state: PreventAny<S, T, Id>, |
| 56 | + entity: T |
| 57 | + ): S |
| 58 | + addOne<S extends DraftableEntityState<T, Id>>( |
| 59 | + state: PreventAny<S, T, Id>, |
| 60 | + action: PayloadAction<T> |
| 61 | + ): S |
| 62 | + |
| 63 | + addMany<S extends DraftableEntityState<T, Id>>( |
| 64 | + state: PreventAny<S, T, Id>, |
| 65 | + entities: readonly T[] | Record<Id, T> |
| 66 | + ): S |
| 67 | + addMany<S extends DraftableEntityState<T, Id>>( |
| 68 | + state: PreventAny<S, T, Id>, |
| 69 | + entities: PayloadAction<readonly T[] | Record<Id, T>> |
| 70 | + ): S |
| 71 | + |
| 72 | + setOne<S extends DraftableEntityState<T, Id>>( |
| 73 | + state: PreventAny<S, T, Id>, |
| 74 | + entity: T |
| 75 | + ): S |
| 76 | + setOne<S extends DraftableEntityState<T, Id>>( |
| 77 | + state: PreventAny<S, T, Id>, |
| 78 | + action: PayloadAction<T> |
| 79 | + ): S |
| 80 | + setMany<S extends DraftableEntityState<T, Id>>( |
| 81 | + state: PreventAny<S, T, Id>, |
| 82 | + entities: readonly T[] | Record<Id, T> |
| 83 | + ): S |
| 84 | + setMany<S extends DraftableEntityState<T, Id>>( |
| 85 | + state: PreventAny<S, T, Id>, |
| 86 | + entities: PayloadAction<readonly T[] | Record<Id, T>> |
| 87 | + ): S |
| 88 | + setAll<S extends DraftableEntityState<T, Id>>( |
| 89 | + state: PreventAny<S, T, Id>, |
| 90 | + entities: readonly T[] | Record<Id, T> |
| 91 | + ): S |
| 92 | + setAll<S extends DraftableEntityState<T, Id>>( |
| 93 | + state: PreventAny<S, T, Id>, |
| 94 | + entities: PayloadAction<readonly T[] | Record<Id, T>> |
| 95 | + ): S |
| 96 | + |
| 97 | + removeOne<S extends DraftableEntityState<T, Id>>( |
| 98 | + state: PreventAny<S, T, Id>, |
| 99 | + key: Id |
| 100 | + ): S |
| 101 | + removeOne<S extends DraftableEntityState<T, Id>>( |
| 102 | + state: PreventAny<S, T, Id>, |
| 103 | + key: PayloadAction<Id> |
| 104 | + ): S |
| 105 | + |
| 106 | + removeMany<S extends DraftableEntityState<T, Id>>( |
| 107 | + state: PreventAny<S, T, Id>, |
| 108 | + keys: readonly Id[] |
| 109 | + ): S |
| 110 | + removeMany<S extends DraftableEntityState<T, Id>>( |
| 111 | + state: PreventAny<S, T, Id>, |
| 112 | + keys: PayloadAction<readonly Id[]> |
| 113 | + ): S |
| 114 | + |
| 115 | + removeAll<S extends DraftableEntityState<T, Id>>(state: PreventAny<S, T, Id>): S |
| 116 | + |
| 117 | + updateOne<S extends DraftableEntityState<T, Id>>( |
| 118 | + state: PreventAny<S, T, Id>, |
| 119 | + update: Update<T, Id> |
| 120 | + ): S |
| 121 | + updateOne<S extends DraftableEntityState<T, Id>>( |
| 122 | + state: PreventAny<S, T, Id>, |
| 123 | + update: PayloadAction<Update<T, Id>> |
| 124 | + ): S |
| 125 | + |
| 126 | + updateMany<S extends DraftableEntityState<T, Id>>( |
| 127 | + state: PreventAny<S, T, Id>, |
| 128 | + updates: ReadonlyArray<Update<T, Id>> |
| 129 | + ): S |
| 130 | + updateMany<S extends DraftableEntityState<T, Id>>( |
| 131 | + state: PreventAny<S, T, Id>, |
| 132 | + updates: PayloadAction<ReadonlyArray<Update<T, Id>>> |
| 133 | + ): S |
| 134 | + |
| 135 | + upsertOne<S extends DraftableEntityState<T, Id>>( |
| 136 | + state: PreventAny<S, T, Id>, |
| 137 | + entity: T |
| 138 | + ): S |
| 139 | + upsertOne<S extends DraftableEntityState<T, Id>>( |
| 140 | + state: PreventAny<S, T, Id>, |
| 141 | + entity: PayloadAction<T> |
| 142 | + ): S |
| 143 | + |
| 144 | + upsertMany<S extends DraftableEntityState<T, Id>>( |
| 145 | + state: PreventAny<S, T, Id>, |
| 146 | + entities: readonly T[] | Record<Id, T> |
| 147 | + ): S |
| 148 | + upsertMany<S extends DraftableEntityState<T, Id>>( |
| 149 | + state: PreventAny<S, T, Id>, |
| 150 | + entities: PayloadAction<readonly T[] | Record<Id, T>> |
| 151 | + ): S |
| 152 | +} |
| 153 | + |
| 154 | +/** |
| 155 | + * @public |
| 156 | + */ |
| 157 | +export interface EntitySelectors<T, V, Id extends EntityId> { |
| 158 | + selectIds: (state: V) => Id[] |
| 159 | + selectEntities: (state: V) => Record<Id, T> |
| 160 | + selectAll: (state: V) => T[] |
| 161 | + selectTotal: (state: V) => number |
| 162 | + selectById: (state: V, id: Id) => Compute<UncheckedIndexedAccess<T>> |
| 163 | +} |
| 164 | + |
| 165 | +/** |
| 166 | + * @public |
| 167 | + */ |
| 168 | +export interface EntityAdapter<T, Id extends EntityId> |
| 169 | + extends EntityStateAdapter<T, Id> { |
| 170 | + selectId: IdSelector<T, Id> |
| 171 | + sortComparer: false | Comparer<T> |
| 172 | + getInitialState(): EntityState<T, Id> |
| 173 | + getInitialState<S extends object>(state: S): EntityState<T, Id> & S |
| 174 | + getSelectors( |
| 175 | + selectState?: undefined, |
| 176 | + options?: GetSelectorsOptions |
| 177 | + ): EntitySelectors<T, EntityState<T, Id>, Id> |
| 178 | + getSelectors<V>( |
| 179 | + selectState: (state: V) => EntityState<T, Id>, |
| 180 | + options?: GetSelectorsOptions |
| 181 | + ): EntitySelectors<T, V, Id> |
| 182 | +} |
0 commit comments