Skip to content

Commit

Permalink
Merge pull request #649 from reduxjs/feature/5.0-weakmapmemoize-default
Browse files Browse the repository at this point in the history
  • Loading branch information
markerikson authored Dec 1, 2023
2 parents 5af3050 + 10b6753 commit 5cb8537
Show file tree
Hide file tree
Showing 18 changed files with 231 additions and 468 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ A customized [`createSelector`] function.
| Name | Description |
| :-------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `MemoizeFunction` | The type of the memoize function that is used to memoize the `resultFunc` inside [`createSelector`] (e.g., `defaultMemoize` or `weakMapMemoize`). |
| `ArgsMemoizeFunction` | The type of the optional memoize function that is used to memoize the arguments passed into the [output selector] generated by [`createSelector`] (e.g., `defaultMemoize` or `weakMapMemoize`). If none is explicitly provided, `defaultMemoize` will be used. |
| `ArgsMemoizeFunction` | The type of the optional memoize function that is used to memoize the arguments passed into the [output selector] generated by [`createSelector`] (e.g., `defaultMemoize` or `weakMapMemoize`). If none is explicitly provided, `weakMapMemoize` will be used. |

</details>

Expand Down Expand Up @@ -405,7 +405,7 @@ customSelector(

`createSelectorCreator` can be used to make a customized version of [`createSelector`].

The `memoize` argument is a memoization function to replace `defaultMemoize`.
The `memoize` argument is a memoization function to replace `weakMapMemoize`.

The `...memoizeOptions` rest parameters are zero or more configuration options to be passed to `memoizeFunc`. The selectors `resultFunc` is passed as the first argument to `memoize` and the `memoizeOptions` are passed as the second argument onwards:

Expand Down Expand Up @@ -494,8 +494,8 @@ A memoized structured selector.
| Name | Description |
| :--------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `InputSelectorsObject` | The shape of the [input selectors] object. |
| `MemoizeFunction` | The type of the memoize function that is used to create the structured selector. It defaults to `defaultMemoize`. |
| `ArgsMemoizeFunction` | The type of the of the memoize function that is used to memoize the arguments passed into the generated structured selector. It defaults to `defaultMemoize`. |
| `MemoizeFunction` | The type of the memoize function that is used to create the structured selector. It defaults to `weakMapMemoize`. |
| `ArgsMemoizeFunction` | The type of the of the memoize function that is used to memoize the arguments passed into the generated structured selector. It defaults to `weakMapMemoize`. |

</details>

Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,15 @@
"@types/react": "^18.2.38",
"@types/react-dom": "^18.2.17",
"@types/shelljs": "^0.8.11",
"@typescript-eslint/eslint-plugin": "5.1.0",
"@typescript-eslint/eslint-plugin-tslint": "5.1.0",
"@typescript-eslint/parser": "5.1.0",
"@typescript-eslint/eslint-plugin": "^6",
"@typescript-eslint/eslint-plugin-tslint": "^6",
"@typescript-eslint/parser": "^6",
"@typescript/analyze-trace": "^0.10.1",
"eslint": "^8.0.1",
"eslint-plugin-react": "^7.26.1",
"eslint-plugin-typescript": "0.14.0",
"jsdom": "^23.0.0",
"lodash": "^4.17.21",
"lodash.memoize": "^4.1.2",
"memoize-one": "^6.0.0",
"micro-memoize": "^4.0.9",
Expand All @@ -74,7 +75,7 @@
"rimraf": "^3.0.2",
"shelljs": "^0.8.5",
"tsup": "^6.7.0",
"typescript": "^4.9",
"typescript": "5.2",
"vitest": "^0.34"
}
}
22 changes: 11 additions & 11 deletions src/createSelectorCreator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defaultMemoize } from './defaultMemoize'
import { weakMapMemoize } from './weakMapMemoize'

import type {
Combiner,
Expand Down Expand Up @@ -30,13 +30,13 @@ import {
* An instance of `createSelector`, customized with a given memoize implementation.
*
* @template MemoizeFunction - The type of the memoize function that is used to memoize the `resultFunc` inside `createSelector` (e.g., `defaultMemoize` or `weakMapMemoize`).
* @template ArgsMemoizeFunction - The type of the optional memoize function that is used to memoize the arguments passed into the output selector generated by `createSelector` (e.g., `defaultMemoize` or `weakMapMemoize`). If none is explicitly provided, `defaultMemoize` will be used.
* @template ArgsMemoizeFunction - The type of the optional memoize function that is used to memoize the arguments passed into the output selector generated by `createSelector` (e.g., `defaultMemoize` or `weakMapMemoize`). If none is explicitly provided, `weakMapMemoize` will be used.
*
* @public
*/
export interface CreateSelectorFunction<
MemoizeFunction extends UnknownMemoizer = typeof defaultMemoize,
ArgsMemoizeFunction extends UnknownMemoizer = typeof defaultMemoize
MemoizeFunction extends UnknownMemoizer = typeof weakMapMemoize,
ArgsMemoizeFunction extends UnknownMemoizer = typeof weakMapMemoize
> {
/**
* Creates a memoized selector function.
Expand Down Expand Up @@ -213,7 +213,7 @@ export function setInputStabilityCheckEnabled(
* ```
*
* @template MemoizeFunction - The type of the memoize function that is used to memoize the `resultFunc` inside `createSelector` (e.g., `defaultMemoize` or `weakMapMemoize`).
* @template ArgsMemoizeFunction - The type of the optional memoize function that is used to memoize the arguments passed into the output selector generated by `createSelector` (e.g., `defaultMemoize` or `weakMapMemoize`). If none is explicitly provided, `defaultMemoize` will be used.
* @template ArgsMemoizeFunction - The type of the optional memoize function that is used to memoize the arguments passed into the output selector generated by `createSelector` (e.g., `defaultMemoize` or `weakMapMemoize`). If none is explicitly provided, `weakMapMemoize` will be used.
*
* @see {@link https://github.com/reduxjs/reselect#createselectorcreatormemoize--options-memoizeoptions createSelectorCreator}
*
Expand All @@ -222,13 +222,13 @@ export function setInputStabilityCheckEnabled(
*/
export function createSelectorCreator<
MemoizeFunction extends UnknownMemoizer,
ArgsMemoizeFunction extends UnknownMemoizer = typeof defaultMemoize
ArgsMemoizeFunction extends UnknownMemoizer = typeof weakMapMemoize
>(
options: Simplify<
SetRequired<
CreateSelectorOptions<
typeof defaultMemoize,
typeof defaultMemoize,
typeof weakMapMemoize,
typeof weakMapMemoize,
MemoizeFunction,
ArgsMemoizeFunction
>,
Expand Down Expand Up @@ -277,7 +277,7 @@ export function createSelectorCreator<MemoizeFunction extends UnknownMemoizer>(
* @returns A customized `createSelector` function.
*
* @template MemoizeFunction - The type of the memoize function that is used to memoize the `resultFunc` inside `createSelector` (e.g., `defaultMemoize` or `weakMapMemoize`).
* @template ArgsMemoizeFunction - The type of the optional memoize function that is used to memoize the arguments passed into the output selector generated by `createSelector` (e.g., `defaultMemoize` or `weakMapMemoize`). If none is explicitly provided, `defaultMemoize` will be used.
* @template ArgsMemoizeFunction - The type of the optional memoize function that is used to memoize the arguments passed into the output selector generated by `createSelector` (e.g., `defaultMemoize` or `weakMapMemoize`). If none is explicitly provided, `weakMapMemoize` will be used.
* @template MemoizeOrOptions - The type of the first argument. It can either be a `memoize` function or an `options` object containing the `memoize` function.
*/
export function createSelectorCreator<
Expand Down Expand Up @@ -372,7 +372,7 @@ export function createSelectorCreator<
const {
memoize,
memoizeOptions = [],
argsMemoize = defaultMemoize,
argsMemoize = weakMapMemoize,
argsMemoizeOptions = [],
inputStabilityCheck = globalStabilityCheck
} = combinedOptions
Expand Down Expand Up @@ -477,4 +477,4 @@ export function createSelectorCreator<
* @public
*/
export const createSelector =
/* #__PURE__ */ createSelectorCreator(defaultMemoize)
/* #__PURE__ */ createSelectorCreator(weakMapMemoize)
4 changes: 2 additions & 2 deletions src/createStructuredSelector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ export interface StructuredSelectorCreator {
* ```
*
* @template InputSelectorsObject - The shape of the input selectors object.
* @template MemoizeFunction - The type of the memoize function that is used to create the structured selector. It defaults to `defaultMemoize`.
* @template ArgsMemoizeFunction - The type of the of the memoize function that is used to memoize the arguments passed into the generated structured selector. It defaults to `defaultMemoize`.
* @template MemoizeFunction - The type of the memoize function that is used to create the structured selector. It defaults to `weakMapMemoize`.
* @template ArgsMemoizeFunction - The type of the of the memoize function that is used to memoize the arguments passed into the generated structured selector. It defaults to `weakMapMemoize`.
*
* @see {@link https://github.com/reduxjs/reselect#createstructuredselector-inputselectorsobject--selectorcreator--createselector createStructuredSelector}
*/
Expand Down
26 changes: 13 additions & 13 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { defaultMemoize } from './defaultMemoize'
import type { MergeParameters } from './versionedTypes'
import type { weakMapMemoize } from './weakMapMemoize'

export type { MergeParameters } from './versionedTypes'

Expand Down Expand Up @@ -55,15 +55,15 @@ export type SelectorResultArray<Selectors extends SelectorArray> =
* The options object used inside `createSelector` and `createSelectorCreator`.
*
* @template MemoizeFunction - The type of the memoize function that is used to memoize the `resultFunc` inside `createSelector` (e.g., `defaultMemoize` or `weakMapMemoize`).
* @template ArgsMemoizeFunction - The type of the optional memoize function that is used to memoize the arguments passed into the output selector generated by `createSelector` (e.g., `defaultMemoize` or `weakMapMemoize`). If none is explicitly provided, `defaultMemoize` will be used.
* @template ArgsMemoizeFunction - The type of the optional memoize function that is used to memoize the arguments passed into the output selector generated by `createSelector` (e.g., `defaultMemoize` or `weakMapMemoize`). If none is explicitly provided, `weakMapMemoize` will be used.
* @template OverrideMemoizeFunction - The type of the optional `memoize` function that could be passed into the options object inside `createSelector` to override the original `memoize` function that was initially passed into `createSelectorCreator`.
* @template OverrideArgsMemoizeFunction - The type of the optional `argsMemoize` function that could be passed into the options object inside `createSelector` to override the original `argsMemoize` function that was initially passed into `createSelectorCreator`. If none was initially provided, `defaultMemoize` will be used.
* @template OverrideArgsMemoizeFunction - The type of the optional `argsMemoize` function that could be passed into the options object inside `createSelector` to override the original `argsMemoize` function that was initially passed into `createSelectorCreator`. If none was initially provided, `weakMapMemoize` will be used.
*
* @public
*/
export interface CreateSelectorOptions<
MemoizeFunction extends UnknownMemoizer = typeof defaultMemoize,
ArgsMemoizeFunction extends UnknownMemoizer = typeof defaultMemoize,
MemoizeFunction extends UnknownMemoizer = typeof weakMapMemoize,
ArgsMemoizeFunction extends UnknownMemoizer = typeof weakMapMemoize,
OverrideMemoizeFunction extends UnknownMemoizer = never,
OverrideArgsMemoizeFunction extends UnknownMemoizer = never
> {
Expand Down Expand Up @@ -114,7 +114,7 @@ export interface CreateSelectorOptions<
*
* When passed directly into `createSelector`, it overrides the
* `argsMemoize` function initially passed into `createSelectorCreator`.
* If none was initially provided, `defaultMemoize` will be used.
* If none was initially provided, `weakMapMemoize` will be used.
*
* @example
* ```ts
Expand All @@ -130,7 +130,7 @@ export interface CreateSelectorOptions<
* )
* ```
*
* @default defaultMemoize
* @default weakMapMemoize
*
* @since 5.0.0
*/
Expand Down Expand Up @@ -176,15 +176,15 @@ export interface CreateSelectorOptions<
* @template InputSelectors - The type of the input selectors.
* @template Result - The type of the result returned by the `resultFunc`.
* @template MemoizeFunction - The type of the memoize function that is used to memoize the `resultFunc` inside `createSelector` (e.g., `defaultMemoize` or `weakMapMemoize`).
* @template ArgsMemoizeFunction - The type of the optional memoize function that is used to memoize the arguments passed into the output selector generated by `createSelector` (e.g., `defaultMemoize` or `weakMapMemoize`). If none is explicitly provided, `defaultMemoize` will be used.
* @template ArgsMemoizeFunction - The type of the optional memoize function that is used to memoize the arguments passed into the output selector generated by `createSelector` (e.g., `defaultMemoize` or `weakMapMemoize`). If none is explicitly provided, `weakMapMemoize` will be used.
*
* @public
*/
export type OutputSelectorFields<
InputSelectors extends SelectorArray = SelectorArray,
Result = unknown,
MemoizeFunction extends UnknownMemoizer = typeof defaultMemoize,
ArgsMemoizeFunction extends UnknownMemoizer = typeof defaultMemoize
MemoizeFunction extends UnknownMemoizer = typeof weakMapMemoize,
ArgsMemoizeFunction extends UnknownMemoizer = typeof weakMapMemoize
> = {
/**
* The final function passed to `createSelector`. Otherwise known as the `combiner`.
Expand Down Expand Up @@ -250,15 +250,15 @@ export type OutputSelectorFields<
* @template InputSelectors - The type of the input selectors.
* @template Result - The type of the result returned by the `resultFunc`.
* @template MemoizeFunction - The type of the memoize function that is used to memoize the `resultFunc` inside `createSelector` (e.g., `defaultMemoize` or `weakMapMemoize`).
* @template ArgsMemoizeFunction - The type of the optional memoize function that is used to memoize the arguments passed into the output selector generated by `createSelector` (e.g., `defaultMemoize` or `weakMapMemoize`). If none is explicitly provided, `defaultMemoize` will be used.
* @template ArgsMemoizeFunction - The type of the optional memoize function that is used to memoize the arguments passed into the output selector generated by `createSelector` (e.g., `defaultMemoize` or `weakMapMemoize`). If none is explicitly provided, `weakMapMemoize` will be used.
*
* @public
*/
export type OutputSelector<
InputSelectors extends SelectorArray = SelectorArray,
Result = unknown,
MemoizeFunction extends UnknownMemoizer = typeof defaultMemoize,
ArgsMemoizeFunction extends UnknownMemoizer = typeof defaultMemoize
MemoizeFunction extends UnknownMemoizer = typeof weakMapMemoize,
ArgsMemoizeFunction extends UnknownMemoizer = typeof weakMapMemoize
> = Selector<
GetStateFromSelectors<InputSelectors>,
Result,
Expand Down
16 changes: 13 additions & 3 deletions test/benchmarks/weakMapMemoize.bench.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { OutputSelector, Selector } from 'reselect'
import { defaultMemoize } from 'reselect'
import {
unstable_autotrackMemoize as autotrackMemoize,
createSelector,
Expand Down Expand Up @@ -38,17 +39,26 @@ describe('Parametric selectors: weakMapMemoize vs others', () => {
const selectorDefaultWithCacheSize = createSelector(
[(state: RootState) => state.todos, (state: RootState, id: number) => id],
(todos, id) => todos.find(todo => todo.id === id),
{ memoizeOptions: { maxSize: 30 } }
{ memoize: defaultMemoize, memoizeOptions: { maxSize: 30 } }
)
const selectorDefaultWithArgsCacheSize = createSelector(
[(state: RootState) => state.todos, (state: RootState, id: number) => id],
(todos, id) => todos.find(todo => todo.id === id),
{ argsMemoizeOptions: { maxSize: 30 } }
{
memoize: defaultMemoize,
argsMemoize: defaultMemoize,
argsMemoizeOptions: { maxSize: 30 }
}
)
const selectorDefaultWithBothCacheSize = createSelector(
[(state: RootState) => state.todos, (state: RootState, id: number) => id],
(todos, id) => todos.find(todo => todo.id === id),
{ memoizeOptions: { maxSize: 30 }, argsMemoizeOptions: { maxSize: 30 } }
{
memoize: defaultMemoize,
argsMemoize: defaultMemoize,
memoizeOptions: { maxSize: 30 },
argsMemoizeOptions: { maxSize: 30 }
}
)
const selectorWeakMap = createSelector(
[(state: RootState) => state.todos, (state: RootState, id: number) => id],
Expand Down
7 changes: 6 additions & 1 deletion test/defaultMemoize.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
// TODO: Add test for React Redux connect function

import { createSelector, defaultMemoize } from 'reselect'
import { defaultMemoize, createSelectorCreator } from 'reselect'
import { vi } from 'vitest'

const createSelector = createSelectorCreator({
memoize: defaultMemoize,
argsMemoize: defaultMemoize
})

describe('defaultMemoize', () => {
test('Basic memoization', () => {
let called = 0
Expand Down
7 changes: 6 additions & 1 deletion test/inputStabilityCheck.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { createSelector, setInputStabilityCheckEnabled } from 'reselect'
import {
createSelector,
defaultMemoize,
setInputStabilityCheckEnabled
} from 'reselect'
import { shallowEqual } from 'react-redux'

describe('inputStabilityCheck', () => {
Expand Down Expand Up @@ -107,6 +111,7 @@ describe('inputStabilityCheck', () => {
[unstableInput],
({ a, b }) => a + b,
{
memoize: defaultMemoize,
memoizeOptions: {
equalityCheck: shallowEqual
}
Expand Down
4 changes: 2 additions & 2 deletions test/perfComparisons.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,11 +337,11 @@ describe('More perf comparisons', () => {

cdTodoIdsAndNames(reduxStates[0])

expect(cdTodoIdsAndNames.recomputations()).toBe(NUM_ITEMS + 1)
expect(cdTodoIdsAndNames.recomputations()).toBe(NUM_ITEMS)

cdTodoIdsAndNames(reduxStates[1])

expect(cdTodoIdsAndNames.recomputations()).toBe(NUM_ITEMS + 2)
expect(cdTodoIdsAndNames.recomputations()).toBe(NUM_ITEMS)

// @ts-ignore
reduxStates[0] = null
Expand Down
Loading

0 comments on commit 5cb8537

Please sign in to comment.