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

V5 final type changes #639

Merged
merged 11 commits into from
Nov 21, 2023
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ typescript_test/common.js
flow_test/should_fail/flow-typed/index.js.flow
flow_test/should_pass/flow-typed/index.js.flow
reselect-builds/

trace

typesversions
.cache
Expand All @@ -24,4 +24,4 @@ typesversions
!.yarn/sdks
!.yarn/versions
.pnp.*
*.tgz
*.tgz
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@
"format": "prettier --write \"{src,test}/**/*.{js,ts}\" \"docs/**/*.md\"",
"lint": "eslint src test",
"prepack": "yarn build",
"bench": "vitest --run bench",
"bench": "vitest --run bench --mode production",
"test": "node --expose-gc ./node_modules/vitest/dist/cli-wrapper.js run",
"test:cov": "vitest run --coverage",
"type-check": "vitest --run typecheck",
"type-check:trace": "vitest --run typecheck && tsc --noEmit -p typescript_test/tsconfig.json --generateTrace trace && npx @typescript/analyze-trace trace && rimraf trace",
"test:typescript": "tsc --noEmit -p typescript_test/tsconfig.json"
},
"keywords": [
Expand All @@ -54,6 +56,7 @@
"@typescript-eslint/eslint-plugin": "5.1.0",
"@typescript-eslint/eslint-plugin-tslint": "5.1.0",
"@typescript-eslint/parser": "5.1.0",
"@typescript/analyze-trace": "^0.10.1",
"eslint": "^8.0.1",
"eslint-plugin-react": "^7.26.1",
"eslint-plugin-typescript": "0.14.0",
Expand Down
14 changes: 10 additions & 4 deletions src/autotrackMemoize/autotrackMemoize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import {
createCacheKeyComparator,
defaultEqualityCheck
} from '@internal/defaultMemoize'
import type { AnyFunction } from '@internal/types'
import type {
AnyFunction,
DefaultMemoizeFields,
Simplify
} from '@internal/types'
import { createCache } from './autotracking'

/**
Expand Down Expand Up @@ -55,7 +59,7 @@ import { createCache } from './autotracking'
* ```ts
* import { unstable_autotrackMemoize as autotrackMemoize, createSelectorCreator } from 'reselect'
*
* const createSelectorAutotrack = createSelectorCreator(autotrackMemoize)
* const createSelectorAutotrack = createSelectorCreator({ memoize: autotrackMemoize })
*
* const selectTodoIds = createSelectorAutotrack(
* [(state: RootState) => state.todos],
Expand Down Expand Up @@ -95,7 +99,9 @@ export function autotrackMemoize<Func extends AnyFunction>(func: Func) {
return cache.value
}

memoized.clearCache = () => cache.clear()
memoized.clearCache = () => {
return cache.clear()
}

return memoized as Func & { clearCache: () => void }
return memoized as Func & Simplify<DefaultMemoizeFields>
}
97 changes: 53 additions & 44 deletions src/createSelectorCreator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { OutputSelector, Selector, SelectorArray } from 'reselect'
import { defaultMemoize } from './defaultMemoize'

import type {
Expand All @@ -9,6 +8,11 @@ import type {
GetParamsFromSelectors,
GetStateFromSelectors,
InterruptRecursion,
OutputSelector,
Selector,
SelectorArray,
SetRequired,
Simplify,
StabilityCheckFrequency,
UnknownMemoizer
} from './types'
Expand Down Expand Up @@ -82,7 +86,7 @@ export interface CreateSelectorFunction<
...createSelectorArgs: [
...inputSelectors: InputSelectors,
combiner: Combiner<InputSelectors, Result>,
createSelectorOptions: Partial<
createSelectorOptions: Simplify<
CreateSelectorOptions<
MemoizeFunction,
ArgsMemoizeFunction,
Expand Down Expand Up @@ -122,7 +126,7 @@ export interface CreateSelectorFunction<
>(
inputSelectors: [...InputSelectors],
combiner: Combiner<InputSelectors, Result>,
createSelectorOptions?: Partial<
createSelectorOptions?: Simplify<
CreateSelectorOptions<
MemoizeFunction,
ArgsMemoizeFunction,
Expand Down Expand Up @@ -220,11 +224,16 @@ export function createSelectorCreator<
MemoizeFunction extends UnknownMemoizer,
ArgsMemoizeFunction extends UnknownMemoizer = typeof defaultMemoize
>(
options: CreateSelectorOptions<
typeof defaultMemoize,
typeof defaultMemoize,
MemoizeFunction,
ArgsMemoizeFunction
options: Simplify<
SetRequired<
CreateSelectorOptions<
typeof defaultMemoize,
typeof defaultMemoize,
MemoizeFunction,
ArgsMemoizeFunction
>,
'memoize'
>
>
): CreateSelectorFunction<MemoizeFunction, ArgsMemoizeFunction>

Expand Down Expand Up @@ -276,27 +285,29 @@ export function createSelectorCreator<
ArgsMemoizeFunction extends UnknownMemoizer,
MemoizeOrOptions extends
| MemoizeFunction
| CreateSelectorOptions<MemoizeFunction, ArgsMemoizeFunction>
| SetRequired<
CreateSelectorOptions<MemoizeFunction, ArgsMemoizeFunction>,
'memoize'
>
>(
memoizeOrOptions: MemoizeOrOptions,
...memoizeOptionsFromArgs: MemoizeOrOptions extends CreateSelectorOptions<
MemoizeFunction,
ArgsMemoizeFunction
...memoizeOptionsFromArgs: MemoizeOrOptions extends SetRequired<
CreateSelectorOptions<MemoizeFunction, ArgsMemoizeFunction>,
'memoize'
>
? never
: DropFirstParameter<MemoizeFunction>
) {
/** options initially passed into `createSelectorCreator`. */
const createSelectorCreatorOptions: CreateSelectorOptions<
MemoizeFunction,
ArgsMemoizeFunction
> =
typeof memoizeOrOptions === 'function'
? {
memoize: memoizeOrOptions as MemoizeFunction,
memoizeOptions: memoizeOptionsFromArgs
}
: memoizeOrOptions
const createSelectorCreatorOptions: SetRequired<
CreateSelectorOptions<MemoizeFunction, ArgsMemoizeFunction>,
'memoize'
> = typeof memoizeOrOptions === 'function'
? {
memoize: memoizeOrOptions as MemoizeFunction,
memoizeOptions: memoizeOptionsFromArgs
}
: memoizeOrOptions

const createSelector = <
InputSelectors extends SelectorArray,
Expand All @@ -307,41 +318,36 @@ export function createSelectorCreator<
...createSelectorArgs: [
...inputSelectors: [...InputSelectors],
combiner: Combiner<InputSelectors, Result>,
createSelectorOptions?: Partial<
CreateSelectorOptions<
MemoizeFunction,
ArgsMemoizeFunction,
OverrideMemoizeFunction,
OverrideArgsMemoizeFunction
>
createSelectorOptions?: CreateSelectorOptions<
MemoizeFunction,
ArgsMemoizeFunction,
OverrideMemoizeFunction,
OverrideArgsMemoizeFunction
>
]
) => {
let recomputations = 0
let dependencyRecomputations = 0
let lastResult: Result

// Due to the intricacies of rest params, we can't do an optional arg after `...funcs`.
// Due to the intricacies of rest params, we can't do an optional arg after `...createSelectorArgs`.
// 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: Partial<
CreateSelectorOptions<
MemoizeFunction,
ArgsMemoizeFunction,
OverrideMemoizeFunction,
OverrideArgsMemoizeFunction
>
let directlyPassedOptions: CreateSelectorOptions<
MemoizeFunction,
ArgsMemoizeFunction,
OverrideMemoizeFunction,
OverrideArgsMemoizeFunction
> = {}

// Normally, the result func or "combiner" is the last arg
let resultFunc = createSelectorArgs.pop() as
| Combiner<InputSelectors, Result>
| Partial<
CreateSelectorOptions<
MemoizeFunction,
ArgsMemoizeFunction,
OverrideMemoizeFunction,
OverrideArgsMemoizeFunction
>
| CreateSelectorOptions<
MemoizeFunction,
ArgsMemoizeFunction,
OverrideMemoizeFunction,
OverrideArgsMemoizeFunction
>

// If the result func is actually an _object_, assume it's our options object
Expand Down Expand Up @@ -395,6 +401,7 @@ export function createSelectorCreator<

// If a selector is called with the exact same arguments we don't need to traverse our dependencies again.
const selector = argsMemoize(function dependenciesChecker() {
dependencyRecomputations++
/** Return values of input selectors which the `resultFunc` takes as arguments. */
const inputSelectorResults = collectInputSelectorResults(
dependencies,
Expand Down Expand Up @@ -436,6 +443,8 @@ export function createSelectorCreator<
resultFunc,
memoizedResultFunc,
dependencies,
dependencyRecomputations: () => dependencyRecomputations,
resetDependencyRecomputations: () => (dependencyRecomputations = 0),
lastResult: () => lastResult,
recomputations: () => recomputations,
resetRecomputations: () => (recomputations = 0),
Expand Down
Loading
Loading