From 106de54fa4771d3e490366dd84d103ff6a32414f Mon Sep 17 00:00:00 2001 From: JacobShafer Date: Tue, 21 May 2024 13:18:18 -0500 Subject: [PATCH 1/2] Fix Immer current usage when calling addManyMutably more than once --- .../src/entities/sorted_state_adapter.ts | 2 +- .../tests/sorted_state_adapter.test.ts | 25 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/packages/toolkit/src/entities/sorted_state_adapter.ts b/packages/toolkit/src/entities/sorted_state_adapter.ts index 8f16ed2942..8e4f47e744 100644 --- a/packages/toolkit/src/entities/sorted_state_adapter.ts +++ b/packages/toolkit/src/entities/sorted_state_adapter.ts @@ -71,7 +71,7 @@ export function createSortedStateAdapter( newEntities = ensureEntitiesArray(newEntities) const existingKeys = new Set( - existingIds ?? (current(state.ids) as Id[]), + existingIds ?? (getCurrent(state.ids) as Id[]), ) const models = newEntities.filter( diff --git a/packages/toolkit/src/entities/tests/sorted_state_adapter.test.ts b/packages/toolkit/src/entities/tests/sorted_state_adapter.test.ts index fb09089ece..93cff228f6 100644 --- a/packages/toolkit/src/entities/tests/sorted_state_adapter.test.ts +++ b/packages/toolkit/src/entities/tests/sorted_state_adapter.test.ts @@ -808,6 +808,31 @@ describe('Sorted State Adapter', () => { ) }) + it('should not throw an Immer `current` error when the adapter is called twice', () => { + const book1: BookModel = { id: 'a', title: 'First' } + const book2: BookModel = { id: 'b', title: 'Second' } + const initialState = adapter.getInitialState() + const booksSlice = createSlice({ + name: 'books', + initialState, + reducers: { + testCurrentBehavior(state, action: PayloadAction) { + // Will overwrite `state.ids` with a plain array + adapter.removeAll(state) + + // will call `splitAddedUpdatedEntities` and call `current(state.ids)` + adapter.addOne(state, book1) + adapter.addOne(state, book2) + }, + }, + }) + + booksSlice.reducer( + initialState, + booksSlice.actions.testCurrentBehavior(book1), + ) + }) + describe('can be used mutably when wrapped in createNextState', () => { test('removeAll', () => { const withTwo = adapter.addMany(state, [TheGreatGatsby, AnimalFarm]) From b1b845a89f98be9c50461b875192f381d0220a72 Mon Sep 17 00:00:00 2001 From: Ben Durrant Date: Fri, 28 Jun 2024 15:31:32 +0100 Subject: [PATCH 2/2] remove unused imports --- packages/toolkit/src/entities/sorted_state_adapter.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/toolkit/src/entities/sorted_state_adapter.ts b/packages/toolkit/src/entities/sorted_state_adapter.ts index 8e4f47e744..560d8a8896 100644 --- a/packages/toolkit/src/entities/sorted_state_adapter.ts +++ b/packages/toolkit/src/entities/sorted_state_adapter.ts @@ -1,4 +1,3 @@ -import { current, isDraft } from 'immer' import type { IdSelector, Comparer,