From 30c5ae7b33a1f0015c6b1416239196369d155b57 Mon Sep 17 00:00:00 2001 From: Ben Durrant Date: Sun, 29 Oct 2023 12:03:21 +0000 Subject: [PATCH 1/3] Allow passing selector creators with different memoize options --- packages/toolkit/src/entities/state_selectors.ts | 12 ++++++++++-- .../src/entities/tests/sorted_state_adapter.test.ts | 6 +++--- .../toolkit/src/entities/tests/state_adapter.test.ts | 2 +- .../src/entities/tests/state_selectors.test.ts | 5 ++++- 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/packages/toolkit/src/entities/state_selectors.ts b/packages/toolkit/src/entities/state_selectors.ts index 3bb3b88872..d2c8d9f833 100644 --- a/packages/toolkit/src/entities/state_selectors.ts +++ b/packages/toolkit/src/entities/state_selectors.ts @@ -2,9 +2,15 @@ import type { CreateSelectorFunction, Selector, createSelector } from 'reselect' import { createDraftSafeSelector } from '../createDraftSafeSelector' import type { EntityState, EntitySelectors, EntityId } from './models' +type AnyFunction = (...args: any) => any +type AnyCreateSelectorFunction = CreateSelectorFunction< + (f: F) => F, + (f: F) => F +> + export interface GetSelectorsOptions { // TODO Review if this causes issues or if we can go back to using `CreateSelectorFunction` - createSelector?: typeof createSelector + createSelector?: AnyCreateSelectorFunction } export function createSelectorsFactory() { @@ -20,7 +26,9 @@ export function createSelectorsFactory() { selectState?: (state: V) => EntityState, options: GetSelectorsOptions = {} ): EntitySelectors { - const { createSelector = createDraftSafeSelector } = options + let { createSelector } = options + createSelector ??= createDraftSafeSelector + const selectIds = (state: EntityState) => state.ids const selectEntities = (state: EntityState) => state.entities 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 e3287d0e3b..ff691a8326 100644 --- a/packages/toolkit/src/entities/tests/sorted_state_adapter.test.ts +++ b/packages/toolkit/src/entities/tests/sorted_state_adapter.test.ts @@ -11,8 +11,8 @@ import { import { createNextState } from '../..' describe('Sorted State Adapter', () => { - let adapter: EntityAdapter - let state: EntityState + let adapter: EntityAdapter + let state: EntityState beforeAll(() => { //eslint-disable-next-line @@ -349,7 +349,7 @@ describe('Sorted State Adapter', () => { order: number ts: number } - const sortedItemsAdapter = createEntityAdapter({ + const sortedItemsAdapter = createEntityAdapter({ sortComparer: (a, b) => a.order - b.order, }) const withInitialItems = sortedItemsAdapter.setAll( diff --git a/packages/toolkit/src/entities/tests/state_adapter.test.ts b/packages/toolkit/src/entities/tests/state_adapter.test.ts index ec760080ce..a05b715ff9 100644 --- a/packages/toolkit/src/entities/tests/state_adapter.test.ts +++ b/packages/toolkit/src/entities/tests/state_adapter.test.ts @@ -6,7 +6,7 @@ import { createSlice } from '../../createSlice' import type { BookModel } from './fixtures/book' describe('createStateOperator', () => { - let adapter: EntityAdapter + let adapter: EntityAdapter beforeEach(() => { adapter = createEntityAdapter({ diff --git a/packages/toolkit/src/entities/tests/state_selectors.test.ts b/packages/toolkit/src/entities/tests/state_selectors.test.ts index 9ef7b32762..f581965be3 100644 --- a/packages/toolkit/src/entities/tests/state_selectors.test.ts +++ b/packages/toolkit/src/entities/tests/state_selectors.test.ts @@ -130,7 +130,10 @@ describe('Entity State Selectors', () => { }) describe('custom createSelector instance', () => { it('should use the custom createSelector function if provided', () => { - const memoizeSpy = vi.fn(weakMapMemoize) + const memoizeSpy = vi.fn( + // test that we're allowed to pass memoizers with different options, as long as they're optional + any>(fn: F, param?: boolean) => fn + ) const createCustomSelector = createDraftSafeSelectorCreator(memoizeSpy) const adapter = createEntityAdapter({ From b05b6babb5387734cc4987ed52848e7ea67bad0e Mon Sep 17 00:00:00 2001 From: Ben Durrant Date: Sun, 29 Oct 2023 12:04:37 +0000 Subject: [PATCH 2/3] rm TODO --- packages/toolkit/src/entities/state_selectors.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/toolkit/src/entities/state_selectors.ts b/packages/toolkit/src/entities/state_selectors.ts index d2c8d9f833..3152b48ddb 100644 --- a/packages/toolkit/src/entities/state_selectors.ts +++ b/packages/toolkit/src/entities/state_selectors.ts @@ -9,7 +9,6 @@ type AnyCreateSelectorFunction = CreateSelectorFunction< > export interface GetSelectorsOptions { - // TODO Review if this causes issues or if we can go back to using `CreateSelectorFunction` createSelector?: AnyCreateSelectorFunction } From d28e6f5fdeec7507447dab6aac34aa3577d262c5 Mon Sep 17 00:00:00 2001 From: Ben Durrant Date: Sun, 29 Oct 2023 14:34:57 +0000 Subject: [PATCH 3/3] use default assignment --- packages/toolkit/src/entities/state_selectors.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/toolkit/src/entities/state_selectors.ts b/packages/toolkit/src/entities/state_selectors.ts index 3152b48ddb..f8f04449df 100644 --- a/packages/toolkit/src/entities/state_selectors.ts +++ b/packages/toolkit/src/entities/state_selectors.ts @@ -25,8 +25,7 @@ export function createSelectorsFactory() { selectState?: (state: V) => EntityState, options: GetSelectorsOptions = {} ): EntitySelectors { - let { createSelector } = options - createSelector ??= createDraftSafeSelector + const { createSelector = createDraftSafeSelector as AnyCreateSelectorFunction } = options const selectIds = (state: EntityState) => state.ids