/
sorted_state_adapter.ts
210 lines (176 loc) · 6.79 KB
/
sorted_state_adapter.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
import {
EntityRelationState,
IdSelector,
Comparer,
EntityRelationStateAdapter,
Update,
EntityRelationMap, KeySelector,
} from './models';
import {createStateOperator, DidMutate} from './state_adapter';
import {createUnsortedStateAdapter} from './unsorted_state_adapter';
import {selectIdValue} from './utils';
export function createSortedStateAdapter<T>(
selectId: IdSelector<T>,
sort: Comparer<T>,
selectKey: KeySelector<T>
): EntityRelationStateAdapter<T>;
export function createSortedStateAdapter<T>(selectId: any, sort: any, selectKey: any): any {
type R = EntityRelationState<T>;
const {removeOne, removeMany, removeAll} = createUnsortedStateAdapter(
selectId,
selectKey
);
function addOneMutably(entity: T, state: R): DidMutate;
function addOneMutably(entity: any, state: any): DidMutate {
return addManyMutably([entity], state);
}
function addManyMutably(newModels: T[], state: R): DidMutate;
function addManyMutably(newModels: any[], state: any): DidMutate {
const models = newModels.filter(
model => !(selectIdValue(model, selectId) in state.entities)
);
if (models.length === 0) {
return DidMutate.None;
} else {
merge(models, state);
return DidMutate.Both;
}
}
function addAllMutably(models: T[], state: R): DidMutate;
function addAllMutably(models: any[], state: any): DidMutate {
state.entities = {};
state.ids = [];
addManyMutably(models, state);
return DidMutate.Both;
}
function updateOneMutably(update: Update<T>, state: R): DidMutate;
function updateOneMutably(update: any, state: any): DidMutate {
return updateManyMutably([update], state);
}
function takeUpdatedModel(models: T[], update: Update<T>, state: R): boolean;
function takeUpdatedModel(models: any[], update: any, state: any): boolean {
if (!(update.id in state.entities)) {
return false;
}
const original = state.entities[update.id];
const updated = Object.assign({}, original, update.changes);
const newKey = selectIdValue(updated, selectId);
delete state.entities[update.id];
models.push(updated);
return newKey !== update.id;
}
function updateManyMutably(updates: Update<T>[], state: R): DidMutate;
function updateManyMutably(updates: any[], state: any): DidMutate {
const models: T[] = [];
const didMutateIds =
updates.filter(update => takeUpdatedModel(models, update, state)).length >
0;
if (models.length === 0) {
return DidMutate.None;
} else {
const originalIds = state.ids;
const updatedIndexes: any[] = [];
state.ids = state.ids.filter((id: any, index: number) => {
if (id in state.entities) {
return true;
} else {
updatedIndexes.push(index);
return false;
}
});
merge(models, state);
if (
!didMutateIds &&
updatedIndexes.every((i: number) => state.ids[i] === originalIds[i])
) {
return DidMutate.EntitiesOnly;
} else {
return DidMutate.Both;
}
}
}
function mapMutably(map: EntityRelationMap<T>, state: R): DidMutate;
function mapMutably(updatesOrMap: any, state: any): DidMutate {
const updates: Update<T>[] = state.ids.reduce(
(changes: any[], id: string | number) => {
const change = updatesOrMap(state.entities[id]);
if (change !== state.entities[id]) {
changes.push({id, changes: change});
}
return changes;
},
[]
);
return updateManyMutably(updates, state);
}
function upsertOneMutably(entity: T, state: R): DidMutate;
function upsertOneMutably(entity: any, state: any): DidMutate {
return upsertManyMutably([entity], state);
}
function upsertManyMutably(entities: T[], state: R): DidMutate;
function upsertManyMutably(entities: any[], state: any): DidMutate {
const added: any[] = [];
const updated: any[] = [];
for (const entity of entities) {
const id = selectIdValue(entity, selectId);
if (id in state.entities) {
updated.push({id, changes: entity});
} else {
added.push(entity);
}
}
const didMutateByUpdated = updateManyMutably(updated, state);
const didMutateByAdded = addManyMutably(added, state);
switch (true) {
case didMutateByAdded === DidMutate.None &&
didMutateByUpdated === DidMutate.None:
return DidMutate.None;
case didMutateByAdded === DidMutate.Both ||
didMutateByUpdated === DidMutate.Both:
return DidMutate.Both;
default:
return DidMutate.EntitiesOnly;
}
}
function merge(models: T[], state: R): void;
function merge(models: any[], state: any): void {
models.sort(sort);
const ids: any[] = [];
let i = 0;
let j = 0;
while (i < models.length && j < state.ids.length) {
const model = models[i];
const modelId = selectIdValue(model, selectId);
const entityId = state.ids[j];
const entity = state.entities[entityId];
if (sort(model, entity) <= 0) {
ids.push(modelId);
i++;
} else {
ids.push(entityId);
j++;
}
}
if (i < models.length) {
state.ids = ids.concat(models.slice(i).map(selectId));
} else {
state.ids = ids.concat(state.ids.slice(j));
}
models.forEach((model, i) => {
state.entities[selectId(model)] = model;
});
}
return {
removeOne,
removeMany,
removeAll,
addOne: createStateOperator(addOneMutably),
updateOne: createStateOperator(updateOneMutably),
upsertOne: createStateOperator(upsertOneMutably),
addAll: createStateOperator(addAllMutably),
addMany: createStateOperator(addManyMutably),
updateMany: createStateOperator(updateManyMutably),
upsertMany: createStateOperator(upsertManyMutably),
map: createStateOperator(mapMutably),
};
}