Skip to content

Commit b2d5eeb

Browse files
author
dmelnik
committed
Ensure entity is added to the store before applying updates in upsertMany
Fixes for #3429
1 parent 2751bec commit b2d5eeb

File tree

3 files changed

+7
-5
lines changed

3 files changed

+7
-5
lines changed

packages/toolkit/src/entities/sorted_state_adapter.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,12 @@ export function createSortedStateAdapter<T, Id extends EntityId>(
160160
state,
161161
)
162162

163-
if (updated.length) {
164-
updateManyMutably(updated, state)
165-
}
166163
if (added.length) {
167164
addManyMutably(added, state, existingIdsArray)
168165
}
166+
if (updated.length) {
167+
updateManyMutably(updated, state)
168+
}
169169
}
170170

171171
function areArraysEqual(a: readonly unknown[], b: readonly unknown[]) {

packages/toolkit/src/entities/unsorted_state_adapter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,8 @@ export function createUnsortedStateAdapter<T, Id extends EntityId>(
183183
state,
184184
)
185185

186-
updateManyMutably(updated, state)
187186
addManyMutably(added, state)
187+
updateManyMutably(updated, state)
188188
}
189189

190190
return {

packages/toolkit/src/entities/utils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,15 @@ export function splitAddedUpdatedEntities<T, Id extends EntityId>(
5252
const existingIds = new Set<Id>(existingIdsArray)
5353

5454
const added: T[] = []
55+
const addedIds = new Set<Id>([])
5556
const updated: Update<T, Id>[] = []
5657

5758
for (const entity of newEntities) {
5859
const id = selectIdValue(entity, selectId)
59-
if (existingIds.has(id)) {
60+
if (existingIds.has(id) || addedIds.has(id)) {
6061
updated.push({ id, changes: entity })
6162
} else {
63+
addedIds.add(id)
6264
added.push(entity)
6365
}
6466
}

0 commit comments

Comments
 (0)