From f0088d3e1cd3fed1150a07292bc004af244110b2 Mon Sep 17 00:00:00 2001 From: Ben Durrant Date: Fri, 28 Jun 2024 16:23:34 +0100 Subject: [PATCH 1/3] golf a few things --- packages/toolkit/src/entities/sorted_state_adapter.ts | 10 ++++------ .../toolkit/src/entities/unsorted_state_adapter.ts | 4 +--- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/packages/toolkit/src/entities/sorted_state_adapter.ts b/packages/toolkit/src/entities/sorted_state_adapter.ts index 560d8a8896..0578032764 100644 --- a/packages/toolkit/src/entities/sorted_state_adapter.ts +++ b/packages/toolkit/src/entities/sorted_state_adapter.ts @@ -175,7 +175,7 @@ export function createSortedStateAdapter( 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 } @@ -191,7 +191,7 @@ export function createSortedStateAdapter( replacedIds?: boolean, ) => void - const mergeInsertion: MergeFunction = ( + const mergeFunction: MergeFunction = ( state, addedItems, appliedUpdates, @@ -202,9 +202,9 @@ export function createSortedStateAdapter( const stateEntities = state.entities as Record - let ids = currentIds + let ids: Iterable = currentIds if (replacedIds) { - ids = Array.from(new Set(currentIds)) + ids = new Set(currentIds) } let sortedEntities: T[] = [] @@ -241,8 +241,6 @@ export function createSortedStateAdapter( } } - const mergeFunction: MergeFunction = mergeInsertion - return { removeOne, removeMany, diff --git a/packages/toolkit/src/entities/unsorted_state_adapter.ts b/packages/toolkit/src/entities/unsorted_state_adapter.ts index e2d6c31357..54739ec9d7 100644 --- a/packages/toolkit/src/entities/unsorted_state_adapter.ts +++ b/packages/toolkit/src/entities/unsorted_state_adapter.ts @@ -145,9 +145,7 @@ export function createUnsortedStateAdapter( // 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, }, } From d114f816cd5a5fd47f29056efd5073c0408f5f5c Mon Sep 17 00:00:00 2001 From: Ben Durrant Date: Fri, 28 Jun 2024 16:24:21 +0100 Subject: [PATCH 2/3] type getCurrent to remove Draft type --- packages/toolkit/src/entities/utils.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/toolkit/src/entities/utils.ts b/packages/toolkit/src/entities/utils.ts index c073a17daa..99a7abebf4 100644 --- a/packages/toolkit/src/entities/utils.ts +++ b/packages/toolkit/src/entities/utils.ts @@ -1,4 +1,4 @@ -import { current, isDraft } from 'immer' +import { Draft, current, isDraft } from 'immer' import type { IdSelector, Update, @@ -36,8 +36,8 @@ export function ensureEntitiesArray( return entities } -export function getCurrent(value: T): T { - return isDraft(value) ? current(value) : value +export function getCurrent(value: T | Draft): T { + return (isDraft(value) ? current(value) : value) as T } export function splitAddedUpdatedEntities( @@ -47,7 +47,7 @@ export function splitAddedUpdatedEntities( ): [T[], Update[], Id[]] { newEntities = ensureEntitiesArray(newEntities) - const existingIdsArray = getCurrent(state.ids) as Id[] + const existingIdsArray = getCurrent(state.ids) const existingIds = new Set(existingIdsArray) const added: T[] = [] From 93b29e13c1e9564df997887363d6ff98e3cb0094 Mon Sep 17 00:00:00 2001 From: Ben Durrant Date: Fri, 28 Jun 2024 16:36:10 +0100 Subject: [PATCH 3/3] remove a couple more unnecessary assertions --- packages/toolkit/src/entities/sorted_state_adapter.ts | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/packages/toolkit/src/entities/sorted_state_adapter.ts b/packages/toolkit/src/entities/sorted_state_adapter.ts index 0578032764..ec2ae5dd50 100644 --- a/packages/toolkit/src/entities/sorted_state_adapter.ts +++ b/packages/toolkit/src/entities/sorted_state_adapter.ts @@ -69,9 +69,7 @@ export function createSortedStateAdapter( ): void { newEntities = ensureEntitiesArray(newEntities) - const existingKeys = new Set( - existingIds ?? (getCurrent(state.ids) as Id[]), - ) + const existingKeys = new Set(existingIds ?? getCurrent(state.ids)) const models = newEntities.filter( (model) => !existingKeys.has(selectIdValue(model, selectId)), @@ -197,8 +195,8 @@ export function createSortedStateAdapter( appliedUpdates, replacedIds, ) => { - const currentEntities = getCurrent(state.entities) as Record - const currentIds = getCurrent(state.ids) as Id[] + const currentEntities = getCurrent(state.entities) + const currentIds = getCurrent(state.ids) const stateEntities = state.entities as Record