Skip to content

Commit

Permalink
use a library-wide flag instead of per-selector config
Browse files Browse the repository at this point in the history
  • Loading branch information
EskiMojo14 committed May 13, 2023
1 parent 903b463 commit f1f6fef
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
23 changes: 15 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ export { defaultMemoize, defaultEqualityCheck }

export type { DefaultMemoizeOptions }

let inputStabilityCheckDisabled = false

export function disableInputStabilityCheck(disable = true) {
inputStabilityCheckDisabled = disable
}

function getDependencies(funcs: unknown[]) {
const dependencies = Array.isArray(funcs[0]) ? funcs[0] : funcs

Expand Down Expand Up @@ -77,7 +83,9 @@ export function createSelectorCreator<
// Due to the intricacies of rest params, we can't do an optional arg after `...funcs`.
// So, start by declaring the default value here.
// (And yes, the words 'memoize' and 'options' appear too many times in this next sequence.)
let directlyPassedOptions: CreateSelectorOptions<MemoizeOptions> = {}
let directlyPassedOptions: CreateSelectorOptions<MemoizeOptions> = {
memoizeOptions: undefined
}

// Normally, the result func or "output selector" is the last arg
let resultFunc = funcs.pop()
Expand All @@ -97,10 +105,7 @@ export function createSelectorCreator<

// Determine which set of options we're using. Prefer options passed directly,
// but fall back to options given to createSelectorCreator.
const {
memoizeOptions = memoizeOptionsFromArgs,
inputStabilityCheck = true
} = directlyPassedOptions
const { memoizeOptions = memoizeOptionsFromArgs } = directlyPassedOptions

// Simplifying assumption: it's unlikely that the first options arg of the provided memoizer
// is an array. In most libs I've looked at, it's an equality function or options object.
Expand Down Expand Up @@ -144,7 +149,10 @@ export function createSelectorCreator<
params.push(dependencies[i].apply(null, arguments))
}

if (process.env.NODE_ENV !== 'production' && inputStabilityCheck) {
if (
process.env.NODE_ENV !== 'production' &&
!inputStabilityCheckDisabled
) {
const paramsCopy = []

for (let i = 0; i < length; i++) {
Expand Down Expand Up @@ -190,8 +198,7 @@ export function createSelectorCreator<
}

export interface CreateSelectorOptions<MemoizeOptions extends unknown[]> {
memoizeOptions?: MemoizeOptions[0] | MemoizeOptions
inputStabilityCheck?: boolean
memoizeOptions: MemoizeOptions[0] | MemoizeOptions
}

/**
Expand Down
18 changes: 9 additions & 9 deletions test/inputStabilityCheck.spec.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
import { createSelector } from 'reselect'
import { createSelector, disableInputStabilityCheck } from 'reselect'

describe('inputStabilityCheck', () => {
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})

const unstableInput = vi.fn((a: number, b: number) => ({ a, b }))

const addNums = createSelector([unstableInput], ({ a, b }) => a + b)

afterEach(() => {
consoleSpy.mockClear()
unstableInput.mockClear()
addNums.clearCache()
})
afterAll(() => {
consoleSpy.mockRestore()
})

it('calls each input selector twice, and warns to console if unstable reference is returned', () => {
const stableAddNums = createSelector(
[(a: number) => a, (a, b: number) => b],
[(a: number) => a, (a: number, b: number) => b],
(a, b) => a + b
)

expect(stableAddNums(1, 2)).toBe(3)

expect(consoleSpy).not.toHaveBeenCalled()

const addNums = createSelector([unstableInput], ({ a, b }) => a + b)

expect(addNums(1, 2)).toBe(3)

expect(unstableInput).toHaveBeenCalledTimes(2)
Expand All @@ -34,24 +36,22 @@ describe('inputStabilityCheck', () => {
})

it('disables check if specified', () => {
const addNums = createSelector([unstableInput], ({ a, b }) => a + b, {
inputStabilityCheck: false
})
disableInputStabilityCheck()

expect(addNums(1, 2)).toBe(3)

expect(unstableInput).toHaveBeenCalledTimes(1)

expect(consoleSpy).not.toHaveBeenCalled()

disableInputStabilityCheck(false)
})

it('disables check in production', () => {
const originalEnv = process.env.NODE_ENV

process.env.NODE_ENV = 'production'

const addNums = createSelector([unstableInput], ({ a, b }) => a + b)

expect(addNums(1, 2)).toBe(3)

expect(unstableInput).toHaveBeenCalledTimes(1)
Expand Down

0 comments on commit f1f6fef

Please sign in to comment.