Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: use unique value for NOT_FOUND #709

Merged
merged 3 commits into from
Jun 1, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type {
DevModeChecksExecutionInfo
} from './types'

export const NOT_FOUND = 'NOT_FOUND'
export const NOT_FOUND = Symbol('NOT_FOUND')
markerikson marked this conversation as resolved.
Show resolved Hide resolved
export type NOT_FOUND_TYPE = typeof NOT_FOUND

/**
Expand Down
32 changes: 32 additions & 0 deletions test/lruMemoize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<State>()

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 }) => {
Expand Down