-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathmodels.ts
248 lines (225 loc) · 6.3 KB
/
models.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import type { UncheckedIndexedAccess } from '../uncheckedindexed'
import type { Draft } from 'immer'
import type { PayloadAction } from '../createAction'
import type { GetSelectorsOptions } from './state_selectors'
import type { CastAny, Id } from '../tsHelpers'
import type { CaseReducerDefinition } from '../createSlice'
import type { CaseReducer } from '../createReducer'
/**
* @public
*/
export type EntityId = number | string
/**
* @public
*/
export type Comparer<T> = (a: T, b: T) => number
/**
* @public
*/
export type IdSelector<T, Id extends EntityId> = (model: T) => Id
/**
* @public
*/
export type Update<T, Id extends EntityId> = { id: Id; changes: Partial<T> }
/**
* @public
*/
export interface EntityState<T, Id extends EntityId> {
ids: Id[]
entities: Record<Id, T>
}
/**
* @public
*/
export interface EntityAdapterOptions<T, Id extends EntityId> {
selectId?: IdSelector<T, Id>
sortComparer?: false | Comparer<T>
}
export type PreventAny<S, T, Id extends EntityId> = CastAny<
S,
EntityState<T, Id>
>
export type DraftableEntityState<T, Id extends EntityId> =
| EntityState<T, Id>
| Draft<EntityState<T, Id>>
/**
* @public
*/
export interface EntityStateAdapter<T, Id extends EntityId> {
addOne<S extends DraftableEntityState<T, Id>>(
state: PreventAny<S, T, Id>,
entity: T,
): S
addOne<S extends DraftableEntityState<T, Id>>(
state: PreventAny<S, T, Id>,
action: PayloadAction<T>,
): S
addMany<S extends DraftableEntityState<T, Id>>(
state: PreventAny<S, T, Id>,
entities: readonly T[] | Record<Id, T>,
): S
addMany<S extends DraftableEntityState<T, Id>>(
state: PreventAny<S, T, Id>,
entities: PayloadAction<readonly T[] | Record<Id, T>>,
): S
setOne<S extends DraftableEntityState<T, Id>>(
state: PreventAny<S, T, Id>,
entity: T,
): S
setOne<S extends DraftableEntityState<T, Id>>(
state: PreventAny<S, T, Id>,
action: PayloadAction<T>,
): S
setMany<S extends DraftableEntityState<T, Id>>(
state: PreventAny<S, T, Id>,
entities: readonly T[] | Record<Id, T>,
): S
setMany<S extends DraftableEntityState<T, Id>>(
state: PreventAny<S, T, Id>,
entities: PayloadAction<readonly T[] | Record<Id, T>>,
): S
setAll<S extends DraftableEntityState<T, Id>>(
state: PreventAny<S, T, Id>,
entities: readonly T[] | Record<Id, T>,
): S
setAll<S extends DraftableEntityState<T, Id>>(
state: PreventAny<S, T, Id>,
entities: PayloadAction<readonly T[] | Record<Id, T>>,
): S
removeOne<S extends DraftableEntityState<T, Id>>(
state: PreventAny<S, T, Id>,
key: Id,
): S
removeOne<S extends DraftableEntityState<T, Id>>(
state: PreventAny<S, T, Id>,
key: PayloadAction<Id>,
): S
removeMany<S extends DraftableEntityState<T, Id>>(
state: PreventAny<S, T, Id>,
keys: readonly Id[],
): S
removeMany<S extends DraftableEntityState<T, Id>>(
state: PreventAny<S, T, Id>,
keys: PayloadAction<readonly Id[]>,
): S
removeAll<S extends DraftableEntityState<T, Id>>(
state: PreventAny<S, T, Id>,
): S
updateOne<S extends DraftableEntityState<T, Id>>(
state: PreventAny<S, T, Id>,
update: Update<T, Id>,
): S
updateOne<S extends DraftableEntityState<T, Id>>(
state: PreventAny<S, T, Id>,
update: PayloadAction<Update<T, Id>>,
): S
updateMany<S extends DraftableEntityState<T, Id>>(
state: PreventAny<S, T, Id>,
updates: ReadonlyArray<Update<T, Id>>,
): S
updateMany<S extends DraftableEntityState<T, Id>>(
state: PreventAny<S, T, Id>,
updates: PayloadAction<ReadonlyArray<Update<T, Id>>>,
): S
upsertOne<S extends DraftableEntityState<T, Id>>(
state: PreventAny<S, T, Id>,
entity: T,
): S
upsertOne<S extends DraftableEntityState<T, Id>>(
state: PreventAny<S, T, Id>,
entity: PayloadAction<T>,
): S
upsertMany<S extends DraftableEntityState<T, Id>>(
state: PreventAny<S, T, Id>,
entities: readonly T[] | Record<Id, T>,
): S
upsertMany<S extends DraftableEntityState<T, Id>>(
state: PreventAny<S, T, Id>,
entities: PayloadAction<readonly T[] | Record<Id, T>>,
): S
}
/**
* @public
*/
export type EntitySelectors<
T,
V,
IdType extends EntityId,
Single extends string = '',
Plural extends string = DefaultPlural<Single>,
> = Id<
{
[K in `select${Capitalize<Single>}Ids`]: (state: V) => IdType[]
} & {
[K in `select${Capitalize<Single>}Entities`]: (
state: V,
) => Record<IdType, T>
} & {
[K in `selectAll${Capitalize<Plural>}`]: (state: V) => T[]
} & {
[K in `selectTotal${Capitalize<Plural>}`]: (state: V) => number
} & {
[K in `select${Capitalize<Single>}ById`]: (
state: V,
id: IdType,
) => Id<UncheckedIndexedAccess<T>>
}
>
export type DefaultPlural<Single extends string> = Single extends ''
? ''
: `${Single}s`
export type EntityReducers<
T,
Id extends EntityId,
State = EntityState<T, Id>,
Single extends string = '',
Plural extends string = DefaultPlural<Single>,
> = {
[K in keyof EntityStateAdapter<
T,
Id
> as `${K}${Capitalize<K extends `${string}One` ? Single : Plural>}`]: EntityStateAdapter<
T,
Id
>[K] extends (state: any) => any
? CaseReducerDefinition<State, PayloadAction>
: EntityStateAdapter<T, Id>[K] extends CaseReducer<any, infer A>
? CaseReducerDefinition<State, A>
: never
}
/**
* @public
*/
export interface EntityStateFactory<T, Id extends EntityId> {
getInitialState(
state?: undefined,
entities?: Record<Id, T> | readonly T[],
): EntityState<T, Id>
getInitialState<S extends object>(
state: S,
entities?: Record<Id, T> | readonly T[],
): EntityState<T, Id> & S
}
/**
* @public
*/
export interface EntityAdapter<T, Id extends EntityId>
extends EntityStateAdapter<T, Id>,
EntityStateFactory<T, Id>,
Required<EntityAdapterOptions<T, Id>> {
getSelectors<
Single extends string = '',
Plural extends string = DefaultPlural<Single>,
>(
selectState?: undefined,
options?: GetSelectorsOptions<Single, Plural>,
): EntitySelectors<T, EntityState<T, Id>, Id, Single, Plural>
getSelectors<
V,
Single extends string = '',
Plural extends string = DefaultPlural<Single>,
>(
selectState: (state: V) => EntityState<T, Id>,
options?: GetSelectorsOptions<Single, Plural>,
): EntitySelectors<T, V, Id, Single, Plural>
}