Skip to content

Commit

Permalink
Merge pull request #4485 from reduxjs/entity-golf
Browse files Browse the repository at this point in the history
Golf a few pieces of entity adapter code
  • Loading branch information
EskiMojo14 committed Jun 28, 2024
2 parents 09c36a2 + 93b29e1 commit 20d5d2b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 18 deletions.
18 changes: 7 additions & 11 deletions packages/toolkit/src/entities/sorted_state_adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@ export function createSortedStateAdapter<T, Id extends EntityId>(
): void {
newEntities = ensureEntitiesArray(newEntities)

const existingKeys = new Set<Id>(
existingIds ?? (getCurrent(state.ids) as Id[]),
)
const existingKeys = new Set<Id>(existingIds ?? getCurrent(state.ids))

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

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

const mergeInsertion: MergeFunction = (
const mergeFunction: MergeFunction = (
state,
addedItems,
appliedUpdates,
replacedIds,
) => {
const currentEntities = getCurrent(state.entities) as Record<Id, T>
const currentIds = getCurrent(state.ids) as Id[]
const currentEntities = getCurrent(state.entities)
const currentIds = getCurrent(state.ids)

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

let ids = currentIds
let ids: Iterable<Id> = currentIds
if (replacedIds) {
ids = Array.from(new Set(currentIds))
ids = new Set(currentIds)
}

let sortedEntities: T[] = []
Expand Down Expand Up @@ -241,8 +239,6 @@ export function createSortedStateAdapter<T, Id extends EntityId>(
}
}

const mergeFunction: MergeFunction = mergeInsertion

return {
removeOne,
removeMany,
Expand Down
4 changes: 1 addition & 3 deletions packages/toolkit/src/entities/unsorted_state_adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,7 @@ export function createUnsortedStateAdapter<T, Id extends EntityId>(
// Spreads ignore falsy values, so this works even if there isn't
// an existing update already at this key
changes: {
...(updatesPerEntity[update.id]
? updatesPerEntity[update.id].changes
: null),
...updatesPerEntity[update.id]?.changes,
...update.changes,
},
}
Expand Down
8 changes: 4 additions & 4 deletions packages/toolkit/src/entities/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { current, isDraft } from 'immer'
import { Draft, current, isDraft } from 'immer'
import type {
IdSelector,
Update,
Expand Down Expand Up @@ -36,8 +36,8 @@ export function ensureEntitiesArray<T, Id extends EntityId>(
return entities
}

export function getCurrent<T>(value: T): T {
return isDraft(value) ? current(value) : value
export function getCurrent<T>(value: T | Draft<T>): T {
return (isDraft(value) ? current(value) : value) as T
}

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

const existingIdsArray = getCurrent(state.ids) as Id[]
const existingIdsArray = getCurrent(state.ids)
const existingIds = new Set<Id>(existingIdsArray)

const added: T[] = []
Expand Down

0 comments on commit 20d5d2b

Please sign in to comment.