Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Golf a few pieces of entity adapter code #4485

Merged
merged 3 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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