From 877d093e537e7d2d1ce163e804df9927c41216c1 Mon Sep 17 00:00:00 2001 From: Rom Grk Date: Fri, 15 Mar 2024 03:02:23 -0400 Subject: [PATCH 1/3] fix: use unique value for not_found --- src/utils.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/utils.ts b/src/utils.ts index 756740515..ab3be9527 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -9,7 +9,9 @@ import type { DevModeChecksExecutionInfo } from './types' -export const NOT_FOUND = 'NOT_FOUND' +class NotFound {} + +export const NOT_FOUND = new NotFound() export type NOT_FOUND_TYPE = typeof NOT_FOUND /** From 8ba87ff3a7f29137e0b39014ab9de0fab5e77c70 Mon Sep 17 00:00:00 2001 From: Rom Grk Date: Fri, 22 Mar 2024 09:49:53 -0400 Subject: [PATCH 2/3] test: add collision test --- src/utils.ts | 4 +--- test/lruMemoize.test.ts | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index ab3be9527..539980e22 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -9,9 +9,7 @@ import type { DevModeChecksExecutionInfo } from './types' -class NotFound {} - -export const NOT_FOUND = new NotFound() +export const NOT_FOUND = Symbol('NOT_FOUND') export type NOT_FOUND_TYPE = typeof NOT_FOUND /** diff --git a/test/lruMemoize.test.ts b/test/lruMemoize.test.ts index be3ed663d..6c42808eb 100644 --- a/test/lruMemoize.test.ts +++ b/test/lruMemoize.test.ts @@ -421,6 +421,38 @@ describe(lruMemoize, () => { expect(selector.resultFunc.clearCache).toBeUndefined() }) + test('cache miss identifier does not collide with state values', () => { + const state = ['NOT_FOUND', 'FOUND'] + + type State = typeof state + + const createSelector = createSelectorCreator({ + memoize: lruMemoize, + argsMemoize: lruMemoize + }).withTypes() + + const selector = createSelector( + [(state, id: number) => state[id]], + state => state, + { + argsMemoizeOptions: { maxSize: 10 }, + memoizeOptions: { maxSize: 10 } + } + ) + + const firstResult = selector(state, 0) + + expect(selector(state, 1)).toBe(selector(state, 1)) + + const secondResult = selector(state, 0) + + expect(secondResult).toBe('NOT_FOUND') + + expect(firstResult).toBe(secondResult) + + expect(selector.recomputations()).toBe(2) + }) + localTest( 'maxSize should default to 1 when set to a number that is less than 1', ({ state, store }) => { From 3692d5c47b1b2c4b52bbe901a40feb5dde85b7f1 Mon Sep 17 00:00:00 2001 From: Mark Erikson Date: Sat, 1 Jun 2024 17:53:00 -0400 Subject: [PATCH 3/3] Update src/utils.ts Co-authored-by: Arya Emami --- src/utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils.ts b/src/utils.ts index 539980e22..672fcd320 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -9,7 +9,7 @@ import type { DevModeChecksExecutionInfo } from './types' -export const NOT_FOUND = Symbol('NOT_FOUND') +export const NOT_FOUND = /* @__PURE__ */ Symbol('NOT_FOUND') export type NOT_FOUND_TYPE = typeof NOT_FOUND /**