Skip to content

Commit

Permalink
Add creationStack, created when createSelector was first called
Browse files Browse the repository at this point in the history
  • Loading branch information
EskiMojo14 committed Jan 15, 2024
1 parent bcfc3cd commit 0e1cb32
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 8 deletions.
14 changes: 12 additions & 2 deletions src/createSelectorCreator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,14 @@ export function createSelectorCreator<
>
]
) => {
let creationStack: string | undefined = undefined
if (process.env.NODE_ENV !== 'production') {
try {
throw new Error()
} catch (error) {
creationStack = (error as Error).stack
}
}
let recomputations = 0
let dependencyRecomputations = 0
let lastResult: Result
Expand Down Expand Up @@ -418,7 +426,8 @@ export function createSelectorCreator<
identityFunctionCheck.run(
resultFunc as Combiner<InputSelectors, Result>,
inputSelectorResults,
lastResult
lastResult,
creationStack
)
}

Expand All @@ -432,7 +441,8 @@ export function createSelectorCreator<
inputStabilityCheck.run(
{ inputSelectorResults, inputSelectorResultsCopy },
{ memoize, memoizeOptions: finalMemoizeOptions },
arguments
arguments,
creationStack
)
}

Expand Down
5 changes: 3 additions & 2 deletions src/devModeChecks/identityFunctionCheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import type { AnyFunction } from '../types'
export const runIdentityFunctionCheck = (
resultFunc: AnyFunction,
inputSelectorsResults: unknown[],
outputSelectorResult: unknown
outputSelectorResult: unknown,
creationStack: string | undefined
) => {
if (
inputSelectorsResults.length === 1 &&
Expand All @@ -46,7 +47,7 @@ export const runIdentityFunctionCheck = (
'\n`createSelector([state => state.todos], todos => todos)`' +
'\nThis could lead to inefficient memoization and unnecessary re-renders.' +
'\nEnsure transformation logic is in the result function, and extraction logic is in the input selectors.',
{ stack }
{ stack, creationStack }
)
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/devModeChecks/inputStabilityCheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ export const runInputStabilityCheck = (
'memoize' | 'memoizeOptions'
>
>,
inputSelectorArgs: unknown[] | IArguments
inputSelectorArgs: unknown[] | IArguments,
creationStack: string | undefined
) => {
const { memoize, memoizeOptions } = options
const { inputSelectorResults, inputSelectorResultsCopy } =
Expand All @@ -52,7 +53,8 @@ export const runInputStabilityCheck = (
arguments: inputSelectorArgs,
firstInputs: inputSelectorResults,
secondInputs: inputSelectorResultsCopy,
stack
stack,
creationStack
}
)
}
Expand Down
3 changes: 2 additions & 1 deletion test/identityFunctionCheck.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ describe('identityFunctionCheck', () => {
'The result function returned its own inputs without modification'
),
{
stack: expect.any(String)
stack: expect.any(String),
creationStack: expect.any(String)
}
)
})
Expand Down
3 changes: 2 additions & 1 deletion test/inputStabilityCheck.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ describe('inputStabilityCheck', () => {
secondInputs: expect.arrayContaining([
expect.objectContaining({ a: 1, b: 2 })
]),
stack: expect.any(String)
stack: expect.any(String),
creationStack: expect.any(String)
})
)
})
Expand Down

0 comments on commit 0e1cb32

Please sign in to comment.