Skip to content

Commit 20d5d2b

Browse files
authored
Merge pull request #4485 from reduxjs/entity-golf
Golf a few pieces of entity adapter code
2 parents 09c36a2 + 93b29e1 commit 20d5d2b

File tree

3 files changed

+12
-18
lines changed

3 files changed

+12
-18
lines changed

packages/toolkit/src/entities/sorted_state_adapter.ts

+7-11
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,7 @@ export function createSortedStateAdapter<T, Id extends EntityId>(
6969
): void {
7070
newEntities = ensureEntitiesArray(newEntities)
7171

72-
const existingKeys = new Set<Id>(
73-
existingIds ?? (getCurrent(state.ids) as Id[]),
74-
)
72+
const existingKeys = new Set<Id>(existingIds ?? getCurrent(state.ids))
7573

7674
const models = newEntities.filter(
7775
(model) => !existingKeys.has(selectIdValue(model, selectId)),
@@ -175,7 +173,7 @@ export function createSortedStateAdapter<T, Id extends EntityId>(
175173
return false
176174
}
177175

178-
for (let i = 0; i < a.length && i < b.length; i++) {
176+
for (let i = 0; i < a.length; i++) {
179177
if (a[i] === b[i]) {
180178
continue
181179
}
@@ -191,20 +189,20 @@ export function createSortedStateAdapter<T, Id extends EntityId>(
191189
replacedIds?: boolean,
192190
) => void
193191

194-
const mergeInsertion: MergeFunction = (
192+
const mergeFunction: MergeFunction = (
195193
state,
196194
addedItems,
197195
appliedUpdates,
198196
replacedIds,
199197
) => {
200-
const currentEntities = getCurrent(state.entities) as Record<Id, T>
201-
const currentIds = getCurrent(state.ids) as Id[]
198+
const currentEntities = getCurrent(state.entities)
199+
const currentIds = getCurrent(state.ids)
202200

203201
const stateEntities = state.entities as Record<Id, T>
204202

205-
let ids = currentIds
203+
let ids: Iterable<Id> = currentIds
206204
if (replacedIds) {
207-
ids = Array.from(new Set(currentIds))
205+
ids = new Set(currentIds)
208206
}
209207

210208
let sortedEntities: T[] = []
@@ -241,8 +239,6 @@ export function createSortedStateAdapter<T, Id extends EntityId>(
241239
}
242240
}
243241

244-
const mergeFunction: MergeFunction = mergeInsertion
245-
246242
return {
247243
removeOne,
248244
removeMany,

packages/toolkit/src/entities/unsorted_state_adapter.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,7 @@ export function createUnsortedStateAdapter<T, Id extends EntityId>(
145145
// Spreads ignore falsy values, so this works even if there isn't
146146
// an existing update already at this key
147147
changes: {
148-
...(updatesPerEntity[update.id]
149-
? updatesPerEntity[update.id].changes
150-
: null),
148+
...updatesPerEntity[update.id]?.changes,
151149
...update.changes,
152150
},
153151
}

packages/toolkit/src/entities/utils.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { current, isDraft } from 'immer'
1+
import { Draft, current, isDraft } from 'immer'
22
import type {
33
IdSelector,
44
Update,
@@ -36,8 +36,8 @@ export function ensureEntitiesArray<T, Id extends EntityId>(
3636
return entities
3737
}
3838

39-
export function getCurrent<T>(value: T): T {
40-
return isDraft(value) ? current(value) : value
39+
export function getCurrent<T>(value: T | Draft<T>): T {
40+
return (isDraft(value) ? current(value) : value) as T
4141
}
4242

4343
export function splitAddedUpdatedEntities<T, Id extends EntityId>(
@@ -47,7 +47,7 @@ export function splitAddedUpdatedEntities<T, Id extends EntityId>(
4747
): [T[], Update<T, Id>[], Id[]] {
4848
newEntities = ensureEntitiesArray(newEntities)
4949

50-
const existingIdsArray = getCurrent(state.ids) as Id[]
50+
const existingIdsArray = getCurrent(state.ids)
5151
const existingIds = new Set<Id>(existingIdsArray)
5252

5353
const added: T[] = []

0 commit comments

Comments
 (0)