-
-
Notifications
You must be signed in to change notification settings - Fork 2k
/
sorted_state_adapter.ts
122 lines (95 loc) · 2.78 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
import {
EntityState,
IdSelector,
Comparer,
Dictionary,
EntityStateAdapter,
Update,
} from './models';
import { createStateOperator } from './state_adapter';
import { createUnsortedStateAdapter } from './unsorted_state_adapter';
export function createSortedStateAdapter<T>(
selectId: IdSelector<T>,
sort: Comparer<T>
): EntityStateAdapter<T> {
type R = EntityState<T>;
const { removeOne, removeMany, removeAll } = createUnsortedStateAdapter(
selectId
);
function addOneMutably(entity: T, state: R): boolean {
return addManyMutably([entity], state);
}
function addManyMutably(newModels: T[], state: R): boolean {
const models = newModels.filter(
model => !(selectId(model) in state.entities)
);
return merge(models, state);
}
function addAllMutably(models: T[], state: R): boolean {
state.entities = {};
state.ids = [];
addManyMutably(models, state);
return true;
}
function updateOneMutably(update: Update<T>, state: R): boolean {
return updateManyMutably([update], state);
}
function takeUpdatedModel(models: T[], update: Update<T>, state: R): void {
if (!(update.id in state.entities)) {
return;
}
const original = state.entities[update.id];
const updated = Object.assign({}, original, update.changes);
delete state.entities[update.id];
models.push(updated);
}
function updateManyMutably(updates: Update<T>[], state: R): boolean {
const models: T[] = [];
updates.forEach(update => takeUpdatedModel(models, update, state));
if (models.length) {
state.ids = state.ids.filter(id => id in state.entities);
}
return merge(models, state);
}
function merge(models: T[], state: R): boolean {
if (models.length === 0) {
return false;
}
models.sort(sort);
const ids: string[] = [];
let i = 0;
let j = 0;
while (i < models.length && j < state.ids.length) {
const model = models[i];
const modelId = selectId(model);
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 true;
}
return {
removeOne,
removeMany,
removeAll,
addOne: createStateOperator(addOneMutably),
updateOne: createStateOperator(updateOneMutably),
addAll: createStateOperator(addAllMutably),
addMany: createStateOperator(addManyMutably),
updateMany: createStateOperator(updateManyMutably),
};
}