-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathmodels.ts
More file actions
127 lines (101 loc) · 3.48 KB
/
models.ts
File metadata and controls
127 lines (101 loc) · 3.48 KB
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
import { MemoizedSelector } from '@ngrx/store';
export type Comparer<T> = (a: T, b: T) => number;
export type IdSelectorStr<T> = (model: T) => string;
export type IdSelectorNum<T> = (model: T) => number;
export type IdSelector<T> = IdSelectorStr<T> | IdSelectorNum<T>;
export interface DictionaryNum<T> {
[id: number]: T | undefined;
}
export abstract class Dictionary<T> implements DictionaryNum<T> {
[id: string]: T | undefined;
}
export interface UpdateStr<T> {
id: string;
changes: Partial<T>;
}
export interface UpdateNum<T> {
id: number;
changes: Partial<T>;
}
export type Update<T> = UpdateStr<T> | UpdateNum<T>;
export type Predicate<T> = (entity: T) => boolean;
export type EntityMap<T> = (entity: T) => T;
export interface EntityMapOneNum<T> {
id: number;
map: EntityMap<T>;
}
export interface EntityMapOneStr<T> {
id: string;
map: EntityMap<T>;
}
export type EntityMapOne<T> = EntityMapOneNum<T> | EntityMapOneStr<T>;
export interface EntityState<T> {
ids: string[] | number[];
entities: Dictionary<T>;
}
export interface EntityDefinition<T> {
selectId: IdSelector<T>;
sortComparer: false | Comparer<T>;
}
export interface EntityStateAdapter<T> {
addOne<S extends EntityState<T>>(entity: T, state: S): S;
addMany<S extends EntityState<T>>(entities: T[], state: S): S;
setAll<S extends EntityState<T>>(entities: T[], state: S): S;
setOne<S extends EntityState<T>>(entity: T, state: S): S;
setMany<S extends EntityState<T>>(entities: T[], state: S): S;
removeOne<S extends EntityState<T>>(key: string, state: S): S;
removeOne<S extends EntityState<T>>(key: number, state: S): S;
removeMany<S extends EntityState<T>>(keys: string[], state: S): S;
removeMany<S extends EntityState<T>>(keys: number[], state: S): S;
removeMany<S extends EntityState<T>>(predicate: Predicate<T>, state: S): S;
removeAll<S extends EntityState<T>>(state: S): S;
updateOne<S extends EntityState<T>>(update: Update<T>, state: S): S;
updateMany<S extends EntityState<T>>(updates: Update<T>[], state: S): S;
upsertOne<S extends EntityState<T>>(entity: T, state: S): S;
upsertMany<S extends EntityState<T>>(entities: T[], state: S): S;
mapOne<S extends EntityState<T>>(map: EntityMapOne<T>, state: S): S;
map<S extends EntityState<T>>(map: EntityMap<T>, state: S): S;
}
export type EntitySelectors<T, V> = {
selectIds: (state: V) => string[] | number[];
selectEntities: (state: V) => Dictionary<T>;
selectAll: (state: V) => T[];
selectTotal: (state: V) => number;
};
export type MemoizedEntitySelectors<T, V> = {
selectIds: MemoizedSelector<
V,
string[] | number[],
(entityState: EntityState<T>) => string[] | number[]
>;
selectEntities: MemoizedSelector<
V,
Dictionary<T>,
(entityState: EntityState<T>) => Dictionary<T>
>;
selectAll: MemoizedSelector<V, T[], (entityState: EntityState<T>) => T[]>;
selectTotal: MemoizedSelector<
V,
number,
(entityState: EntityState<T>) => number
>;
};
export interface EntityAdapter<
T,
IdType = string | number,
> extends EntityStateAdapter<T> {
selectId: IdType extends string
? IdSelectorStr<T>
: IdType extends number
? IdSelectorNum<T>
: unknown;
sortComparer: false | Comparer<T>;
getInitialState(): EntityState<T>;
getInitialState<S extends EntityState<T>>(
state: Omit<S, keyof EntityState<T>>
): S;
getSelectors(): EntitySelectors<T, EntityState<T>>;
getSelectors<V>(
selectState: (state: V) => EntityState<T>
): MemoizedEntitySelectors<T, V>;
}