-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
sorted_state_adapter.ts
150 lines (122 loc) · 3.46 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
import {
EntityState,
IdSelector,
Comparer,
EntityStateAdapter,
Update,
EntityId
} from './models'
import { createStateOperator } from './state_adapter'
import { createUnsortedStateAdapter } from './unsorted_state_adapter'
import {
selectIdValue,
ensureEntitiesArray,
splitAddedUpdatedEntities
} from './utils'
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): void {
return addManyMutably([entity], state)
}
function addManyMutably(
newEntities: T[] | Record<EntityId, T>,
state: R
): void {
newEntities = ensureEntitiesArray(newEntities)
const models = newEntities.filter(
model => !(selectIdValue(model, selectId) in state.entities)
)
if (models.length !== 0) {
merge(models, state)
}
}
function setAllMutably(
newEntities: T[] | Record<EntityId, T>,
state: R
): void {
newEntities = ensureEntitiesArray(newEntities)
state.entities = {}
state.ids = []
addManyMutably(newEntities, state)
}
function updateOneMutably(update: Update<T>, state: R): void {
return updateManyMutably([update], state)
}
function takeUpdatedModel(models: T[], update: Update<T>, state: R): 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): void {
const models: T[] = []
updates.forEach(update => takeUpdatedModel(models, update, state))
if (models.length !== 0) {
merge(models, state)
}
}
function upsertOneMutably(entity: T, state: R): void {
return upsertManyMutably([entity], state)
}
function upsertManyMutably(
newEntities: T[] | Record<EntityId, T>,
state: R
): void {
const [added, updated] = splitAddedUpdatedEntities<T>(
newEntities,
selectId,
state
)
updateManyMutably(updated, state)
addManyMutably(added, state)
}
function areArraysEqual(a: unknown[], b: unknown[]) {
if (a.length !== b.length) {
return false
}
for (let i = 0; i < a.length && i < b.length; i++) {
if (a[i] === b[i]) {
continue
}
return false
}
return true
}
function merge(models: T[], state: R): void {
models.sort(sort)
// Insert/overwrite all new/updated
models.forEach(model => {
state.entities[selectId(model)] = model
})
const allEntities = Object.values(state.entities) as T[]
allEntities.sort(sort)
const newSortedIds = allEntities.map(selectId)
const { ids } = state
if (!areArraysEqual(ids, newSortedIds)) {
state.ids = newSortedIds
}
}
return {
removeOne,
removeMany,
removeAll,
addOne: createStateOperator(addOneMutably),
updateOne: createStateOperator(updateOneMutably),
upsertOne: createStateOperator(upsertOneMutably),
setAll: createStateOperator(setAllMutably),
addMany: createStateOperator(addManyMutably),
updateMany: createStateOperator(updateManyMutably),
upsertMany: createStateOperator(upsertManyMutably)
}
}