|
|
import { runIdentityFunctionCheck } from './devModeChecks/identityFunctionCheck' |
|
|
import { runInputStabilityCheck } from './devModeChecks/inputStabilityCheck' |
|
|
import { globalDevModeChecks } from './devModeChecks/setGlobalDevModeChecks' |
|
|
|
|
|
import type { |
|
|
DevModeChecks, |
|
|
Selector, |
|
|
SelectorArray, |
|
|
DevModeChecksExecutionInfo |
|
|
} from './types' |
|
|
|
|
|
export const NOT_FOUND = Symbol('NOT_FOUND') |
|
|
export type NOT_FOUND_TYPE = typeof NOT_FOUND |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function assertIsFunction<FunctionType extends Function>( |
|
|
func: unknown, |
|
|
errorMessage = `expected a function, instead received ${typeof func}` |
|
|
): asserts func is FunctionType { |
|
|
if (typeof func !== 'function') { |
|
|
throw new TypeError(errorMessage) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function assertIsObject<ObjectType extends Record<string, unknown>>( |
|
|
object: unknown, |
|
|
errorMessage = `expected an object, instead received ${typeof object}` |
|
|
): asserts object is ObjectType { |
|
|
if (typeof object !== 'object') { |
|
|
throw new TypeError(errorMessage) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function assertIsArrayOfFunctions<FunctionType extends Function>( |
|
|
array: unknown[], |
|
|
errorMessage = `expected all items to be functions, instead received the following types: ` |
|
|
): asserts array is FunctionType[] { |
|
|
if ( |
|
|
!array.every((item): item is FunctionType => typeof item === 'function') |
|
|
) { |
|
|
const itemTypes = array |
|
|
.map(item => |
|
|
typeof item === 'function' |
|
|
? `function ${item.name || 'unnamed'}()` |
|
|
: typeof item |
|
|
) |
|
|
.join(', ') |
|
|
throw new TypeError(`${errorMessage}[${itemTypes}]`) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const ensureIsArray = (item: unknown) => { |
|
|
return Array.isArray(item) ? item : [item] |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function getDependencies(createSelectorArgs: unknown[]) { |
|
|
const dependencies = Array.isArray(createSelectorArgs[0]) |
|
|
? createSelectorArgs[0] |
|
|
: createSelectorArgs |
|
|
|
|
|
assertIsArrayOfFunctions<Selector>( |
|
|
dependencies, |
|
|
`createSelector expects all input-selectors to be functions, but received the following types: ` |
|
|
) |
|
|
|
|
|
return dependencies as SelectorArray |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function collectInputSelectorResults( |
|
|
dependencies: SelectorArray, |
|
|
inputSelectorArgs: unknown[] | IArguments |
|
|
) { |
|
|
const inputSelectorResults = [] |
|
|
const { length } = dependencies |
|
|
for (let i = 0; i < length; i++) { |
|
|
|
|
|
|
|
|
inputSelectorResults.push(dependencies[i].apply(null, inputSelectorArgs)) |
|
|
} |
|
|
return inputSelectorResults |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const getDevModeChecksExecutionInfo = ( |
|
|
firstRun: boolean, |
|
|
devModeChecks: Partial<DevModeChecks> |
|
|
) => { |
|
|
const { identityFunctionCheck, inputStabilityCheck } = { |
|
|
...globalDevModeChecks, |
|
|
...devModeChecks |
|
|
} |
|
|
return { |
|
|
identityFunctionCheck: { |
|
|
shouldRun: |
|
|
identityFunctionCheck === 'always' || |
|
|
(identityFunctionCheck === 'once' && firstRun), |
|
|
run: runIdentityFunctionCheck |
|
|
}, |
|
|
inputStabilityCheck: { |
|
|
shouldRun: |
|
|
inputStabilityCheck === 'always' || |
|
|
(inputStabilityCheck === 'once' && firstRun), |
|
|
run: runInputStabilityCheck |
|
|
} |
|
|
} satisfies DevModeChecksExecutionInfo |
|
|
} |
|
|
|