From 8e81ed03177cbba3d45f2ef3773e738ce1e74f84 Mon Sep 17 00:00:00 2001 From: Arya Emami Date: Fri, 1 Dec 2023 13:44:25 -0600 Subject: [PATCH 01/13] Rename `defaultMemoize` to `lruMemoize` --- src/createStructuredSelector.ts | 14 +- src/defaultMemoize.ts | 4 +- src/index.ts | 2 +- test/benchmarks/orderOfExecution.bench.ts | 4 +- test/benchmarks/weakMapMemoize.bench.ts | 12 +- test/computationComparisons.spec.tsx | 21 ++- test/createStructuredSelector.spec.ts | 4 +- test/defaultMemoize.spec.ts | 26 ++-- test/examples.test.ts | 12 +- test/inputStabilityCheck.spec.ts | 8 +- test/perfComparisons.spec.ts | 4 +- test/reselect.spec.ts | 26 ++-- test/selectorUtils.spec.ts | 6 +- typescript_test/argsMemoize.typetest.ts | 148 +++++++++++----------- typescript_test/test.ts | 32 ++--- 15 files changed, 157 insertions(+), 166 deletions(-) diff --git a/src/createStructuredSelector.ts b/src/createStructuredSelector.ts index cfd3b8937..0d9e185db 100644 --- a/src/createStructuredSelector.ts +++ b/src/createStructuredSelector.ts @@ -1,7 +1,7 @@ import { createSelector } from './createSelectorCreator' import type { CreateSelectorFunction } from './createSelectorCreator' -import type { defaultMemoize } from './defaultMemoize' +import type { lruMemoize } from './defaultMemoize' import type { InterruptRecursion, ObjectValuesToTuple, @@ -36,8 +36,8 @@ export interface TypedStructuredSelectorCreator { } = { [Key in keyof RootState]: Selector }, - MemoizeFunction extends UnknownMemoizer = typeof defaultMemoize, - ArgsMemoizeFunction extends UnknownMemoizer = typeof defaultMemoize + MemoizeFunction extends UnknownMemoizer = typeof lruMemoize, + ArgsMemoizeFunction extends UnknownMemoizer = typeof lruMemoize >( selectors: InputSelectorsObject, selectorCreator?: CreateSelectorFunction< @@ -140,8 +140,8 @@ export interface StructuredSelectorCreator { */ < InputSelectorsObject extends SelectorsObject, - MemoizeFunction extends UnknownMemoizer = typeof defaultMemoize, - ArgsMemoizeFunction extends UnknownMemoizer = typeof defaultMemoize + MemoizeFunction extends UnknownMemoizer = typeof lruMemoize, + ArgsMemoizeFunction extends UnknownMemoizer = typeof lruMemoize >( inputSelectorsObject: InputSelectorsObject, selectorCreator?: CreateSelectorFunction< @@ -209,8 +209,8 @@ export interface StructuredSelectorCreator { */ export const createStructuredSelector: StructuredSelectorCreator = (< InputSelectorsObject extends SelectorsObject, - MemoizeFunction extends UnknownMemoizer = typeof defaultMemoize, - ArgsMemoizeFunction extends UnknownMemoizer = typeof defaultMemoize + MemoizeFunction extends UnknownMemoizer = typeof lruMemoize, + ArgsMemoizeFunction extends UnknownMemoizer = typeof lruMemoize >( inputSelectorsObject: InputSelectorsObject, selectorCreator: CreateSelectorFunction< diff --git a/src/defaultMemoize.ts b/src/defaultMemoize.ts index d590ebefd..ae8ead97f 100644 --- a/src/defaultMemoize.ts +++ b/src/defaultMemoize.ts @@ -94,7 +94,7 @@ function createLruCache(maxSize: number, equals: EqualityFn): Cache { /** * Runs a simple reference equality check. - * What {@linkcode defaultMemoize defaultMemoize} uses by default. + * What {@linkcode lruMemoize defaultMemoize} uses by default. * * @public */ @@ -165,7 +165,7 @@ export interface DefaultMemoizeOptions { * * @public */ -export function defaultMemoize( +export function lruMemoize( func: Func, equalityCheckOrOptions?: EqualityFn | DefaultMemoizeOptions> ) { diff --git a/src/index.ts b/src/index.ts index fe1bd6573..d83e632b1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,7 +6,7 @@ export type { StructuredSelectorCreator, TypedStructuredSelectorCreator } from './createStructuredSelector' -export { defaultEqualityCheck, defaultMemoize } from './defaultMemoize' +export { defaultEqualityCheck, lruMemoize } from './defaultMemoize' export type { DefaultMemoizeOptions } from './defaultMemoize' export { setGlobalDevModeChecks } from './devModeChecks/setGlobalDevModeChecks' export type { diff --git a/test/benchmarks/orderOfExecution.bench.ts b/test/benchmarks/orderOfExecution.bench.ts index 210b489d7..34014619c 100644 --- a/test/benchmarks/orderOfExecution.bench.ts +++ b/test/benchmarks/orderOfExecution.bench.ts @@ -1,5 +1,5 @@ import type { OutputSelector, Selector } from 'reselect' -import { createSelector, defaultMemoize } from 'reselect' +import { createSelector, lruMemoize } from 'reselect' import type { Options } from 'tinybench' import { bench } from 'vitest' import type { RootState } from '../testUtils' @@ -121,7 +121,7 @@ describe('Reselect vs standalone memoization for field access', () => { users => users.appSettings ) const fieldAccessorWithMemoize = countRecomputations( - defaultMemoize((state: RootState) => { + lruMemoize((state: RootState) => { return state.users.appSettings }) ) diff --git a/test/benchmarks/weakMapMemoize.bench.ts b/test/benchmarks/weakMapMemoize.bench.ts index cf3c77fd6..dd4d5e262 100644 --- a/test/benchmarks/weakMapMemoize.bench.ts +++ b/test/benchmarks/weakMapMemoize.bench.ts @@ -1,8 +1,8 @@ import type { OutputSelector, Selector } from 'reselect' -import { defaultMemoize } from 'reselect' import { unstable_autotrackMemoize as autotrackMemoize, createSelector, + lruMemoize, weakMapMemoize } from 'reselect' import { bench } from 'vitest' @@ -39,14 +39,14 @@ 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), - { memoize: defaultMemoize, memoizeOptions: { maxSize: 30 } } + { memoize: lruMemoize, memoizeOptions: { maxSize: 30 } } ) const selectorDefaultWithArgsCacheSize = createSelector( [(state: RootState) => state.todos, (state: RootState, id: number) => id], (todos, id) => todos.find(todo => todo.id === id), { - memoize: defaultMemoize, - argsMemoize: defaultMemoize, + memoize: lruMemoize, + argsMemoize: lruMemoize, argsMemoizeOptions: { maxSize: 30 } } ) @@ -54,8 +54,8 @@ describe('Parametric selectors: weakMapMemoize vs others', () => { [(state: RootState) => state.todos, (state: RootState, id: number) => id], (todos, id) => todos.find(todo => todo.id === id), { - memoize: defaultMemoize, - argsMemoize: defaultMemoize, + memoize: lruMemoize, + argsMemoize: lruMemoize, memoizeOptions: { maxSize: 30 }, argsMemoizeOptions: { maxSize: 30 } } diff --git a/test/computationComparisons.spec.tsx b/test/computationComparisons.spec.tsx index a939c8588..e23266382 100644 --- a/test/computationComparisons.spec.tsx +++ b/test/computationComparisons.spec.tsx @@ -8,19 +8,14 @@ import type { TypedUseSelectorHook } from 'react-redux' import { Provider, shallowEqual, useSelector } from 'react-redux' import { createSelector, + lruMemoize, unstable_autotrackMemoize, - weakMapMemoize, - defaultMemoize + weakMapMemoize } from 'reselect' import type { OutputSelector } from 'reselect' import type { RootState, Todo } from './testUtils' -import { - addTodo, - logSelectorRecomputations, - setupStore, - toggleCompleted -} from './testUtils' +import { addTodo, setupStore, toggleCompleted } from './testUtils' describe('Computations and re-rendering with React components', () => { const selector = createSelector( @@ -44,8 +39,8 @@ describe('Computations and re-rendering with React components', () => { type SelectTodoIds = OutputSelector< [(state: RootState) => RootState['todos']], number[], - typeof defaultMemoize | typeof weakMapMemoize, - typeof defaultMemoize | typeof weakMapMemoize + typeof lruMemoize | typeof weakMapMemoize, + typeof lruMemoize | typeof weakMapMemoize > type SelectTodoById = OutputSelector< @@ -54,8 +49,8 @@ describe('Computations and re-rendering with React components', () => { (state: RootState, id: number) => number ], readonly [todo: Todo | undefined], - typeof defaultMemoize | typeof weakMapMemoize, - typeof defaultMemoize | typeof weakMapMemoize + typeof lruMemoize | typeof weakMapMemoize, + typeof lruMemoize | typeof weakMapMemoize > const selectTodos = (state: RootState) => state.todos @@ -99,7 +94,7 @@ describe('Computations and re-rendering with React components', () => { [selectTodos, selectTodoId], mapTodoById, { - memoize: defaultMemoize, + memoize: lruMemoize, memoizeOptions: { resultEqualityCheck: shallowEqual, maxSize: 500 } } ) diff --git a/test/createStructuredSelector.spec.ts b/test/createStructuredSelector.spec.ts index 56cc78013..dc719b957 100644 --- a/test/createStructuredSelector.spec.ts +++ b/test/createStructuredSelector.spec.ts @@ -2,7 +2,7 @@ import { createSelector, createSelectorCreator, createStructuredSelector, - defaultMemoize + lruMemoize } from 'reselect' import type { LocalTestContext, RootState } from './testUtils' import { setupStore } from './testUtils' @@ -47,7 +47,7 @@ describe('createStructureSelector', () => { test('structured selector with custom selector creator', () => { const customSelectorCreator = createSelectorCreator( - defaultMemoize, + lruMemoize, (a, b) => a === b ) const selector = createStructuredSelector( diff --git a/test/defaultMemoize.spec.ts b/test/defaultMemoize.spec.ts index 502c29d44..c603ea051 100644 --- a/test/defaultMemoize.spec.ts +++ b/test/defaultMemoize.spec.ts @@ -1,17 +1,17 @@ // TODO: Add test for React Redux connect function -import { defaultMemoize, createSelectorCreator } from 'reselect' +import { createSelectorCreator, lruMemoize } from 'reselect' import { vi } from 'vitest' const createSelector = createSelectorCreator({ - memoize: defaultMemoize, - argsMemoize: defaultMemoize + memoize: lruMemoize, + argsMemoize: lruMemoize }) describe('defaultMemoize', () => { test('Basic memoization', () => { let called = 0 - const memoized = defaultMemoize(state => { + const memoized = lruMemoize(state => { called++ return state.a }) @@ -26,7 +26,7 @@ describe('defaultMemoize', () => { }) test('Memoizes with multiple arguments', () => { - const memoized = defaultMemoize((...args) => + const memoized = lruMemoize((...args) => args.reduce((sum, value) => sum + value, 0) ) expect(memoized(1, 2)).toBe(3) @@ -37,7 +37,7 @@ describe('defaultMemoize', () => { // a rather absurd equals operation we can verify in tests let called = 0 const valueEquals = (a: any, b: any) => typeof a === typeof b - const memoized = defaultMemoize(a => { + const memoized = lruMemoize(a => { called++ return a }, valueEquals) @@ -73,7 +73,7 @@ describe('defaultMemoize', () => { const someObject = { foo: 'bar' } const anotherObject = { foo: 'bar' } - const memoized = defaultMemoize(a => a, shallowEqual) + const memoized = lruMemoize(a => a, shallowEqual) // the first call to `memoized` doesn't hit because `defaultMemoize.lastArgs` is uninitialized // and so `equalityCheck` is never called @@ -105,7 +105,7 @@ describe('defaultMemoize', () => { test('Accepts a max size greater than 1 with LRU cache behavior', () => { let funcCalls = 0 - const memoizer = defaultMemoize( + const memoizer = lruMemoize( (state: any) => { funcCalls++ return state @@ -206,7 +206,7 @@ describe('defaultMemoize', () => { for (const maxSize of [1, 3]) { let funcCalls = 0 - const memoizer = defaultMemoize( + const memoizer = lruMemoize( (state: Todo[]) => { funcCalls++ return state.map(todo => todo.id) @@ -235,7 +235,7 @@ describe('defaultMemoize', () => { const equalityCheck = vi.fn((a, b) => a === b) const resultEqualityCheck = vi.fn((a, b) => typeof a === typeof b) - const memoizedFn = defaultMemoize(selector, { + const memoizedFn = lruMemoize(selector, { maxSize: 1, resultEqualityCheck, equalityCheck @@ -298,7 +298,7 @@ describe('defaultMemoize', () => { test('Accepts an options object as an arg', () => { let memoizer1Calls = 0 - const acceptsEqualityCheckAsOption = defaultMemoize((a: any) => a, { + const acceptsEqualityCheckAsOption = lruMemoize((a: any) => a, { equalityCheck: (a, b) => { memoizer1Calls++ return a === b @@ -311,7 +311,7 @@ describe('defaultMemoize', () => { expect(memoizer1Calls).toBeGreaterThan(0) let called = 0 - const fallsBackToDefaultEqualityIfNoArgGiven = defaultMemoize( + const fallsBackToDefaultEqualityIfNoArgGiven = lruMemoize( state => { called++ return state.a @@ -334,7 +334,7 @@ describe('defaultMemoize', () => { let funcCalls = 0 // Cache size of 1 - const memoizer = defaultMemoize( + const memoizer = lruMemoize( (state: any) => { funcCalls++ return state diff --git a/test/examples.test.ts b/test/examples.test.ts index 9a54026e6..c8f1a68f6 100644 --- a/test/examples.test.ts +++ b/test/examples.test.ts @@ -8,7 +8,7 @@ import { unstable_autotrackMemoize as autotrackMemoize, createSelector, createSelectorCreator, - defaultMemoize, + lruMemoize, weakMapMemoize } from 'reselect' import { test } from 'vitest' @@ -81,7 +81,7 @@ test.todo('Find Fastest Selector', () => { selector: S, ...selectorArgs: Parameters ) => { - const memoizeFuncs = [defaultMemoize, weakMapMemoize, autotrackMemoize] + const memoizeFuncs = [lruMemoize, weakMapMemoize, autotrackMemoize] const results = memoizeFuncs .map(memoize => { const alternateSelector = createSelector( @@ -126,8 +126,8 @@ test.todo('Find Fastest Selector', () => { test('TypedCreateSelector', () => { type TypedCreateSelector< State, - MemoizeFunction extends UnknownMemoizer = typeof defaultMemoize, - ArgsMemoizeFunction extends UnknownMemoizer = typeof defaultMemoize + MemoizeFunction extends UnknownMemoizer = typeof lruMemoize, + ArgsMemoizeFunction extends UnknownMemoizer = typeof lruMemoize > = < InputSelectors extends readonly Selector[], Result, @@ -178,8 +178,8 @@ test('createCurriedSelector copy paste pattern', () => { const createCurriedSelector = < InputSelectors extends SelectorArray, Result, - OverrideMemoizeFunction extends UnknownMemoizer = typeof defaultMemoize, - OverrideArgsMemoizeFunction extends UnknownMemoizer = typeof defaultMemoize + OverrideMemoizeFunction extends UnknownMemoizer = typeof lruMemoize, + OverrideArgsMemoizeFunction extends UnknownMemoizer = typeof lruMemoize >( ...args: Parameters< typeof createSelector< diff --git a/test/inputStabilityCheck.spec.ts b/test/inputStabilityCheck.spec.ts index f419f4387..cde7e15ff 100644 --- a/test/inputStabilityCheck.spec.ts +++ b/test/inputStabilityCheck.spec.ts @@ -1,9 +1,5 @@ -import { - createSelector, - defaultMemoize, - setGlobalDevModeChecks -} from 'reselect' import { shallowEqual } from 'react-redux' +import { createSelector, lruMemoize, setGlobalDevModeChecks } from 'reselect' describe('inputStabilityCheck', () => { const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}) @@ -153,7 +149,7 @@ describe('inputStabilityCheck', () => { [unstableInput], ({ a, b }) => a + b, { - memoize: defaultMemoize, + memoize: lruMemoize, memoizeOptions: { equalityCheck: shallowEqual } diff --git a/test/perfComparisons.spec.ts b/test/perfComparisons.spec.ts index ae66c01c0..282857bf7 100644 --- a/test/perfComparisons.spec.ts +++ b/test/perfComparisons.spec.ts @@ -3,7 +3,7 @@ import { configureStore, createSlice } from '@reduxjs/toolkit' import { unstable_autotrackMemoize as autotrackMemoize, createSelectorCreator, - defaultMemoize, + lruMemoize, weakMapMemoize } from 'reselect' import { vi } from 'vitest' @@ -18,7 +18,7 @@ describe('More perf comparisons', () => { process.env.NODE_NV = originalEnv }) - const csDefault = createSelectorCreator(defaultMemoize) + const csDefault = createSelectorCreator(lruMemoize) const csAutotrack = createSelectorCreator(autotrackMemoize) interface Todo { diff --git a/test/reselect.spec.ts b/test/reselect.spec.ts index 15619f03a..1f63cac86 100644 --- a/test/reselect.spec.ts +++ b/test/reselect.spec.ts @@ -3,10 +3,10 @@ import lodashMemoize from 'lodash/memoize' import microMemoize from 'micro-memoize' import { + unstable_autotrackMemoize as autotrackMemoize, createSelector, createSelectorCreator, - defaultMemoize, - unstable_autotrackMemoize as autotrackMemoize, + lruMemoize, weakMapMemoize } from 'reselect' @@ -299,7 +299,7 @@ describe('Combining selectors', () => { test('override valueEquals', () => { // a rather absurd equals operation we can verify in tests const createOverridenSelector = createSelectorCreator( - defaultMemoize, + lruMemoize, (a, b) => typeof a === typeof b ) const selector = createOverridenSelector( @@ -348,7 +348,7 @@ describe('Customizing selectors', () => { (state: StateAB) => state.b, (a, b) => a + b, { - memoize: defaultMemoize, + memoize: lruMemoize, memoizeOptions: (a, b) => { memoizer1Calls++ return a === b @@ -366,7 +366,7 @@ describe('Customizing selectors', () => { (state: StateAB) => state.b, (a, b) => a + b, { - memoize: defaultMemoize, + memoize: lruMemoize, memoizeOptions: [ (a, b) => { memoizer2Calls++ @@ -382,7 +382,7 @@ describe('Customizing selectors', () => { expect(memoizer2Calls).toBeGreaterThan(0) const createSelectorWithSeparateArg = createSelectorCreator( - defaultMemoize, + lruMemoize, (a, b) => { memoizer3Calls++ return a === b @@ -409,7 +409,7 @@ describe('Customizing selectors', () => { (state: RootState) => state.todos, todos => todos.map(({ id }) => id), { - memoize: defaultMemoize, + memoize: lruMemoize, devModeChecks: { inputStabilityCheck: 'always' }, memoizeOptions: { equalityCheck: (a, b) => false, @@ -442,7 +442,7 @@ describe('argsMemoize and memoize', () => { const selectorDefaultParametric = createSelector( [(state: RootState, id: number) => id, (state: RootState) => state.todos], (id, todos) => todos.filter(todo => todo.id === id), - { memoize: defaultMemoize } + { memoize: lruMemoize } ) selectorDefaultParametric(state, 0) selectorDefaultParametric(state, 1) @@ -590,7 +590,7 @@ describe('argsMemoize and memoize', () => { const selectorDefault = otherCreateSelector( [(state: RootState) => state.todos], todos => todos.map(({ id }) => id), - { memoize: defaultMemoize, argsMemoize: defaultMemoize } + { memoize: lruMemoize, argsMemoize: lruMemoize } ) const selectorAutotrack = createSelector( [(state: RootState) => state.todos], @@ -675,7 +675,7 @@ describe('argsMemoize and memoize', () => { [(state: RootState) => state.todos], todos => todos.map(({ id }) => id), { - memoize: defaultMemoize, + memoize: lruMemoize, // WARNING!! This is just for testing purposes, do not use `autotrackMemoize` to memoize the arguments, // it can return false positives, since it's not tracking a nested field. argsMemoize: autotrackMemoize @@ -827,7 +827,7 @@ describe('argsMemoize and memoize', () => { const selectorMicroMemoizeOverridden = createSelectorMicroMemoize( [(state: RootState) => state.todos], todos => todos.map(({ id }) => id), - { memoize: defaultMemoize, argsMemoize: defaultMemoize } + { memoize: lruMemoize, argsMemoize: lruMemoize } ) expect(selectorMicroMemoizeOverridden(state)).to.be.an('array').that.is.not .empty @@ -894,7 +894,7 @@ describe('argsMemoize and memoize', () => { (state: RootState) => state.todos, todos => todos.map(({ id }) => id), { - argsMemoize: defaultMemoize, + argsMemoize: lruMemoize, argsMemoizeOptions: { resultEqualityCheck: (a, b) => a === b } } ) @@ -969,7 +969,7 @@ describe('argsMemoize and memoize', () => { const selectorMicroMemoizeOverrideMemoizeOnly = createSelectorMicroMemoize( [(state: RootState) => state.todos], todos => todos.map(({ id }) => id), - { memoize: defaultMemoize } + { memoize: lruMemoize } ) expect(selectorMicroMemoizeOverrideMemoizeOnly(state)).to.be.an('array') .that.is.not.empty diff --git a/test/selectorUtils.spec.ts b/test/selectorUtils.spec.ts index 281af2507..47b3508dd 100644 --- a/test/selectorUtils.spec.ts +++ b/test/selectorUtils.spec.ts @@ -1,4 +1,4 @@ -import { createSelector, defaultMemoize } from 'reselect' +import { createSelector, lruMemoize } from 'reselect' import type { StateA, StateAB } from 'testTypes' describe('createSelector exposed utils', () => { @@ -7,8 +7,8 @@ describe('createSelector exposed utils', () => { (state: StateA) => state.a, a => a, { - memoize: defaultMemoize, - argsMemoize: defaultMemoize, + memoize: lruMemoize, + argsMemoize: lruMemoize, devModeChecks: { identityFunctionCheck: 'never' } } ) diff --git a/typescript_test/argsMemoize.typetest.ts b/typescript_test/argsMemoize.typetest.ts index a9f7b4c8e..4dde2d54a 100644 --- a/typescript_test/argsMemoize.typetest.ts +++ b/typescript_test/argsMemoize.typetest.ts @@ -4,7 +4,7 @@ import { unstable_autotrackMemoize as autotrackMemoize, createSelector, createSelectorCreator, - defaultMemoize, + lruMemoize, weakMapMemoize } from 'reselect' import { expectExactType } from './typesTestUtils' @@ -26,22 +26,22 @@ function overrideOnlyMemoizeInCreateSelector() { const selectorDefaultSeparateInlineArgs = createSelector( (state: RootState) => state.todos, todos => todos.map(t => t.id), - { memoize: defaultMemoize } + { memoize: lruMemoize } ) const selectorDefaultArgsAsArray = createSelector( [(state: RootState) => state.todos], todos => todos.map(t => t.id), - { memoize: defaultMemoize } + { memoize: lruMemoize } ) const selectorDefaultArgsAsArrayWithMemoizeOptions = createSelector( [(state: RootState) => state.todos], todos => todos.map(t => t.id), - { memoize: defaultMemoize, memoizeOptions: { maxSize: 2 } } + { memoize: lruMemoize, memoizeOptions: { maxSize: 2 } } ) const selectorDefaultSeparateInlineArgsWithMemoizeOptions = createSelector( (state: RootState) => state.todos, todos => todos.map(t => t.id), - { memoize: defaultMemoize, memoizeOptions: { maxSize: 2 } } + { memoize: lruMemoize, memoizeOptions: { maxSize: 2 } } ) const selectorAutotrackSeparateInlineArgs = createSelector( (state: RootState) => state.todos, @@ -91,7 +91,7 @@ function overrideOnlyMemoizeInCreateSelector() { todos => todos.map(t => t.id), { memoize: weakMapMemoize, memoizeOptions: { maxSize: 2 } } ) - const createSelectorDefault = createSelectorCreator(defaultMemoize) + const createSelectorDefault = createSelectorCreator(lruMemoize) const createSelectorWeakMap = createSelectorCreator(weakMapMemoize) const createSelectorAutotrack = createSelectorCreator(autotrackMemoize) const changeMemoizeMethodSelectorDefault = createSelectorDefault( @@ -102,15 +102,15 @@ function overrideOnlyMemoizeInCreateSelector() { const changeMemoizeMethodSelectorWeakMap = createSelectorWeakMap( (state: RootState) => state.todos, todos => todos.map(t => t.id), - { memoize: defaultMemoize } + { memoize: lruMemoize } ) const changeMemoizeMethodSelectorAutotrack = createSelectorAutotrack( (state: RootState) => state.todos, todos => todos.map(t => t.id), - { memoize: defaultMemoize } + { memoize: lruMemoize } ) const changeMemoizeMethodSelectorDefaultWithMemoizeOptions = - // @ts-expect-error When memoize is changed to weakMapMemoize or autotrackMemoize, memoizeOptions cannot be the same type as options args in defaultMemoize. + // @ts-expect-error When memoize is changed to weakMapMemoize or autotrackMemoize, memoizeOptions cannot be the same type as options args in lruMemoize. createSelectorDefault( (state: RootState) => state.todos, // @ts-expect-error @@ -121,13 +121,13 @@ function overrideOnlyMemoizeInCreateSelector() { createSelectorWeakMap( (state: RootState) => state.todos, todos => todos.map(t => t.id), - { memoize: defaultMemoize, memoizeOptions: { maxSize: 2 } } // When memoize is changed to defaultMemoize, memoizeOptions can now be the same type as options args in defaultMemoize. + { memoize: lruMemoize, memoizeOptions: { maxSize: 2 } } // When memoize is changed to lruMemoize, memoizeOptions can now be the same type as options args in lruMemoize. ) const changeMemoizeMethodSelectorAutotrackWithMemoizeOptions = createSelectorAutotrack( (state: RootState) => state.todos, todos => todos.map(t => t.id), - { memoize: defaultMemoize, memoizeOptions: { maxSize: 2 } } // When memoize is changed to defaultMemoize, memoizeOptions can now be the same type as options args in defaultMemoize. + { memoize: lruMemoize, memoizeOptions: { maxSize: 2 } } // When memoize is changed to lruMemoize, memoizeOptions can now be the same type as options args in lruMemoize. ) } @@ -135,22 +135,22 @@ function overrideOnlyArgsMemoizeInCreateSelector() { const selectorDefaultSeparateInlineArgs = createSelector( (state: RootState) => state.todos, todos => todos.map(t => t.id), - { argsMemoize: defaultMemoize } + { argsMemoize: lruMemoize } ) const selectorDefaultArgsAsArray = createSelector( [(state: RootState) => state.todos], todos => todos.map(t => t.id), - { argsMemoize: defaultMemoize } + { argsMemoize: lruMemoize } ) const selectorDefaultArgsAsArrayWithMemoizeOptions = createSelector( [(state: RootState) => state.todos], todos => todos.map(t => t.id), - { argsMemoize: defaultMemoize, argsMemoizeOptions: { maxSize: 2 } } + { argsMemoize: lruMemoize, argsMemoizeOptions: { maxSize: 2 } } ) const selectorDefaultSeparateInlineArgsWithMemoizeOptions = createSelector( (state: RootState) => state.todos, todos => todos.map(t => t.id), - { argsMemoize: defaultMemoize, argsMemoizeOptions: { maxSize: 2 } } + { argsMemoize: lruMemoize, argsMemoizeOptions: { maxSize: 2 } } ) const selectorAutotrackSeparateInlineArgs = createSelector( (state: RootState) => state.todos, @@ -224,7 +224,7 @@ function overrideOnlyArgsMemoizeInCreateSelector() { // @ts-expect-error todos => todos.map(t => t.id), { - memoize: defaultMemoize, + memoize: lruMemoize, argsMemoize: weakMapMemoize, memoizeOptions: { equalityCheck: @@ -235,9 +235,9 @@ function overrideOnlyArgsMemoizeInCreateSelector() { argsMemoizeOptions: { maxSize: 2 } } ) - // const createSelectorDefaultMemoize = createSelectorCreator(defaultMemoize) + // const createSelectorDefaultMemoize = createSelectorCreator(lruMemoize) const createSelectorDefaultMemoize = createSelectorCreator({ - memoize: defaultMemoize + memoize: lruMemoize }) const selectorWeakMapSeparateInlineArgsWithMemoizeOptions3 = // @ts-expect-error When argsMemoize is weakMapMemoize, type of argsMemoizeOptions needs to be the same as options args in weakMapMemoize. @@ -246,7 +246,7 @@ function overrideOnlyArgsMemoizeInCreateSelector() { // @ts-expect-error todos => todos.map(t => t.id), { - memoize: defaultMemoize, + memoize: lruMemoize, argsMemoize: weakMapMemoize, // memoizeOptions: [], memoizeOptions: [ @@ -285,7 +285,7 @@ function overrideOnlyArgsMemoizeInCreateSelector() { // argsMemoizeOptions: (a, b) => a === b } ) - const createSelectorDefault = createSelectorCreator(defaultMemoize) + const createSelectorDefault = createSelectorCreator(lruMemoize) const createSelectorWeakMap = createSelectorCreator(weakMapMemoize) const createSelectorAutotrack = createSelectorCreator(autotrackMemoize) const changeMemoizeMethodSelectorDefault = createSelectorDefault( @@ -296,15 +296,15 @@ function overrideOnlyArgsMemoizeInCreateSelector() { const changeMemoizeMethodSelectorWeakMap = createSelectorWeakMap( (state: RootState) => state.todos, todos => todos.map(t => t.id), - { argsMemoize: defaultMemoize } + { argsMemoize: lruMemoize } ) const changeMemoizeMethodSelectorAutotrack = createSelectorAutotrack( (state: RootState) => state.todos, todos => todos.map(t => t.id), - { argsMemoize: defaultMemoize } + { argsMemoize: lruMemoize } ) const changeMemoizeMethodSelectorDefaultWithMemoizeOptions = - // @ts-expect-error When argsMemoize is changed to weakMapMemoize or autotrackMemoize, argsMemoizeOptions cannot be the same type as options args in defaultMemoize. + // @ts-expect-error When argsMemoize is changed to weakMapMemoize or autotrackMemoize, argsMemoizeOptions cannot be the same type as options args in lruMemoize. createSelectorDefault( (state: RootState) => state.todos, // @ts-expect-error @@ -315,13 +315,13 @@ function overrideOnlyArgsMemoizeInCreateSelector() { createSelectorWeakMap( (state: RootState) => state.todos, todos => todos.map(t => t.id), - { argsMemoize: defaultMemoize, argsMemoizeOptions: { maxSize: 2 } } // When argsMemoize is changed to defaultMemoize, argsMemoizeOptions can now be the same type as options args in defaultMemoize. + { argsMemoize: lruMemoize, argsMemoizeOptions: { maxSize: 2 } } // When argsMemoize is changed to lruMemoize, argsMemoizeOptions can now be the same type as options args in lruMemoize. ) const changeMemoizeMethodSelectorAutotrackWithMemoizeOptions = createSelectorAutotrack( (state: RootState) => state.todos, todos => todos.map(t => t.id), - { argsMemoize: defaultMemoize, argsMemoizeOptions: { maxSize: 2 } } // When argsMemoize is changed to defaultMemoize, argsMemoizeOptions can now be the same type as options args in defaultMemoize. + { argsMemoize: lruMemoize, argsMemoizeOptions: { maxSize: 2 } } // When argsMemoize is changed to lruMemoize, argsMemoizeOptions can now be the same type as options args in lruMemoize. ) } @@ -380,16 +380,16 @@ function overrideMemoizeAndArgsMemoizeInCreateSelector() { // Checking to see if types dynamically change if memoize or argsMemoize are overridden inside `createSelector`. // `microMemoize` was initially passed into `createSelectorCreator` - // as `memoize` and `argsMemoize`, After overriding them both to `defaultMemoize`, + // as `memoize` and `argsMemoize`, After overriding them both to `lruMemoize`, // not only does the type for `memoizeOptions` and `argsMemoizeOptions` change to - // the options parameter of `defaultMemoize`, the output selector fields - // also change their type to the return type of `defaultMemoize`. + // the options parameter of `lruMemoize`, the output selector fields + // also change their type to the return type of `lruMemoize`. const selectorMicroMemoizeOverridden = createSelectorMicroMemoize( (state: RootState) => state.todos, todos => todos.map(t => t.id), { - memoize: defaultMemoize, - argsMemoize: defaultMemoize, + memoize: lruMemoize, + argsMemoize: lruMemoize, memoizeOptions: { equalityCheck: (a, b) => a === b, maxSize: 2 }, argsMemoizeOptions: { equalityCheck: (a, b) => a === b, maxSize: 3 } } @@ -445,8 +445,8 @@ function overrideMemoizeAndArgsMemoizeInCreateSelector() { [(state: RootState) => state.todos], todos => todos.map(({ id }) => id), { - memoize: defaultMemoize, - argsMemoize: defaultMemoize, + memoize: lruMemoize, + argsMemoize: lruMemoize, memoizeOptions: { equalityCheck: (a, b) => a === b, maxSize: 2 }, argsMemoizeOptions: { equalityCheck: (a, b) => a === b, maxSize: 3 } } @@ -503,7 +503,7 @@ function overrideMemoizeAndArgsMemoizeInCreateSelector() { (state: RootState) => state.todos, todos => todos.map(({ id }) => id), { - argsMemoize: defaultMemoize, + argsMemoize: lruMemoize, memoizeOptions: { isPromise: false, resultEqualityCheck: @@ -518,7 +518,7 @@ function overrideMemoizeAndArgsMemoizeInCreateSelector() { (state: RootState) => state.todos, todos => todos.map(({ id }) => id), { - argsMemoize: defaultMemoize, + argsMemoize: lruMemoize, memoizeOptions: { isPromise: false }, argsMemoizeOptions: { resultEqualityCheck: (a, b) => a === b } } @@ -578,7 +578,7 @@ function overrideMemoizeAndArgsMemoizeInCreateSelector() { (state: RootState) => state.todos, todos => todos.map(t => t.id), { - memoize: defaultMemoize, + memoize: lruMemoize, memoizeOptions: { resultEqualityCheck: (a, b) => a === b } } ) @@ -636,32 +636,32 @@ function overrideMemoizeAndArgsMemoizeInCreateSelector() { ) const selectorMicroMemoizePartiallyOverridden = - // @ts-expect-error Since `argsMemoize` is set to `defaultMemoize`, `argsMemoizeOptions` must match the options object parameter of `defaultMemoize` + // @ts-expect-error Since `argsMemoize` is set to `lruMemoize`, `argsMemoizeOptions` must match the options object parameter of `lruMemoize` createSelectorMicroMemoize( (state: RootState) => state.todos, // @ts-expect-error todos => todos.map(t => t.id), { - memoize: defaultMemoize, - argsMemoize: defaultMemoize, + memoize: lruMemoize, + argsMemoize: lruMemoize, memoizeOptions: { equalityCheck: // @ts-expect-error (a, b) => a === b, maxSize: 2 }, - argsMemoizeOptions: { isPromise: false } // This field causes a type error since it does not match the options param of `defaultMemoize`. + argsMemoizeOptions: { isPromise: false } // This field causes a type error since it does not match the options param of `lruMemoize`. } ) const selectorMicroMemoizePartiallyOverridden1 = - // @ts-expect-error Since `argsMemoize` is set to `defaultMemoize`, `argsMemoizeOptions` must match the options object parameter of `defaultMemoize` + // @ts-expect-error Since `argsMemoize` is set to `lruMemoize`, `argsMemoizeOptions` must match the options object parameter of `lruMemoize` createSelectorMicroMemoize( (state: RootState) => state.todos, // @ts-expect-error todos => todos.map(t => t.id), { - memoize: defaultMemoize, - argsMemoize: defaultMemoize, + memoize: lruMemoize, + argsMemoize: lruMemoize, memoizeOptions: [ { equalityCheck: @@ -670,7 +670,7 @@ function overrideMemoizeAndArgsMemoizeInCreateSelector() { maxSize: 2 } ], - argsMemoizeOptions: [{ isPromise: false }] // This field causes a type error since it does not match the options param of `defaultMemoize`. + argsMemoizeOptions: [{ isPromise: false }] // This field causes a type error since it does not match the options param of `lruMemoize`. } ) const selectorMicroMemoizePartiallyOverridden2 = createSelectorMicroMemoize( @@ -759,7 +759,7 @@ function overrideMemoizeAndArgsMemoizeInCreateSelector() { function memoizeAndArgsMemoizeInCreateSelectorCreator() { // If we don't pass in `argsMemoize`, the type for `argsMemoizeOptions` - // falls back to the options parameter of `defaultMemoize`. + // falls back to the options parameter of `lruMemoize`. const createSelectorArgsMemoizeOptionsFallbackToDefault = createSelectorCreator({ memoize: microMemoize, @@ -832,7 +832,7 @@ function memoizeAndArgsMemoizeInCreateSelectorCreator() { ) const createSelectorWithWrongArgsMemoizeOptions = - // @ts-expect-error If we don't pass in `argsMemoize`, the type for `argsMemoizeOptions` falls back to the options parameter of `defaultMemoize`. + // @ts-expect-error If we don't pass in `argsMemoize`, the type for `argsMemoizeOptions` falls back to the options parameter of `lruMemoize`. createSelectorCreator({ memoize: microMemoize, memoizeOptions: { isEqual: (a, b) => a === b }, @@ -953,91 +953,91 @@ function deepNesting2() { const selector0 = createSelector(readOne, one => one) const selector1 = createSelector(selector0, s => s, { - memoize: defaultMemoize + memoize: lruMemoize }) const selector2 = createSelector(selector1, s => s, { - memoize: defaultMemoize + memoize: lruMemoize }) const selector3 = createSelector(selector2, s => s, { - memoize: defaultMemoize + memoize: lruMemoize }) const selector4 = createSelector(selector3, s => s, { - memoize: defaultMemoize + memoize: lruMemoize }) const selector5 = createSelector(selector4, s => s, { - memoize: defaultMemoize + memoize: lruMemoize }) const selector6 = createSelector(selector5, s => s, { - memoize: defaultMemoize + memoize: lruMemoize }) const selector7 = createSelector(selector6, s => s, { - memoize: defaultMemoize + memoize: lruMemoize }) const selector8 = createSelector(selector7, s => s, { - memoize: defaultMemoize + memoize: lruMemoize }) const selector9 = createSelector(selector8, s => s, { - memoize: defaultMemoize + memoize: lruMemoize }) const selector10 = createSelector(selector9, s => s, { - memoize: defaultMemoize + memoize: lruMemoize }) const selector11 = createSelector(selector10, s => s, { - memoize: defaultMemoize + memoize: lruMemoize }) const selector12 = createSelector(selector11, s => s, { - memoize: defaultMemoize + memoize: lruMemoize }) const selector13 = createSelector(selector12, s => s, { - memoize: defaultMemoize + memoize: lruMemoize }) const selector14 = createSelector(selector13, s => s, { - memoize: defaultMemoize + memoize: lruMemoize }) const selector15 = createSelector(selector14, s => s, { - memoize: defaultMemoize + memoize: lruMemoize }) const selector16 = createSelector(selector15, s => s, { - memoize: defaultMemoize + memoize: lruMemoize }) const selector17 = createSelector(selector16, s => s, { - memoize: defaultMemoize + memoize: lruMemoize }) const selector18 = createSelector(selector17, s => s, { - memoize: defaultMemoize + memoize: lruMemoize }) const selector19 = createSelector(selector18, s => s, { - memoize: defaultMemoize + memoize: lruMemoize }) const selector20 = createSelector(selector19, s => s, { - memoize: defaultMemoize + memoize: lruMemoize }) const selector21 = createSelector(selector20, s => s, { - memoize: defaultMemoize + memoize: lruMemoize }) const selector22 = createSelector(selector21, s => s, { - memoize: defaultMemoize + memoize: lruMemoize }) const selector23 = createSelector(selector22, s => s, { - memoize: defaultMemoize + memoize: lruMemoize }) const selector24 = createSelector(selector23, s => s, { - memoize: defaultMemoize + memoize: lruMemoize }) const selector25 = createSelector(selector24, s => s, { - memoize: defaultMemoize + memoize: lruMemoize }) const selector26 = createSelector(selector25, s => s, { - memoize: defaultMemoize + memoize: lruMemoize }) const selector27 = createSelector(selector26, s => s, { - memoize: defaultMemoize + memoize: lruMemoize }) const selector28 = createSelector(selector27, s => s, { - memoize: defaultMemoize + memoize: lruMemoize }) const selector29 = createSelector(selector28, s => s, { - memoize: defaultMemoize + memoize: lruMemoize }) } diff --git a/typescript_test/test.ts b/typescript_test/test.ts index 90423639c..d6c779fab 100644 --- a/typescript_test/test.ts +++ b/typescript_test/test.ts @@ -18,7 +18,7 @@ import { createSelectorCreator, createStructuredSelector, defaultEqualityCheck, - defaultMemoize + lruMemoize } from 'reselect' import { expectExactType } from './typesTestUtils' @@ -55,7 +55,7 @@ export const testExportBasic = createSelector( // Test for exporting declaration of created selector creator export const testExportStructured = createSelectorCreator( - defaultMemoize, + lruMemoize, (a, b) => typeof a === typeof b ) @@ -675,13 +675,13 @@ function testOptionalArgumentsConflicting() { function testDefaultMemoize() { const func = (a: string) => +a - const memoized = defaultMemoize(func) + const memoized = lruMemoize(func) const ret0: number = memoized('42') // @ts-expect-error const ret1: string = memoized('42') - const memoized2 = defaultMemoize( + const memoized2 = lruMemoize( (str: string, arr: string[]): { str: string; arr: string[] } => ({ str, arr @@ -697,7 +697,7 @@ function testDefaultMemoize() { } function testCreateSelectorCreator() { - const defaultCreateSelector = createSelectorCreator(defaultMemoize) + const defaultCreateSelector = createSelectorCreator(lruMemoize) const selector = defaultCreateSelector( (state: { foo: string }) => state.foo, @@ -708,7 +708,7 @@ function testCreateSelectorCreator() { // @ts-expect-error selector({ foo: 'fizz' }, { bar: 42 }) - // clearCache should exist because of defaultMemoize + // clearCache should exist because of lruMemoize selector.clearCache() const parametric = defaultCreateSelector( @@ -725,9 +725,9 @@ function testCreateSelectorCreator() { const bar: number = ret.bar // @ts-expect-error - createSelectorCreator(defaultMemoize, 1) + createSelectorCreator(lruMemoize, 1) - createSelectorCreator(defaultMemoize, (a: T, b: T) => { + createSelectorCreator(lruMemoize, (a: T, b: T) => { return `${a}` === `${b}` }) } @@ -947,7 +947,7 @@ function multiArgMemoize any>( return () => {} } -// #384: check for defaultMemoize +// #384: check for lruMemoize { interface Transaction { @@ -961,7 +961,7 @@ function multiArgMemoize any>( isEqual(transactionsIds(ts1), transactionsIds(ts2)) const createTransactionsSelector = createSelectorCreator( - defaultMemoize, + lruMemoize, collectionsEqual ) @@ -986,7 +986,7 @@ function multiArgMemoize any>( defaultEqualityCheck ) - const groupTransactionsByLabel = defaultMemoize( + const groupTransactionsByLabel = lruMemoize( (transactions: Transaction[]) => groupBy(transactions, item => item.transactionId), collectionsEqual @@ -1157,7 +1157,7 @@ function createSelectorConfigOptions() { (state: StateAB) => state.b, (a, b) => a + b, { - memoize: defaultMemoize, + memoize: lruMemoize, memoizeOptions: (a, b) => a === b } ) @@ -1167,7 +1167,7 @@ function createSelectorConfigOptions() { (state: StateAB) => state.b, (a, b) => a + b, { - memoize: defaultMemoize, + memoize: lruMemoize, memoizeOptions: { equalityCheck: (a, b) => a === b } @@ -1179,7 +1179,7 @@ function createSelectorConfigOptions() { (state: StateAB) => state.b, (a, b) => a + b, { - memoize: defaultMemoize, + memoize: lruMemoize, memoizeOptions: [(a, b) => a === b] } ) @@ -1342,7 +1342,7 @@ function testInputSelectorWithUndefinedReturn() { ({ field }: Input) => field, args => 'test', { - memoize: defaultMemoize, + memoize: lruMemoize, memoizeOptions: { maxSize: 42 } } ) @@ -1350,7 +1350,7 @@ function testInputSelectorWithUndefinedReturn() { // Make sure inference of functions works... const selector3: SelectorType = createSelector(input, result) const selector4: SelectorType = createSelector(input, result, { - memoize: defaultMemoize, + memoize: lruMemoize, memoizeOptions: { maxSize: 42 } }) } From eada4228a2bf0d31cd08af06f055012fe0a9261b Mon Sep 17 00:00:00 2001 From: Arya Emami Date: Fri, 1 Dec 2023 13:59:21 -0600 Subject: [PATCH 02/13] Rename `DefaultMemoizeOptions` to `LruMemoizeOptions` --- src/defaultMemoize.ts | 8 ++++---- src/index.ts | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/defaultMemoize.ts b/src/defaultMemoize.ts index ae8ead97f..c2c7d2d89 100644 --- a/src/defaultMemoize.ts +++ b/src/defaultMemoize.ts @@ -126,7 +126,7 @@ export function createCacheKeyComparator(equalityCheck: EqualityFn) { /** * @public */ -export interface DefaultMemoizeOptions { +export interface LruMemoizeOptions { /** * Used to compare the individual arguments of the provided calculation function. * @@ -142,7 +142,7 @@ export interface DefaultMemoizeOptions { * use case, where an update to another field in the original data causes a recalculation * due to changed references, but the output is still effectively the same. */ - resultEqualityCheck?: EqualityFn + resultEqualityCheck?: EqualityFn /** * The cache size for the selector. If greater than 1, the selector will use an LRU cache internally. * @@ -161,13 +161,13 @@ export interface DefaultMemoizeOptions { * * @template Func - The type of the function that is memoized. * - * @see {@link https://github.com/reduxjs/reselect#defaultmemoizefunc-equalitycheckoroptions--defaultequalitycheck defaultMemoize} + * @see {@link https://github.com/reduxjs/reselect#lrumemoizefunc-equalitycheckoroptions--defaultequalitycheck lruMemoize} * * @public */ export function lruMemoize( func: Func, - equalityCheckOrOptions?: EqualityFn | DefaultMemoizeOptions> + equalityCheckOrOptions?: EqualityFn | LruMemoizeOptions> ) { const providedOptions = typeof equalityCheckOrOptions === 'object' diff --git a/src/index.ts b/src/index.ts index d83e632b1..267a6aca9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,7 +7,7 @@ export type { TypedStructuredSelectorCreator } from './createStructuredSelector' export { defaultEqualityCheck, lruMemoize } from './defaultMemoize' -export type { DefaultMemoizeOptions } from './defaultMemoize' +export type { LruMemoizeOptions } from './defaultMemoize' export { setGlobalDevModeChecks } from './devModeChecks/setGlobalDevModeChecks' export type { Combiner, From bf8851378e03ccad9e4e235d24ceb89048b0e367 Mon Sep 17 00:00:00 2001 From: Arya Emami Date: Fri, 1 Dec 2023 14:24:59 -0600 Subject: [PATCH 03/13] Update JSDocs for `lruMemoize` --- src/defaultMemoize.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/defaultMemoize.ts b/src/defaultMemoize.ts index c2c7d2d89..15c649a32 100644 --- a/src/defaultMemoize.ts +++ b/src/defaultMemoize.ts @@ -94,7 +94,7 @@ function createLruCache(maxSize: number, equals: EqualityFn): Cache { /** * Runs a simple reference equality check. - * What {@linkcode lruMemoize defaultMemoize} uses by default. + * What {@linkcode lruMemoize lruMemoize} uses by default. * * @public */ @@ -151,10 +151,15 @@ export interface LruMemoizeOptions { maxSize?: number } -// defaultMemoize now supports a configurable cache size with LRU behavior, -// and optional comparison of the result value with existing values /** - * The standard memoize function used by `createSelector`. + * Creates a memoized version of a function with an optional + * LRU (Least Recently Used) cache. The memoized function uses a cache to + * store computed values. Depending on the `maxSize` option, it will use + * either a singleton cache (for a single entry) or an + * LRU cache (for multiple entries). + * + * **Note**: This function was previously known as `defaultMemoize`. + * * @param func - The function to be memoized. * @param equalityCheckOrOptions - Either an equality check function or an options object. * @returns A memoized function with a `.clearCache()` method attached. From 1fe1914d64058915af224e4ce4aa5cceab40d380 Mon Sep 17 00:00:00 2001 From: Arya Emami Date: Fri, 1 Dec 2023 14:35:16 -0600 Subject: [PATCH 04/13] Rename `defaultEqualityCheck` to `referenceEqualityCheck` --- src/autotrackMemoize/autotrackMemoize.ts | 4 ++-- src/defaultMemoize.ts | 4 ++-- src/index.ts | 2 +- typescript_test/test.ts | 8 ++++---- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/autotrackMemoize/autotrackMemoize.ts b/src/autotrackMemoize/autotrackMemoize.ts index 05a693a7b..82605e35b 100644 --- a/src/autotrackMemoize/autotrackMemoize.ts +++ b/src/autotrackMemoize/autotrackMemoize.ts @@ -3,7 +3,7 @@ import type { Node } from './tracking' import { createCacheKeyComparator, - defaultEqualityCheck + referenceEqualityCheck } from '../defaultMemoize' import type { AnyFunction, DefaultMemoizeFields, Simplify } from '../types' import { createCache } from './autotracking' @@ -80,7 +80,7 @@ export function autotrackMemoize(func: Func) { let lastArgs: IArguments | null = null - const shallowEqual = createCacheKeyComparator(defaultEqualityCheck) + const shallowEqual = createCacheKeyComparator(referenceEqualityCheck) const cache = createCache(() => { const res = func.apply(null, node.proxy as unknown as any[]) diff --git a/src/defaultMemoize.ts b/src/defaultMemoize.ts index 15c649a32..6680714e5 100644 --- a/src/defaultMemoize.ts +++ b/src/defaultMemoize.ts @@ -98,7 +98,7 @@ function createLruCache(maxSize: number, equals: EqualityFn): Cache { * * @public */ -export const defaultEqualityCheck: EqualityFn = (a, b): boolean => { +export const referenceEqualityCheck: EqualityFn = (a, b): boolean => { return a === b } @@ -180,7 +180,7 @@ export function lruMemoize( : { equalityCheck: equalityCheckOrOptions } const { - equalityCheck = defaultEqualityCheck, + equalityCheck = referenceEqualityCheck, maxSize = 1, resultEqualityCheck } = providedOptions diff --git a/src/index.ts b/src/index.ts index 267a6aca9..7aab57d0f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,7 +6,7 @@ export type { StructuredSelectorCreator, TypedStructuredSelectorCreator } from './createStructuredSelector' -export { defaultEqualityCheck, lruMemoize } from './defaultMemoize' +export { lruMemoize, referenceEqualityCheck } from './defaultMemoize' export type { LruMemoizeOptions } from './defaultMemoize' export { setGlobalDevModeChecks } from './devModeChecks/setGlobalDevModeChecks' export type { diff --git a/typescript_test/test.ts b/typescript_test/test.ts index d6c779fab..d42533d64 100644 --- a/typescript_test/test.ts +++ b/typescript_test/test.ts @@ -17,7 +17,7 @@ import { createSelector, createSelectorCreator, createStructuredSelector, - defaultEqualityCheck, + referenceEqualityCheck, lruMemoize } from 'reselect' import { expectExactType } from './typesTestUtils' @@ -941,7 +941,7 @@ function multiArgMemoize any>( func: F, a: number, b: string, - equalityCheck = defaultEqualityCheck + equalityCheck = referenceEqualityCheck ): F { // @ts-ignore return () => {} @@ -969,7 +969,7 @@ function multiArgMemoize any>( multiArgMemoize, 42, 'abcd', - defaultEqualityCheck + referenceEqualityCheck ) const select = createMultiMemoizeArgSelector( @@ -983,7 +983,7 @@ function multiArgMemoize any>( multiArgMemoize, 42, // @ts-expect-error - defaultEqualityCheck + referenceEqualityCheck ) const groupTransactionsByLabel = lruMemoize( From 2c39c8fbcfab71f55abf099dd32632ffb84204a1 Mon Sep 17 00:00:00 2001 From: Arya Emami Date: Fri, 1 Dec 2023 15:07:23 -0600 Subject: [PATCH 05/13] Update JSDocs for `LruMemoizeOptions` --- src/defaultMemoize.ts | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/src/defaultMemoize.ts b/src/defaultMemoize.ts index 6680714e5..bf5d370b8 100644 --- a/src/defaultMemoize.ts +++ b/src/defaultMemoize.ts @@ -124,27 +124,42 @@ export function createCacheKeyComparator(equalityCheck: EqualityFn) { } /** + * Options for configuring the behavior of a function memoized with + * LRU (Least Recently Used) caching. + * + * @template Result - The type of the return value of the memoized function. + * * @public */ export interface LruMemoizeOptions { /** - * Used to compare the individual arguments of the provided calculation function. + * Function used to compare the individual arguments of the + * provided calculation function. * - * @default defaultEqualityCheck + * @default referenceEqualityCheck */ equalityCheck?: EqualityFn + /** - * If provided, used to compare a newly generated output value against previous values in the cache. - * If a match is found, the old value is returned. This addresses the common + * If provided, used to compare a newly generated output value against + * previous values in the cache. If a match is found, + * the old value is returned. This addresses the common * ```ts * todos.map(todo => todo.id) * ``` - * use case, where an update to another field in the original data causes a recalculation - * due to changed references, but the output is still effectively the same. + * use case, where an update to another field in the original data causes + * a recalculation due to changed references, but the output is still + * effectively the same. + * + * @since 4.1.0 */ resultEqualityCheck?: EqualityFn + /** - * The cache size for the selector. If greater than 1, the selector will use an LRU cache internally. + * The maximum size of the cache used by the selector. + * A size greater than 1 means the selector will use an + * LRU (Least Recently Used) cache, allowing for the caching of multiple + * results based on different sets of arguments. * * @default 1 */ From df615f3b4ed5d40faa3208572b231372c73b9acb Mon Sep 17 00:00:00 2001 From: Arya Emami Date: Fri, 1 Dec 2023 15:07:57 -0600 Subject: [PATCH 06/13] Update JSDocs for `referenceEqualityCheck` --- src/defaultMemoize.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/defaultMemoize.ts b/src/defaultMemoize.ts index bf5d370b8..00c4416b3 100644 --- a/src/defaultMemoize.ts +++ b/src/defaultMemoize.ts @@ -96,11 +96,11 @@ function createLruCache(maxSize: number, equals: EqualityFn): Cache { * Runs a simple reference equality check. * What {@linkcode lruMemoize lruMemoize} uses by default. * + * **Note**: This function was previously known as `defaultEqualityCheck`. + * * @public */ -export const referenceEqualityCheck: EqualityFn = (a, b): boolean => { - return a === b -} +export const referenceEqualityCheck: EqualityFn = (a, b) => a === b export function createCacheKeyComparator(equalityCheck: EqualityFn) { return function areArgumentsShallowlyEqual( From fe618e8cc98006d7b1e72f02e8cf55842146d014 Mon Sep 17 00:00:00 2001 From: Arya Emami Date: Fri, 1 Dec 2023 15:17:48 -0600 Subject: [PATCH 07/13] Change `defaultMemoize` to `lruMemoize` in different files --- README.md | 68 ++++++++-------- src/autotrackMemoize/autotrackMemoize.ts | 6 +- src/createSelectorCreator.ts | 18 ++--- src/types.ts | 18 ++--- src/weakMapMemoize.ts | 2 +- test/benchmarks/weakMapMemoize.bench.ts | 2 +- test/defaultMemoize.spec.ts | 6 +- test/reselect.spec.ts | 2 +- test/testUtils.ts | 3 +- type-tests/argsMemoize.test-d.ts | 90 +++++++++++----------- type-tests/createSelectorCreator.test-d.ts | 6 +- type-tests/deepNesting.test-d.ts | 60 +++++++-------- 12 files changed, 141 insertions(+), 140 deletions(-) diff --git a/README.md b/README.md index 25c1bf560..45bbb60ef 100644 --- a/README.md +++ b/README.md @@ -113,7 +113,7 @@ In addition to skipping unnecessary recalculations, `memoizedSelectCompletedTodo - [**`createSelector`**][`createSelector`] - [**`createSelectorCreator`**][`createSelectorCreator`] - [**`createStructuredSelector`**][`createStructuredSelector`] - - [**`defaultMemoize`**][`defaultMemoize`] + - [**`lruMemoize`**][`lruMemoize`] - [**`weakMapMemoize`**][`weakMapMemoize`] - [**`unstable_autotrackMemoize`**][`unstable_autotrackMemoize`] - [Debugging Tools](#debuggingtools) @@ -348,18 +348,18 @@ Accepts either a `memoize` function and `...memoizeOptions` rest parameter, or s | Name | Description | | :----------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | -| `options` | An options object containing the `memoize` function responsible for memoizing the `resultFunc` inside [`createSelector`] (e.g., `defaultMemoize` or `weakMapMemoize`). It also provides additional options for customizing memoization. While the `memoize` property is mandatory, the rest are optional. | -| `options.argsMemoize?` | The optional memoize function that is used to memoize the arguments passed into the [output selector] generated by [`createSelector`] (e.g., `defaultMemoize` or `weakMapMemoize`).
**`Default`** `defaultMemoize` | +| `options` | An options object containing the `memoize` function responsible for memoizing the `resultFunc` inside [`createSelector`] (e.g., `lruMemoize` or `weakMapMemoize`). It also provides additional options for customizing memoization. While the `memoize` property is mandatory, the rest are optional. | +| `options.argsMemoize?` | The optional memoize function that is used to memoize the arguments passed into the [output selector] generated by [`createSelector`] (e.g., `lruMemoize` or `weakMapMemoize`).
**`Default`** `lruMemoize` | | `options.argsMemoizeOptions?` | Optional configuration options for the `argsMemoize` function. These options are passed to the `argsMemoize` function as the second argument.
since 5.0.0 | | `options.devModeChecks?` | Overrides the settings for the global development mode checks for the selector.
since 5.0.0 | -| `options.memoize` | The memoize function that is used to memoize the `resultFunc` inside [`createSelector`] (e.g., `defaultMemoize` or `weakMapMemoize`). since 5.0.0 | +| `options.memoize` | The memoize function that is used to memoize the `resultFunc` inside [`createSelector`] (e.g., `lruMemoize` or `weakMapMemoize`). since 5.0.0 | | `options.memoizeOptions?` | Optional configuration options for the `memoize` function. These options are passed to the `memoize` function as the second argument.
since 5.0.0 | Parameters | Name | Description | | :-------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------- | -| `memoize` | The `memoize` function responsible for memoizing the `resultFunc` inside [`createSelector`] (e.g., `defaultMemoize` or `weakMapMemoize`). | +| `memoize` | The `memoize` function responsible for memoizing the `resultFunc` inside [`createSelector`] (e.g., `lruMemoize` or `weakMapMemoize`). | | `...memoizeOptionsFromArgs` | Optional configuration options for the memoization function. These options are then passed to the memoize function as the second argument onwards. | Returns @@ -370,8 +370,8 @@ 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, `weakMapMemoize` will be used. | +| `MemoizeFunction` | The type of the memoize function that is used to memoize the `resultFunc` inside [`createSelector`] (e.g., `lruMemoize` 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., `lruMemoize` or `weakMapMemoize`). If none is explicitly provided, `weakMapMemoize` will be used. | @@ -431,14 +431,14 @@ customMemoize(resultFunc, option1, option2, option3) ##### Additional Examples -###### Customize `equalityCheck` for `defaultMemoize` +###### Customize `equalityCheck` for `lruMemoize` ```js -import { createSelectorCreator, defaultMemoize } from 'reselect' +import { createSelectorCreator, lruMemoize } from 'reselect' import isEqual from 'lodash.isequal' // create a "selector creator" that uses lodash.isequal instead of === -const createDeepEqualSelector = createSelectorCreator(defaultMemoize, isEqual) +const createDeepEqualSelector = createSelectorCreator(lruMemoize, isEqual) // use the new "selector creator" to create a selector const selectSum = createDeepEqualSelector( @@ -597,9 +597,9 @@ const result = structuredSelector({ a: 1, b: 2 }) // will produce { x: 1, y: 2 } Reselect comes with a selection of memoization functions, each uniquely designed to address different scenarios and performance requirements. By effectively leveraging these functions, you can significantly enhance the efficiency and responsiveness of your applications. - + -#### defaultMemoize(func, equalityCheckOrOptions = defaultEqualityCheck) +#### lruMemoize(func, equalityCheckOrOptions = referenceEqualityCheck) Description @@ -607,10 +607,10 @@ The standard memoize function used by [`createSelector`]. It has a default cache size of 1. This means it always recalculates when the value of an argument changes. However, this can be customized as needed with a specific max cache size (since 4.1.0). -It determines if an argument has changed by calling the `equalityCheck` function. As `defaultMemoize` is designed to be used with immutable data, the default `equalityCheck` function checks for changes using [reference equality][Reference Equality Check]: +It determines if an argument has changed by calling the `equalityCheck` function. As `lruMemoize` is designed to be used with immutable data, the default `equalityCheck` function checks for changes using [reference equality][Reference Equality Check]: ```ts -const defaultEqualityCheck = (previousValue: any, currentValue: any) => { +const referenceEqualityCheck = (previousValue: any, currentValue: any) => { return previousValue === currentValue } ``` @@ -622,7 +622,7 @@ const defaultEqualityCheck = (previousValue: any, currentValue: any) => { | `func` | The function to be memoized. | | `equalityCheckOrOptions` | Either an `equality check` function or an `options` object. | -Since 4.1.0, `defaultMemoize` also accepts an options object as its first argument instead of an `equalityCheck` function. The `options` object may contain: +Since 4.1.0, `lruMemoize` also accepts an options object as its first argument instead of an `equalityCheck` function. The `options` object may contain: ```ts type EqualityFn = (a: any, b: any) => boolean @@ -636,7 +636,7 @@ interface DefaultMemoizeOptions { | Name | Description | | :-------------------- | :---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| `equalityCheck` | Used to compare the individual arguments of the provided calculation function.
**`Default`** = `defaultEqualityCheck` | +| `equalityCheck` | Used to compare the individual arguments of the provided calculation function.
**`Default`** = `referenceEqualityCheck` | | `resultEqualityCheck` | If provided, used to compare a newly generated output value against previous values in the cache. If a match is found, the old value is returned. This addresses the common todos.map(todo => todo.id) use case, where an update to another field in the original data causes a recalculation due to changed references, but the output is still effectively the same. | | `maxSize` | The cache size for the selector. If greater than 1, the selector will use an LRU cache internally.
**`Default`** = 1 | @@ -657,7 +657,7 @@ A memoized function with a `.clearCache()` method attached.
Examples -###### Using `defaultMemoize` with [`createSelector`] +###### Using `lruMemoize` with [`createSelector`] ```ts import { shallowEqual } from 'react-redux' @@ -681,20 +681,20 @@ const selectTodoIds = createSelector( ) ``` -###### Using `defaultMemoize` with [`createSelectorCreator`] +###### Using `lruMemoize` with [`createSelectorCreator`] ```ts import { shallowEqual } from 'react-redux' -import { createSelectorCreator, defaultMemoize } from 'reselect' +import { createSelectorCreator, lruMemoize } from 'reselect' const createSelectorShallowEqual = createSelectorCreator({ - memoize: defaultMemoize, + memoize: lruMemoize, memoizeOptions: { equalityCheck: shallowEqual, resultEqualityCheck: shallowEqual, maxSize: 10 }, - argsMemoize: defaultMemoize, + argsMemoize: lruMemoize, argsMemoizeOptions: { equalityCheck: shallowEqual, resultEqualityCheck: shallowEqual, @@ -720,7 +720,7 @@ const selectTodoIds = createSelectorShallowEqual( Description -[`defaultMemoize`] has to be explicitly configured to have a cache size larger than 1, and uses an LRU cache internally. +[`lruMemoize`] has to be explicitly configured to have a cache size larger than 1, and uses an LRU cache internally. `weakMapMemoize` creates a tree of [`WeakMap`]-based cache nodes based on the identity of the arguments it's been called with (in this case, the extracted values from your input selectors). **This allows `weakMapMemoize` to have an effectively infinite cache size**. Cache results will be kept in memory as long as references to the arguments still exist, and then cleared out as the arguments are garbage-collected. @@ -770,7 +770,7 @@ selectItemsByCategory(state, 'Electronics') // Selector runs again! Before you could solve this in a number of different ways: -1. Set the `maxSize` with [`defaultMemoize`]: +1. Set the `maxSize` with [`lruMemoize`]: ```ts const selectItemsByCategory = createSelector( @@ -965,7 +965,7 @@ selectItemsByCategory(state, 'Electronics') Description -Uses an "auto-tracking" approach inspired by the work of the Ember Glimmer team. It uses a Proxy to wrap arguments and track accesses to nested fields in your selector on first read. Later, when the selector is called with new arguments, it identifies which accessed fields have changed and only recalculates the result if one or more of those accessed fields have changed. This allows it to be more precise than the shallow equality checks in `defaultMemoize`. +Uses an "auto-tracking" approach inspired by the work of the Ember Glimmer team. It uses a Proxy to wrap arguments and track accesses to nested fields in your selector on first read. Later, when the selector is called with new arguments, it identifies which accessed fields have changed and only recalculates the result if one or more of those accessed fields have changed. This allows it to be more precise than the shallow equality checks in `lruMemoize`. > [!WARNING] > This API is still experimental and undergoing testing. @@ -974,12 +974,12 @@ Uses an "auto-tracking" approach inspired by the work of the Ember Glimmer team. - Pros: - - It is likely to avoid excess calculations and recalculate fewer times than `defaultMemoize` will, which may also result in fewer component re-renders. + - It is likely to avoid excess calculations and recalculate fewer times than `lruMemoize` will, which may also result in fewer component re-renders. - Cons: - It only has a cache size of 1. - - It is slower than `defaultMemoize`, because it has to do more work. (How much slower is dependent on the number of accessed fields in a selector, number of calls, frequency of input changes, etc) + - It is slower than `lruMemoize`, because it has to do more work. (How much slower is dependent on the number of accessed fields in a selector, number of calls, frequency of input changes, etc) - It can have some unexpected behavior. Because it tracks nested field accesses, cases where you don't access a field will not recalculate properly. For example, a badly-written selector like: ```ts @@ -1533,7 +1533,7 @@ const selectItemById = createSelector( ### Can the memoization behavior be customized? -Yes. The built-in `defaultMemoize` memoizer works great for a lot of use cases, but it can be customized or swapped out for a different memoizer. See [these examples](#customize-equalitycheck-for-defaultmemoize). +Yes. The built-in `lruMemoize` memoizer works great for a lot of use cases, but it can be customized or swapped out for a different memoizer. See [these examples](#customize-equalitycheck-for-lrumemoize). ### How do I test a selector? @@ -1607,7 +1607,7 @@ test('selector unit test', () => { Yes, although if they pass in different arguments, you will need to handle that in order for memoization to work consistently: -- Pass a larger `maxSize` if using `defaultMemoize` ( as of 4.1.0+) +- Pass a larger `maxSize` if using `lruMemoize` ( as of 4.1.0+) - Use [`weakMapMemoize`](#weakmapmemoize) (as of 5.0.0+) ### Are there TypeScript Typings? @@ -1666,14 +1666,14 @@ const selectTodoByIdCurried = currySelector( Or for reusability you can do this: ```ts -import type { defaultMemoize, SelectorArray, UnknownMemoizer } from 'reselect' +import type { lruMemoize, SelectorArray, UnknownMemoizer } from 'reselect' import { createSelector } from 'reselect' export const createCurriedSelector = < InputSelectors extends SelectorArray, Result, - OverrideMemoizeFunction extends UnknownMemoizer = typeof defaultMemoize, - OverrideArgsMemoizeFunction extends UnknownMemoizer = typeof defaultMemoize + OverrideMemoizeFunction extends UnknownMemoizer = typeof lruMemoize, + OverrideArgsMemoizeFunction extends UnknownMemoizer = typeof lruMemoize >( ...args: Parameters< typeof createSelector< @@ -1777,8 +1777,8 @@ interface RootState { export type TypedCreateSelector< State, - MemoizeFunction extends UnknownMemoizer = typeof defaultMemoize, - ArgsMemoizeFunction extends UnknownMemoizer = typeof defaultMemoize + MemoizeFunction extends UnknownMemoizer = typeof lruMemoize, + ArgsMemoizeFunction extends UnknownMemoizer = typeof lruMemoize > = < InputSelectors extends readonly Selector[], Result, @@ -1911,7 +1911,7 @@ Originally inspired by getters in [NuclearJS](https://github.com/optimizely/nucl [output selector fields]: #output-selector-fields 'Output Selector Fields' [`createSelector`]: #createselectorinputselectors--inputselectors-resultfunc-createselectoroptions 'createSelector' [`createSelectorCreator`]: #createselectorcreatormemoize--options-memoizeoptions 'createSelectorCreator' -[`defaultMemoize`]: #defaultmemoizefunc-equalitycheckoroptions--defaultequalitycheck 'defaultMemoize' +[`lruMemoize`]: #lrumemoizefunc-equalitycheckoroptions--defaultequalitycheck 'lruMemoize' [`weakMapMemoize`]: #weakmapmemoizefunc---since-500 'weakMapMemoize' [`unstable_autotrackMemoize`]: #unstable_autotrackmemoizefunc---since-500 'unstable_autotrackMemoize' [`createStructuredSelector`]: #createstructuredselector-inputSelectorsObject--selectorcreator--createselector 'createStructuredSelector' diff --git a/src/autotrackMemoize/autotrackMemoize.ts b/src/autotrackMemoize/autotrackMemoize.ts index 82605e35b..1672cc571 100644 --- a/src/autotrackMemoize/autotrackMemoize.ts +++ b/src/autotrackMemoize/autotrackMemoize.ts @@ -14,15 +14,15 @@ import { createCache } from './autotracking' * in your selector on first read. Later, when the selector is called with * new arguments, it identifies which accessed fields have changed and * only recalculates the result if one or more of those accessed fields have changed. - * This allows it to be more precise than the shallow equality checks in `defaultMemoize`. + * This allows it to be more precise than the shallow equality checks in `lruMemoize`. * * __Design Tradeoffs for `autotrackMemoize`:__ * - Pros: - * - It is likely to avoid excess calculations and recalculate fewer times than `defaultMemoize` will, + * - It is likely to avoid excess calculations and recalculate fewer times than `lruMemoize` will, * which may also result in fewer component re-renders. * - Cons: * - It only has a cache size of 1. - * - It is slower than `defaultMemoize`, because it has to do more work. (How much slower is dependent on the number of accessed fields in a selector, number of calls, frequency of input changes, etc) + * - It is slower than `lruMemoize`, because it has to do more work. (How much slower is dependent on the number of accessed fields in a selector, number of calls, frequency of input changes, etc) * - It can have some unexpected behavior. Because it tracks nested field accesses, * cases where you don't access a field will not recalculate properly. * For example, a badly-written selector like: diff --git a/src/createSelectorCreator.ts b/src/createSelectorCreator.ts index 92553655a..671393590 100644 --- a/src/createSelectorCreator.ts +++ b/src/createSelectorCreator.ts @@ -27,8 +27,8 @@ 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, `weakMapMemoize` will be used. + * @template MemoizeFunction - The type of the memoize function that is used to memoize the `resultFunc` inside `createSelector` (e.g., `lruMemoize` 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., `lruMemoize` or `weakMapMemoize`). If none is explicitly provided, `weakMapMemoize` will be used. * * @public */ @@ -144,7 +144,7 @@ export interface CreateSelectorFunction< /** * Creates a selector creator function with the specified memoization function and options for customizing memoization behavior. * - * @param options - An options object containing the `memoize` function responsible for memoizing the `resultFunc` inside `createSelector` (e.g., `defaultMemoize` or `weakMapMemoize`). It also provides additional options for customizing memoization. While the `memoize` property is mandatory, the rest are optional. + * @param options - An options object containing the `memoize` function responsible for memoizing the `resultFunc` inside `createSelector` (e.g., `lruMemoize` or `weakMapMemoize`). It also provides additional options for customizing memoization. While the `memoize` property is mandatory, the rest are optional. * @returns A customized `createSelector` function. * * @example @@ -166,8 +166,8 @@ export interface CreateSelectorFunction< * ) * ``` * - * @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, `weakMapMemoize` will be used. + * @template MemoizeFunction - The type of the memoize function that is used to memoize the `resultFunc` inside `createSelector` (e.g., `lruMemoize` 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., `lruMemoize` or `weakMapMemoize`). If none is explicitly provided, `weakMapMemoize` will be used. * * @see {@link https://github.com/reduxjs/reselect#createselectorcreatormemoize--options-memoizeoptions createSelectorCreator} * @@ -194,7 +194,7 @@ export function createSelectorCreator< /** * Creates a selector creator function with the specified memoization function and options for customizing memoization behavior. * - * @param memoize - The `memoize` function responsible for memoizing the `resultFunc` inside `createSelector` (e.g., `defaultMemoize` or `weakMapMemoize`). + * @param memoize - The `memoize` function responsible for memoizing the `resultFunc` inside `createSelector` (e.g., `lruMemoize` or `weakMapMemoize`). * @param memoizeOptionsFromArgs - Optional configuration options for the memoization function. These options are then passed to the memoize function as the second argument onwards. * @returns A customized `createSelector` function. * @@ -212,7 +212,7 @@ export function createSelectorCreator< * ) * ``` * - * @template MemoizeFunction - The type of the memoize function that is used to memoize the `resultFunc` inside `createSelector` (e.g., `defaultMemoize` or `weakMapMemoize`). + * @template MemoizeFunction - The type of the memoize function that is used to memoize the `resultFunc` inside `createSelector` (e.g., `lruMemoize` or `weakMapMemoize`). * * @see {@link https://github.com/reduxjs/reselect#createselectorcreatormemoize--options-memoizeoptions createSelectorCreator} * @@ -230,8 +230,8 @@ export function createSelectorCreator( * @param memoizeOptionsFromArgs - Optional configuration options for the memoization function. These options are then passed to the memoize function as the second argument onwards. * @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, `weakMapMemoize` will be used. + * @template MemoizeFunction - The type of the memoize function that is used to memoize the `resultFunc` inside `createSelector` (e.g., `lruMemoize` 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., `lruMemoize` 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< diff --git a/src/types.ts b/src/types.ts index 6ead83c92..5eacacc83 100644 --- a/src/types.ts +++ b/src/types.ts @@ -54,8 +54,8 @@ export type SelectorResultArray = /** * 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, `weakMapMemoize` will be used. + * @template MemoizeFunction - The type of the memoize function that is used to memoize the `resultFunc` inside `createSelector` (e.g., `lruMemoize` 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., `lruMemoize` 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, `weakMapMemoize` will be used. * @@ -80,7 +80,7 @@ export interface CreateSelectorOptions< /** * The memoize function that is used to memoize the {@linkcode OutputSelectorFields.resultFunc resultFunc} - * inside `createSelector` (e.g., `defaultMemoize` or `weakMapMemoize`). + * inside `createSelector` (e.g., `lruMemoize` or `weakMapMemoize`). * * When passed directly into `createSelector`, it overrides the `memoize` function initially passed into `createSelectorCreator`. * @@ -105,7 +105,7 @@ export interface CreateSelectorOptions< /** * The optional memoize function that is used to memoize the arguments * passed into the output selector generated by `createSelector` - * (e.g., `defaultMemoize` or `weakMapMemoize`). + * (e.g., `lruMemoize` or `weakMapMemoize`). * * When passed directly into `createSelector`, it overrides the * `argsMemoize` function initially passed into `createSelectorCreator`. @@ -170,8 +170,8 @@ 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, `weakMapMemoize` will be used. + * @template MemoizeFunction - The type of the memoize function that is used to memoize the `resultFunc` inside `createSelector` (e.g., `lruMemoize` 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., `lruMemoize` or `weakMapMemoize`). If none is explicitly provided, `weakMapMemoize` will be used. * * @public */ @@ -244,8 +244,8 @@ 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, `weakMapMemoize` will be used. + * @template MemoizeFunction - The type of the memoize function that is used to memoize the `resultFunc` inside `createSelector` (e.g., `lruMemoize` 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., `lruMemoize` or `weakMapMemoize`). If none is explicitly provided, `weakMapMemoize` will be used. * * @public */ @@ -442,7 +442,7 @@ export type ExtractMemoizerFields = /** * Represents the additional properties attached to a function memoized by `reselect`. * - * `defaultMemoize`, `weakMapMemoize` and `autotrackMemoize` all return these properties. + * `lruMemoize`, `weakMapMemoize` and `autotrackMemoize` all return these properties. * * @see {@linkcode ExtractMemoizerFields ExtractMemoizerFields} * diff --git a/src/weakMapMemoize.ts b/src/weakMapMemoize.ts index 564fb9d56..39ab9c63e 100644 --- a/src/weakMapMemoize.ts +++ b/src/weakMapMemoize.ts @@ -99,7 +99,7 @@ export interface WeakMapMemoizeOptions { * - Cons: * - There's currently no way to alter the argument comparisons. * They're based on strict reference equality. - * - It's roughly the same speed as `defaultMemoize`, although likely a fraction slower. + * - It's roughly the same speed as `lruMemoize`, although likely a fraction slower. * * __Use Cases for `weakMapMemoize`:__ * - This memoizer is likely best used for cases where you need to call the diff --git a/test/benchmarks/weakMapMemoize.bench.ts b/test/benchmarks/weakMapMemoize.bench.ts index dd4d5e262..e8dd07fe1 100644 --- a/test/benchmarks/weakMapMemoize.bench.ts +++ b/test/benchmarks/weakMapMemoize.bench.ts @@ -208,7 +208,7 @@ describe('Parametric selectors: weakMapMemoize vs others', () => { ) }) -// describe('weakMapMemoize vs defaultMemoize with maxSize', () => { +// describe('weakMapMemoize vs lruMemoize with maxSize', () => { // const store = setupStore() // const state = store.getState() // const arrayOfNumbers = Array.from({ length: 30 }, (num, index) => index) diff --git a/test/defaultMemoize.spec.ts b/test/defaultMemoize.spec.ts index c603ea051..cf04e8cc1 100644 --- a/test/defaultMemoize.spec.ts +++ b/test/defaultMemoize.spec.ts @@ -8,7 +8,7 @@ const createSelector = createSelectorCreator({ argsMemoize: lruMemoize }) -describe('defaultMemoize', () => { +describe(lruMemoize, () => { test('Basic memoization', () => { let called = 0 const memoized = lruMemoize(state => { @@ -75,7 +75,7 @@ describe('defaultMemoize', () => { const anotherObject = { foo: 'bar' } const memoized = lruMemoize(a => a, shallowEqual) - // the first call to `memoized` doesn't hit because `defaultMemoize.lastArgs` is uninitialized + // the first call to `memoized` doesn't hit because `lruMemoize.lastArgs` is uninitialized // and so `equalityCheck` is never called memoized(someObject) // first call does not shallow compare @@ -91,7 +91,7 @@ describe('defaultMemoize', () => { This test was useful when we had a cache size of 1 previously, and always saved `lastArgs`. But, with the new implementation, this doesn't make sense any more. - // the third call does not fall through because `defaultMemoize` passes `anotherObject` as + // the third call does not fall through because `lruMemoize` passes `anotherObject` as // both the `newVal` and `oldVal` params. This allows `shallowEqual` to be much more performant // than if it had passed `someObject` as `oldVal`, even though `someObject` and `anotherObject` // are shallowly equal diff --git a/test/reselect.spec.ts b/test/reselect.spec.ts index 1f63cac86..36c8500b4 100644 --- a/test/reselect.spec.ts +++ b/test/reselect.spec.ts @@ -730,7 +730,7 @@ describe('argsMemoize and memoize', () => { ).toBe(2) selectorDefaultParametricArgsWeakMap(store.getState(), 2) // If we call the selector with 1, then 2, then 1 and back to 2 again, - // `defaultMemoize` will recompute a total of 4 times, + // `lruMemoize` will recompute a total of 4 times, // but weakMapMemoize will recompute only twice. expect(selectorDefaultParametricArgsWeakMap.recomputations()).toBe(2) expect( diff --git a/test/testUtils.ts b/test/testUtils.ts index 530484f44..1b8a2e6b8 100644 --- a/test/testUtils.ts +++ b/test/testUtils.ts @@ -1,6 +1,7 @@ import type { PayloadAction } from '@reduxjs/toolkit' import { combineReducers, configureStore, createSlice } from '@reduxjs/toolkit' import { test } from 'vitest' +import type { lruMemoize } from '../src/defaultMemoize' import type { AnyFunction, OutputSelector, Simplify } from '../src/types' export interface Todo { @@ -489,7 +490,7 @@ export const logRecomputations = (selector: S) => { } export const logSelectorRecomputations = < - S extends OutputSelector + S extends OutputSelector >( selector: S ) => { diff --git a/type-tests/argsMemoize.test-d.ts b/type-tests/argsMemoize.test-d.ts index b95bcbcf1..33e7678b4 100644 --- a/type-tests/argsMemoize.test-d.ts +++ b/type-tests/argsMemoize.test-d.ts @@ -4,7 +4,7 @@ import { unstable_autotrackMemoize as autotrackMemoize, createSelector, createSelectorCreator, - defaultMemoize, + lruMemoize, weakMapMemoize } from 'reselect' import { assertType, describe, expectTypeOf, test } from 'vitest' @@ -28,22 +28,22 @@ describe('memoize and argsMemoize', () => { const selectorDefaultSeparateInlineArgs = createSelector( (state: RootState) => state.todos, todos => todos.map(t => t.id), - { memoize: defaultMemoize } + { memoize: lruMemoize } ) const selectorDefaultArgsAsArray = createSelector( [(state: RootState) => state.todos], todos => todos.map(t => t.id), - { memoize: defaultMemoize } + { memoize: lruMemoize } ) const selectorDefaultArgsAsArrayWithMemoizeOptions = createSelector( [(state: RootState) => state.todos], todos => todos.map(t => t.id), - { memoize: defaultMemoize, memoizeOptions: { maxSize: 2 } } + { memoize: lruMemoize, memoizeOptions: { maxSize: 2 } } ) const selectorDefaultSeparateInlineArgsWithMemoizeOptions = createSelector( (state: RootState) => state.todos, todos => todos.map(t => t.id), - { memoize: defaultMemoize, memoizeOptions: { maxSize: 2 } } + { memoize: lruMemoize, memoizeOptions: { maxSize: 2 } } ) const selectorAutotrackSeparateInlineArgs = createSelector( (state: RootState) => state.todos, @@ -94,7 +94,7 @@ describe('memoize and argsMemoize', () => { todos => todos.map(t => t.id), { memoize: weakMapMemoize, memoizeOptions: { maxSize: 2 } } ) - const createSelectorDefault = createSelectorCreator(defaultMemoize) + const createSelectorDefault = createSelectorCreator(lruMemoize) const createSelectorWeakMap = createSelectorCreator(weakMapMemoize) const createSelectorAutotrack = createSelectorCreator(autotrackMemoize) const changeMemoizeMethodSelectorDefault = createSelectorDefault( @@ -105,15 +105,15 @@ describe('memoize and argsMemoize', () => { const changeMemoizeMethodSelectorWeakMap = createSelectorWeakMap( (state: RootState) => state.todos, todos => todos.map(t => t.id), - { memoize: defaultMemoize } + { memoize: lruMemoize } ) const changeMemoizeMethodSelectorAutotrack = createSelectorAutotrack( (state: RootState) => state.todos, todos => todos.map(t => t.id), - { memoize: defaultMemoize } + { memoize: lruMemoize } ) const changeMemoizeMethodSelectorDefaultWithMemoizeOptions = - // @ts-expect-error When memoize is changed to weakMapMemoize or autotrackMemoize, memoizeOptions cannot be the same type as options args in defaultMemoize. + // @ts-expect-error When memoize is changed to weakMapMemoize or autotrackMemoize, memoizeOptions cannot be the same type as options args in lruMemoize. createSelectorDefault( (state: RootState) => state.todos, // @ts-expect-error @@ -124,13 +124,13 @@ describe('memoize and argsMemoize', () => { createSelectorWeakMap( (state: RootState) => state.todos, todos => todos.map(t => t.id), - { memoize: defaultMemoize, memoizeOptions: { maxSize: 2 } } // When memoize is changed to defaultMemoize, memoizeOptions can now be the same type as options args in defaultMemoize. + { memoize: lruMemoize, memoizeOptions: { maxSize: 2 } } // When memoize is changed to lruMemoize, memoizeOptions can now be the same type as options args in lruMemoize. ) const changeMemoizeMethodSelectorAutotrackWithMemoizeOptions = createSelectorAutotrack( (state: RootState) => state.todos, todos => todos.map(t => t.id), - { memoize: defaultMemoize, memoizeOptions: { maxSize: 2 } } // When memoize is changed to defaultMemoize, memoizeOptions can now be the same type as options args in defaultMemoize. + { memoize: lruMemoize, memoizeOptions: { maxSize: 2 } } // When memoize is changed to lruMemoize, memoizeOptions can now be the same type as options args in lruMemoize. ) }) @@ -138,22 +138,22 @@ describe('memoize and argsMemoize', () => { const selectorDefaultSeparateInlineArgs = createSelector( (state: RootState) => state.todos, todos => todos.map(t => t.id), - { argsMemoize: defaultMemoize } + { argsMemoize: lruMemoize } ) const selectorDefaultArgsAsArray = createSelector( [(state: RootState) => state.todos], todos => todos.map(t => t.id), - { argsMemoize: defaultMemoize } + { argsMemoize: lruMemoize } ) const selectorDefaultArgsAsArrayWithMemoizeOptions = createSelector( [(state: RootState) => state.todos], todos => todos.map(t => t.id), - { argsMemoize: defaultMemoize, argsMemoizeOptions: { maxSize: 2 } } + { argsMemoize: lruMemoize, argsMemoizeOptions: { maxSize: 2 } } ) const selectorDefaultSeparateInlineArgsWithMemoizeOptions = createSelector( (state: RootState) => state.todos, todos => todos.map(t => t.id), - { argsMemoize: defaultMemoize, argsMemoizeOptions: { maxSize: 2 } } + { argsMemoize: lruMemoize, argsMemoizeOptions: { maxSize: 2 } } ) const selectorAutotrackSeparateInlineArgs = createSelector( (state: RootState) => state.todos, @@ -228,7 +228,7 @@ describe('memoize and argsMemoize', () => { // @ts-expect-error todos => todos.map(t => t.id), { - memoize: defaultMemoize, + memoize: lruMemoize, argsMemoize: weakMapMemoize, memoizeOptions: { equalityCheck: @@ -239,9 +239,9 @@ describe('memoize and argsMemoize', () => { argsMemoizeOptions: { maxSize: 2 } } ) - // const createSelectorDefaultMemoize = createSelectorCreator(defaultMemoize) + // const createSelectorDefaultMemoize = createSelectorCreator(lruMemoize) const createSelectorDefaultMemoize = createSelectorCreator({ - memoize: defaultMemoize + memoize: lruMemoize }) const selectorWeakMapSeparateInlineArgsWithMemoizeOptions3 = // @ts-expect-error When argsMemoize is weakMapMemoize, type of argsMemoizeOptions needs to be the same as options args in weakMapMemoize. @@ -250,7 +250,7 @@ describe('memoize and argsMemoize', () => { // @ts-expect-error todos => todos.map(t => t.id), { - memoize: defaultMemoize, + memoize: lruMemoize, argsMemoize: weakMapMemoize, // memoizeOptions: [], memoizeOptions: [ @@ -302,7 +302,7 @@ describe('memoize and argsMemoize', () => { // argsMemoizeOptions: (a, b) => a === b } ) - const createSelectorDefault = createSelectorCreator(defaultMemoize) + const createSelectorDefault = createSelectorCreator(lruMemoize) const createSelectorWeakMap = createSelectorCreator(weakMapMemoize) const createSelectorAutotrack = createSelectorCreator(autotrackMemoize) const changeMemoizeMethodSelectorDefault = createSelectorDefault( @@ -313,15 +313,15 @@ describe('memoize and argsMemoize', () => { const changeMemoizeMethodSelectorWeakMap = createSelectorWeakMap( (state: RootState) => state.todos, todos => todos.map(t => t.id), - { argsMemoize: defaultMemoize } + { argsMemoize: lruMemoize } ) const changeMemoizeMethodSelectorAutotrack = createSelectorAutotrack( (state: RootState) => state.todos, todos => todos.map(t => t.id), - { argsMemoize: defaultMemoize } + { argsMemoize: lruMemoize } ) const changeMemoizeMethodSelectorDefaultWithMemoizeOptions = - // @ts-expect-error When argsMemoize is changed to weakMapMemoize or autotrackMemoize, argsMemoizeOptions cannot be the same type as options args in defaultMemoize. + // @ts-expect-error When argsMemoize is changed to weakMapMemoize or autotrackMemoize, argsMemoizeOptions cannot be the same type as options args in lruMemoize. createSelectorDefault( (state: RootState) => state.todos, // @ts-expect-error @@ -332,13 +332,13 @@ describe('memoize and argsMemoize', () => { createSelectorWeakMap( (state: RootState) => state.todos, todos => todos.map(t => t.id), - { argsMemoize: defaultMemoize, argsMemoizeOptions: { maxSize: 2 } } // When argsMemoize is changed to defaultMemoize, argsMemoizeOptions can now be the same type as options args in defaultMemoize. + { argsMemoize: lruMemoize, argsMemoizeOptions: { maxSize: 2 } } // When argsMemoize is changed to lruMemoize, argsMemoizeOptions can now be the same type as options args in lruMemoize. ) const changeMemoizeMethodSelectorAutotrackWithMemoizeOptions = createSelectorAutotrack( (state: RootState) => state.todos, todos => todos.map(t => t.id), - { argsMemoize: defaultMemoize, argsMemoizeOptions: { maxSize: 2 } } // When argsMemoize is changed to defaultMemoize, argsMemoizeOptions can now be the same type as options args in defaultMemoize. + { argsMemoize: lruMemoize, argsMemoizeOptions: { maxSize: 2 } } // When argsMemoize is changed to lruMemoize, argsMemoizeOptions can now be the same type as options args in lruMemoize. ) }) @@ -397,16 +397,16 @@ describe('memoize and argsMemoize', () => { // Checking to see if types dynamically change if memoize or argsMemoize are overridden inside `createSelector`. // `microMemoize` was initially passed into `createSelectorCreator` - // as `memoize` and `argsMemoize`, After overriding them both to `defaultMemoize`, + // as `memoize` and `argsMemoize`, After overriding them both to `lruMemoize`, // not only does the type for `memoizeOptions` and `argsMemoizeOptions` change to - // the options parameter of `defaultMemoize`, the output selector fields - // also change their type to the return type of `defaultMemoize`. + // the options parameter of `lruMemoize`, the output selector fields + // also change their type to the return type of `lruMemoize`. const selectorMicroMemoizeOverridden = createSelectorMicroMemoize( (state: RootState) => state.todos, todos => todos.map(t => t.id), { - memoize: defaultMemoize, - argsMemoize: defaultMemoize, + memoize: lruMemoize, + argsMemoize: lruMemoize, memoizeOptions: { equalityCheck: (a, b) => a === b, maxSize: 2 }, argsMemoizeOptions: { equalityCheck: (a, b) => a === b, maxSize: 3 } } @@ -462,8 +462,8 @@ describe('memoize and argsMemoize', () => { [(state: RootState) => state.todos], todos => todos.map(({ id }) => id), { - memoize: defaultMemoize, - argsMemoize: defaultMemoize, + memoize: lruMemoize, + argsMemoize: lruMemoize, memoizeOptions: { equalityCheck: (a, b) => a === b, maxSize: 2 }, argsMemoizeOptions: { equalityCheck: (a, b) => a === b, maxSize: 3 } } @@ -522,7 +522,7 @@ describe('memoize and argsMemoize', () => { (state: RootState) => state.todos, todos => todos.map(({ id }) => id), { - argsMemoize: defaultMemoize, + argsMemoize: lruMemoize, memoizeOptions: { isPromise: false, resultEqualityCheck: @@ -537,7 +537,7 @@ describe('memoize and argsMemoize', () => { (state: RootState) => state.todos, todos => todos.map(({ id }) => id), { - argsMemoize: defaultMemoize, + argsMemoize: lruMemoize, memoizeOptions: { isPromise: false }, argsMemoizeOptions: { resultEqualityCheck: (a, b) => a === b } } @@ -597,7 +597,7 @@ describe('memoize and argsMemoize', () => { (state: RootState) => state.todos, todos => todos.map(t => t.id), { - memoize: defaultMemoize, + memoize: lruMemoize, memoizeOptions: { resultEqualityCheck: (a, b) => a === b } } ) @@ -653,32 +653,32 @@ describe('memoize and argsMemoize', () => { ) const selectorMicroMemoizePartiallyOverridden = - // @ts-expect-error Since `argsMemoize` is set to `defaultMemoize`, `argsMemoizeOptions` must match the options object parameter of `defaultMemoize` + // @ts-expect-error Since `argsMemoize` is set to `lruMemoize`, `argsMemoizeOptions` must match the options object parameter of `lruMemoize` createSelectorMicroMemoize( (state: RootState) => state.todos, // @ts-expect-error todos => todos.map(t => t.id), { - memoize: defaultMemoize, - argsMemoize: defaultMemoize, + memoize: lruMemoize, + argsMemoize: lruMemoize, memoizeOptions: { equalityCheck: // @ts-expect-error (a, b) => a === b, maxSize: 2 }, - argsMemoizeOptions: { isPromise: false } // This field causes a type error since it does not match the options param of `defaultMemoize`. + argsMemoizeOptions: { isPromise: false } // This field causes a type error since it does not match the options param of `lruMemoize`. } ) const selectorMicroMemoizePartiallyOverridden1 = - // @ts-expect-error Since `argsMemoize` is set to `defaultMemoize`, `argsMemoizeOptions` must match the options object parameter of `defaultMemoize` + // @ts-expect-error Since `argsMemoize` is set to `lruMemoize`, `argsMemoizeOptions` must match the options object parameter of `lruMemoize` createSelectorMicroMemoize( (state: RootState) => state.todos, // @ts-expect-error todos => todos.map(t => t.id), { - memoize: defaultMemoize, - argsMemoize: defaultMemoize, + memoize: lruMemoize, + argsMemoize: lruMemoize, memoizeOptions: [ { equalityCheck: @@ -687,7 +687,7 @@ describe('memoize and argsMemoize', () => { maxSize: 2 } ], - argsMemoizeOptions: [{ isPromise: false }] // This field causes a type error since it does not match the options param of `defaultMemoize`. + argsMemoizeOptions: [{ isPromise: false }] // This field causes a type error since it does not match the options param of `lruMemoize`. } ) const selectorMicroMemoizePartiallyOverridden2 = createSelectorMicroMemoize( @@ -784,7 +784,7 @@ describe('memoize and argsMemoize', () => { test('memoize And argsMemoize In createSelectorCreator', () => { // If we don't pass in `argsMemoize`, the type for `argsMemoizeOptions` - // falls back to the options parameter of `defaultMemoize`. + // falls back to the options parameter of `lruMemoize`. const createSelectorArgsMemoizeOptionsFallbackToDefault = createSelectorCreator({ memoize: microMemoize, @@ -857,7 +857,7 @@ describe('memoize and argsMemoize', () => { ).toEqualTypeOf(weakMapMemoize) const createSelectorWithWrongArgsMemoizeOptions = - // @ts-expect-error If we don't pass in `argsMemoize`, the type for `argsMemoizeOptions` falls back to the options parameter of `defaultMemoize`. + // @ts-expect-error If we don't pass in `argsMemoize`, the type for `argsMemoizeOptions` falls back to the options parameter of `lruMemoize`. createSelectorCreator({ memoize: microMemoize, memoizeOptions: { isEqual: (a, b) => a === b }, diff --git a/type-tests/createSelectorCreator.test-d.ts b/type-tests/createSelectorCreator.test-d.ts index da5897277..e91205f19 100644 --- a/type-tests/createSelectorCreator.test-d.ts +++ b/type-tests/createSelectorCreator.test-d.ts @@ -3,7 +3,7 @@ import memoizeOne from 'memoize-one' import microMemoize from 'micro-memoize' import { createSelectorCreator, - defaultMemoize, + lruMemoize, unstable_autotrackMemoize as autotrackMemoize, weakMapMemoize } from 'reselect' @@ -28,7 +28,7 @@ const state: RootState = { describe('createSelectorCreator', () => { test('options object as argument', () => { const createSelectorDefault = createSelectorCreator({ - memoize: defaultMemoize + memoize: lruMemoize }) const createSelectorWeakMap = createSelectorCreator({ memoize: weakMapMemoize @@ -48,7 +48,7 @@ describe('createSelectorCreator', () => { }) test('memoize function as argument', () => { - const createSelectorDefault = createSelectorCreator(defaultMemoize) + const createSelectorDefault = createSelectorCreator(lruMemoize) const createSelectorWeakMap = createSelectorCreator(weakMapMemoize) const createSelectorAutotrack = createSelectorCreator(autotrackMemoize) const createSelectorMicro = createSelectorCreator(microMemoize) diff --git a/type-tests/deepNesting.test-d.ts b/type-tests/deepNesting.test-d.ts index 5a5e69bc2..e602026f1 100644 --- a/type-tests/deepNesting.test-d.ts +++ b/type-tests/deepNesting.test-d.ts @@ -1,5 +1,5 @@ import microMemoize from 'micro-memoize' -import { createSelector, defaultMemoize } from 'reselect' +import { createSelector, lruMemoize } from 'reselect' import { describe, test } from 'vitest' interface RootState { @@ -75,91 +75,91 @@ describe('deep nesting', () => { const selector0 = createSelector(readOne, one => one) const selector1 = createSelector(selector0, s => s, { - memoize: defaultMemoize + memoize: lruMemoize }) const selector2 = createSelector(selector1, s => s, { - memoize: defaultMemoize + memoize: lruMemoize }) const selector3 = createSelector(selector2, s => s, { - memoize: defaultMemoize + memoize: lruMemoize }) const selector4 = createSelector(selector3, s => s, { - memoize: defaultMemoize + memoize: lruMemoize }) const selector5 = createSelector(selector4, s => s, { - memoize: defaultMemoize + memoize: lruMemoize }) const selector6 = createSelector(selector5, s => s, { - memoize: defaultMemoize + memoize: lruMemoize }) const selector7 = createSelector(selector6, s => s, { - memoize: defaultMemoize + memoize: lruMemoize }) const selector8 = createSelector(selector7, s => s, { - memoize: defaultMemoize + memoize: lruMemoize }) const selector9 = createSelector(selector8, s => s, { - memoize: defaultMemoize + memoize: lruMemoize }) const selector10 = createSelector(selector9, s => s, { - memoize: defaultMemoize + memoize: lruMemoize }) const selector11 = createSelector(selector10, s => s, { - memoize: defaultMemoize + memoize: lruMemoize }) const selector12 = createSelector(selector11, s => s, { - memoize: defaultMemoize + memoize: lruMemoize }) const selector13 = createSelector(selector12, s => s, { - memoize: defaultMemoize + memoize: lruMemoize }) const selector14 = createSelector(selector13, s => s, { - memoize: defaultMemoize + memoize: lruMemoize }) const selector15 = createSelector(selector14, s => s, { - memoize: defaultMemoize + memoize: lruMemoize }) const selector16 = createSelector(selector15, s => s, { - memoize: defaultMemoize + memoize: lruMemoize }) const selector17 = createSelector(selector16, s => s, { - memoize: defaultMemoize + memoize: lruMemoize }) const selector18 = createSelector(selector17, s => s, { - memoize: defaultMemoize + memoize: lruMemoize }) const selector19 = createSelector(selector18, s => s, { - memoize: defaultMemoize + memoize: lruMemoize }) const selector20 = createSelector(selector19, s => s, { - memoize: defaultMemoize + memoize: lruMemoize }) const selector21 = createSelector(selector20, s => s, { - memoize: defaultMemoize + memoize: lruMemoize }) const selector22 = createSelector(selector21, s => s, { - memoize: defaultMemoize + memoize: lruMemoize }) const selector23 = createSelector(selector22, s => s, { - memoize: defaultMemoize + memoize: lruMemoize }) const selector24 = createSelector(selector23, s => s, { - memoize: defaultMemoize + memoize: lruMemoize }) const selector25 = createSelector(selector24, s => s, { - memoize: defaultMemoize + memoize: lruMemoize }) const selector26 = createSelector(selector25, s => s, { - memoize: defaultMemoize + memoize: lruMemoize }) const selector27 = createSelector(selector26, s => s, { - memoize: defaultMemoize + memoize: lruMemoize }) const selector28 = createSelector(selector27, s => s, { - memoize: defaultMemoize + memoize: lruMemoize }) const selector29 = createSelector(selector28, s => s, { - memoize: defaultMemoize + memoize: lruMemoize }) }) From fd5eebaba6e7711338de44094ab442532460d789 Mon Sep 17 00:00:00 2001 From: Arya Emami Date: Fri, 1 Dec 2023 15:30:56 -0600 Subject: [PATCH 08/13] Change `DefaultMemoizeOptions` to `LruMemoizeOptions` in `README` --- README.md | 4 ++-- src/defaultMemoize.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 45bbb60ef..fb5b0fd81 100644 --- a/README.md +++ b/README.md @@ -627,7 +627,7 @@ Since 4.1.0, `lruMemoize` also accepts an options object as its first argument i ```ts type EqualityFn = (a: any, b: any) => boolean -interface DefaultMemoizeOptions { +interface LruMemoizeOptions { equalityCheck?: EqualityFn resultEqualityCheck?: EqualityFn maxSize?: number @@ -1911,7 +1911,7 @@ Originally inspired by getters in [NuclearJS](https://github.com/optimizely/nucl [output selector fields]: #output-selector-fields 'Output Selector Fields' [`createSelector`]: #createselectorinputselectors--inputselectors-resultfunc-createselectoroptions 'createSelector' [`createSelectorCreator`]: #createselectorcreatormemoize--options-memoizeoptions 'createSelectorCreator' -[`lruMemoize`]: #lrumemoizefunc-equalitycheckoroptions--defaultequalitycheck 'lruMemoize' +[`lruMemoize`]: #lrumemoizefunc-equalitycheckoroptions--referenceequalitycheck 'lruMemoize' [`weakMapMemoize`]: #weakmapmemoizefunc---since-500 'weakMapMemoize' [`unstable_autotrackMemoize`]: #unstable_autotrackmemoizefunc---since-500 'unstable_autotrackMemoize' [`createStructuredSelector`]: #createstructuredselector-inputSelectorsObject--selectorcreator--createselector 'createStructuredSelector' diff --git a/src/defaultMemoize.ts b/src/defaultMemoize.ts index 00c4416b3..4c45af0a2 100644 --- a/src/defaultMemoize.ts +++ b/src/defaultMemoize.ts @@ -181,7 +181,7 @@ export interface LruMemoizeOptions { * * @template Func - The type of the function that is memoized. * - * @see {@link https://github.com/reduxjs/reselect#lrumemoizefunc-equalitycheckoroptions--defaultequalitycheck lruMemoize} + * @see {@link https://github.com/reduxjs/reselect#lrumemoizefunc-equalitycheckoroptions--referenceequalitycheck lruMemoize} * * @public */ @@ -209,10 +209,10 @@ export function lruMemoize( ? createSingletonCache(comparator) : createLruCache(maxSize, comparator) - // we reference arguments instead of spreading them for performance reasons function memoized() { let value = cache.get(arguments) as ReturnType if (value === NOT_FOUND) { + // apply arguments instead of spreading for performance. // @ts-ignore value = func.apply(null, arguments) as ReturnType resultsCount++ From 8e5d9bfa9e08558be4a947c07dc07df3ff1c7349 Mon Sep 17 00:00:00 2001 From: Arya Emami Date: Fri, 1 Dec 2023 15:34:25 -0600 Subject: [PATCH 09/13] Fix negative `resultsCount` in `lruMemoize` --- src/defaultMemoize.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/defaultMemoize.ts b/src/defaultMemoize.ts index 4c45af0a2..b38c7657b 100644 --- a/src/defaultMemoize.ts +++ b/src/defaultMemoize.ts @@ -225,7 +225,7 @@ export function lruMemoize( if (matchingEntry) { value = matchingEntry.value as ReturnType - resultsCount-- + resultsCount !== 0 && resultsCount-- } } From 8affd65c4a04cd0a1e44b2cfffff5fa6c641cd9f Mon Sep 17 00:00:00 2001 From: Arya Emami Date: Fri, 1 Dec 2023 16:00:22 -0600 Subject: [PATCH 10/13] Rename `defaultMemoize.ts` to `lruMemoize.ts` --- src/autotrackMemoize/autotrackMemoize.ts | 2 +- src/createStructuredSelector.ts | 2 +- src/index.ts | 4 ++-- src/{defaultMemoize.ts => lruMemoize.ts} | 0 test/testUtils.ts | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) rename src/{defaultMemoize.ts => lruMemoize.ts} (100%) diff --git a/src/autotrackMemoize/autotrackMemoize.ts b/src/autotrackMemoize/autotrackMemoize.ts index 1672cc571..87cc495ad 100644 --- a/src/autotrackMemoize/autotrackMemoize.ts +++ b/src/autotrackMemoize/autotrackMemoize.ts @@ -4,7 +4,7 @@ import type { Node } from './tracking' import { createCacheKeyComparator, referenceEqualityCheck -} from '../defaultMemoize' +} from '../lruMemoize' import type { AnyFunction, DefaultMemoizeFields, Simplify } from '../types' import { createCache } from './autotracking' diff --git a/src/createStructuredSelector.ts b/src/createStructuredSelector.ts index 0d9e185db..1d933818f 100644 --- a/src/createStructuredSelector.ts +++ b/src/createStructuredSelector.ts @@ -1,7 +1,7 @@ import { createSelector } from './createSelectorCreator' import type { CreateSelectorFunction } from './createSelectorCreator' -import type { lruMemoize } from './defaultMemoize' +import type { lruMemoize } from './lruMemoize' import type { InterruptRecursion, ObjectValuesToTuple, diff --git a/src/index.ts b/src/index.ts index 7aab57d0f..acfe7cf16 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,8 +6,8 @@ export type { StructuredSelectorCreator, TypedStructuredSelectorCreator } from './createStructuredSelector' -export { lruMemoize, referenceEqualityCheck } from './defaultMemoize' -export type { LruMemoizeOptions } from './defaultMemoize' +export { lruMemoize, referenceEqualityCheck } from './lruMemoize' +export type { LruMemoizeOptions } from './lruMemoize' export { setGlobalDevModeChecks } from './devModeChecks/setGlobalDevModeChecks' export type { Combiner, diff --git a/src/defaultMemoize.ts b/src/lruMemoize.ts similarity index 100% rename from src/defaultMemoize.ts rename to src/lruMemoize.ts diff --git a/test/testUtils.ts b/test/testUtils.ts index 1b8a2e6b8..7852c6898 100644 --- a/test/testUtils.ts +++ b/test/testUtils.ts @@ -1,7 +1,7 @@ import type { PayloadAction } from '@reduxjs/toolkit' import { combineReducers, configureStore, createSlice } from '@reduxjs/toolkit' import { test } from 'vitest' -import type { lruMemoize } from '../src/defaultMemoize' +import type { lruMemoize } from '../src/lruMemoize' import type { AnyFunction, OutputSelector, Simplify } from '../src/types' export interface Todo { From c6ab5433f487c545d7d1d29f6479efe757bbd261 Mon Sep 17 00:00:00 2001 From: Arya Emami Date: Fri, 1 Dec 2023 16:05:29 -0600 Subject: [PATCH 11/13] Rename `defaultMemoize` test file --- test/{defaultMemoize.spec.ts => lruMemoize.test.ts} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename test/{defaultMemoize.spec.ts => lruMemoize.test.ts} (100%) diff --git a/test/defaultMemoize.spec.ts b/test/lruMemoize.test.ts similarity index 100% rename from test/defaultMemoize.spec.ts rename to test/lruMemoize.test.ts From e446d84c381bee78fc8b6caaaf640c7369540474 Mon Sep 17 00:00:00 2001 From: Arya Emami Date: Fri, 1 Dec 2023 16:53:55 -0600 Subject: [PATCH 12/13] Replace `lruMemoize` with `weakMapMemoize` in `README` --- README.md | 21 +++++++++++---------- src/createStructuredSelector.ts | 14 +++++++------- test/examples.test.ts | 8 ++++---- test/testUtils.ts | 9 +++++++-- 4 files changed, 29 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index fb5b0fd81..2532235e7 100644 --- a/README.md +++ b/README.md @@ -349,7 +349,7 @@ Accepts either a `memoize` function and `...memoizeOptions` rest parameter, or s | Name | Description | | :----------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | `options` | An options object containing the `memoize` function responsible for memoizing the `resultFunc` inside [`createSelector`] (e.g., `lruMemoize` or `weakMapMemoize`). It also provides additional options for customizing memoization. While the `memoize` property is mandatory, the rest are optional. | -| `options.argsMemoize?` | The optional memoize function that is used to memoize the arguments passed into the [output selector] generated by [`createSelector`] (e.g., `lruMemoize` or `weakMapMemoize`).
**`Default`** `lruMemoize` | +| `options.argsMemoize?` | The optional memoize function that is used to memoize the arguments passed into the [output selector] generated by [`createSelector`] (e.g., `lruMemoize` or `weakMapMemoize`).
**`Default`** `weakMapMemoize` | | `options.argsMemoizeOptions?` | Optional configuration options for the `argsMemoize` function. These options are passed to the `argsMemoize` function as the second argument.
since 5.0.0 | | `options.devModeChecks?` | Overrides the settings for the global development mode checks for the selector.
since 5.0.0 | | `options.memoize` | The memoize function that is used to memoize the `resultFunc` inside [`createSelector`] (e.g., `lruMemoize` or `weakMapMemoize`). since 5.0.0 | @@ -359,7 +359,7 @@ Accepts either a `memoize` function and `...memoizeOptions` rest parameter, or s | Name | Description | | :-------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------- | -| `memoize` | The `memoize` function responsible for memoizing the `resultFunc` inside [`createSelector`] (e.g., `lruMemoize` or `weakMapMemoize`). | +| `memoize` | The `memoize` function responsible for memoizing the `resultFunc` inside [`createSelector`] (e.g., `lruMemoize` or `weakMapMemoize`). | | `...memoizeOptionsFromArgs` | Optional configuration options for the memoization function. These options are then passed to the memoize function as the second argument onwards. | Returns @@ -368,8 +368,8 @@ A customized [`createSelector`] function.
Type parameters -| Name | Description | -| :-------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| Name | Description | +| :-------------------- | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `MemoizeFunction` | The type of the memoize function that is used to memoize the `resultFunc` inside [`createSelector`] (e.g., `lruMemoize` 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., `lruMemoize` or `weakMapMemoize`). If none is explicitly provided, `weakMapMemoize` will be used. | @@ -1666,14 +1666,14 @@ const selectTodoByIdCurried = currySelector( Or for reusability you can do this: ```ts -import type { lruMemoize, SelectorArray, UnknownMemoizer } from 'reselect' +import type { weakMapMemoize, SelectorArray, UnknownMemoizer } from 'reselect' import { createSelector } from 'reselect' export const createCurriedSelector = < InputSelectors extends SelectorArray, Result, - OverrideMemoizeFunction extends UnknownMemoizer = typeof lruMemoize, - OverrideArgsMemoizeFunction extends UnknownMemoizer = typeof lruMemoize + OverrideMemoizeFunction extends UnknownMemoizer = typeof weakMapMemoize, + OverrideArgsMemoizeFunction extends UnknownMemoizer = typeof weakMapMemoize >( ...args: Parameters< typeof createSelector< @@ -1766,7 +1766,8 @@ import type { OutputSelector, Selector, SelectorArray, - UnknownMemoizer + UnknownMemoizer, + weakMapMemoize } from 'reselect' import { createSelector } from 'reselect' @@ -1777,8 +1778,8 @@ interface RootState { export type TypedCreateSelector< State, - MemoizeFunction extends UnknownMemoizer = typeof lruMemoize, - ArgsMemoizeFunction extends UnknownMemoizer = typeof lruMemoize + MemoizeFunction extends UnknownMemoizer = typeof weakMapMemoize, + ArgsMemoizeFunction extends UnknownMemoizer = typeof weakMapMemoize > = < InputSelectors extends readonly Selector[], Result, diff --git a/src/createStructuredSelector.ts b/src/createStructuredSelector.ts index 1d933818f..3a13e05bb 100644 --- a/src/createStructuredSelector.ts +++ b/src/createStructuredSelector.ts @@ -1,7 +1,6 @@ import { createSelector } from './createSelectorCreator' import type { CreateSelectorFunction } from './createSelectorCreator' -import type { lruMemoize } from './lruMemoize' import type { InterruptRecursion, ObjectValuesToTuple, @@ -11,6 +10,7 @@ import type { UnknownMemoizer } from './types' import { assertIsObject } from './utils' +import type { weakMapMemoize } from './weakMapMemoize' /** * @@ -36,8 +36,8 @@ export interface TypedStructuredSelectorCreator { } = { [Key in keyof RootState]: Selector }, - MemoizeFunction extends UnknownMemoizer = typeof lruMemoize, - ArgsMemoizeFunction extends UnknownMemoizer = typeof lruMemoize + MemoizeFunction extends UnknownMemoizer = typeof weakMapMemoize, + ArgsMemoizeFunction extends UnknownMemoizer = typeof weakMapMemoize >( selectors: InputSelectorsObject, selectorCreator?: CreateSelectorFunction< @@ -140,8 +140,8 @@ export interface StructuredSelectorCreator { */ < InputSelectorsObject extends SelectorsObject, - MemoizeFunction extends UnknownMemoizer = typeof lruMemoize, - ArgsMemoizeFunction extends UnknownMemoizer = typeof lruMemoize + MemoizeFunction extends UnknownMemoizer = typeof weakMapMemoize, + ArgsMemoizeFunction extends UnknownMemoizer = typeof weakMapMemoize >( inputSelectorsObject: InputSelectorsObject, selectorCreator?: CreateSelectorFunction< @@ -209,8 +209,8 @@ export interface StructuredSelectorCreator { */ export const createStructuredSelector: StructuredSelectorCreator = (< InputSelectorsObject extends SelectorsObject, - MemoizeFunction extends UnknownMemoizer = typeof lruMemoize, - ArgsMemoizeFunction extends UnknownMemoizer = typeof lruMemoize + MemoizeFunction extends UnknownMemoizer = typeof weakMapMemoize, + ArgsMemoizeFunction extends UnknownMemoizer = typeof weakMapMemoize >( inputSelectorsObject: InputSelectorsObject, selectorCreator: CreateSelectorFunction< diff --git a/test/examples.test.ts b/test/examples.test.ts index c8f1a68f6..52132c941 100644 --- a/test/examples.test.ts +++ b/test/examples.test.ts @@ -126,8 +126,8 @@ test.todo('Find Fastest Selector', () => { test('TypedCreateSelector', () => { type TypedCreateSelector< State, - MemoizeFunction extends UnknownMemoizer = typeof lruMemoize, - ArgsMemoizeFunction extends UnknownMemoizer = typeof lruMemoize + MemoizeFunction extends UnknownMemoizer = typeof weakMapMemoize, + ArgsMemoizeFunction extends UnknownMemoizer = typeof weakMapMemoize > = < InputSelectors extends readonly Selector[], Result, @@ -178,8 +178,8 @@ test('createCurriedSelector copy paste pattern', () => { const createCurriedSelector = < InputSelectors extends SelectorArray, Result, - OverrideMemoizeFunction extends UnknownMemoizer = typeof lruMemoize, - OverrideArgsMemoizeFunction extends UnknownMemoizer = typeof lruMemoize + OverrideMemoizeFunction extends UnknownMemoizer = typeof weakMapMemoize, + OverrideArgsMemoizeFunction extends UnknownMemoizer = typeof weakMapMemoize >( ...args: Parameters< typeof createSelector< diff --git a/test/testUtils.ts b/test/testUtils.ts index 7852c6898..e095ef287 100644 --- a/test/testUtils.ts +++ b/test/testUtils.ts @@ -2,7 +2,12 @@ import type { PayloadAction } from '@reduxjs/toolkit' import { combineReducers, configureStore, createSlice } from '@reduxjs/toolkit' import { test } from 'vitest' import type { lruMemoize } from '../src/lruMemoize' -import type { AnyFunction, OutputSelector, Simplify } from '../src/types' +import type { + AnyFunction, + OutputSelector, + SelectorArray, + Simplify +} from '../src/types' export interface Todo { id: number @@ -490,7 +495,7 @@ export const logRecomputations = (selector: S) => { } export const logSelectorRecomputations = < - S extends OutputSelector + S extends OutputSelector >( selector: S ) => { From 60ba8d68be3112a9d1aa483758570d4a92849845 Mon Sep 17 00:00:00 2001 From: Mark Erikson Date: Fri, 1 Dec 2023 21:14:02 -0500 Subject: [PATCH 13/13] Rename remaining `defaultMemoize` references --- CHANGELOG.md | 110 ++++++++++++------------ test/reselect.spec.ts | 18 ++-- type-tests/argsMemoize.test-d.ts | 12 +-- typescript_test/argsMemoize.typetest.ts | 10 +-- 4 files changed, 76 insertions(+), 74 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2f628faa2..da2a48a7a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,11 @@ # Change log -All notable changes to this project will be documented in this file. +Changes in this project are primarily documented in the Github release notes: + +- https://github.com/reduxjs/reselect/releases + +Changes from v4.0.0 and earlier are documented in this file + This project adheres to [Semantic Versioning](http://semver.org/). ## [v4.0.0](https://github.com/reduxjs/reselect/releases/tag/v4.0.0) - 2018/09/30 @@ -8,11 +13,11 @@ This project adheres to [Semantic Versioning](http://semver.org/). ### New Features Exposed selector dependencies (#251) -Use provided memoize function for selectors (#297) +Use provided memoize function for selectors (#297) ### Breaking Changes -Updated TypeScript typings (#274, #315) +Updated TypeScript typings (#274, #315) ## [v3.0.0](https://github.com/reduxjs/reselect/releases/tag/v3.0.0) - 2017/03/24 @@ -28,7 +33,7 @@ For performance reasons, a selector is now not recalculated if its input is equa #### Example: ```js -import { createSelector } from 'reselect'; +import { createSelector } from 'reselect' const mySelector = createSelector( state => state.values.filter(val => val < 5), @@ -38,7 +43,7 @@ const mySelector = createSelector( } ) -var createSelector = require('./dist/reselect.js').createSelector; +var createSelector = require('./dist/reselect.js').createSelector const mySelector = createSelector( state => state.values.filter(val => val < 5), @@ -48,14 +53,14 @@ const mySelector = createSelector( } ) -var state1 = {values: [1,2,3,4,5,6,7,8,9]}; -console.log(mySelector(state1)); -state1.values = [3,4,5,6,7,8,9]; -console.log(mySelector(state1)); -var state2 = {values: [1,2,3,4,5,6,7,8,9]}; -console.log(mySelector(state2)); -var state3 = {values: [3,4,5,6,7]}; -console.log(mySelector(state3)); +var state1 = { values: [1, 2, 3, 4, 5, 6, 7, 8, 9] } +console.log(mySelector(state1)) +state1.values = [3, 4, 5, 6, 7, 8, 9] +console.log(mySelector(state1)) +var state2 = { values: [1, 2, 3, 4, 5, 6, 7, 8, 9] } +console.log(mySelector(state2)) +var state3 = { values: [3, 4, 5, 6, 7] } +console.log(mySelector(state3)) ``` #### Output in v2.5.4: @@ -171,36 +176,36 @@ There is a small chance that this could cause a breaking change in code that con #### `createStructuredSelector` -`createStructuredSelector` is a convenience function that helps with a common pattern when using Reselect. The selector passed to a connect decorator often just takes other selectors and maps them to keys in an object: +`createStructuredSelector` is a convenience function that helps with a common pattern when using Reselect. The selector passed to a connect decorator often just takes other selectors and maps them to keys in an object: ```js -const mySelectorA = state => state.a; -const mySelectorB = state => state.b; +const mySelectorA = state => state.a +const mySelectorB = state => state.b const structuredSelector = createSelector( - mySelectorA, - mySelectorB, - mySelectorC, - (a, b, c) => ({ - a, - b, - c - }) -); + mySelectorA, + mySelectorB, + mySelectorC, + (a, b, c) => ({ + a, + b, + c + }) +) ``` `createStructuredSelector` takes an object whose properties are input-selectors and returns a structured selector. The structured selector returns an object with the same keys as the `inputSelectors` argument, but with the selectors replaced with their values. ```js -const mySelectorA = state => state.a; -const mySelectorB = state => state.b; +const mySelectorA = state => state.a +const mySelectorB = state => state.b const structuredSelector = createStructuredSelector({ x: mySelectorA, y: mySelectorB -}); +}) -const result = structuredSelector({a: 1, b: 2}); // will produce {x: 1, y: 2} +const result = structuredSelector({ a: 1, b: 2 }) // will produce {x: 1, y: 2} ``` ## [v1.0.0](https://github.com/reduxjs/reselect/releases/tag/v1.0.0) - 2015/09/09 @@ -225,22 +230,19 @@ js:next field added to package.json #### Before ```js -import { isEqual } from 'lodash'; -import { createSelectorCreator } from 'reselect'; +import { isEqual } from 'lodash' +import { createSelectorCreator } from 'reselect' -const deepEqualsSelectorCreator = createSelectorCreator(isEqual); +const deepEqualsSelectorCreator = createSelectorCreator(isEqual) ``` #### After ```js -import { isEqual } from 'lodash'; -import { createSelectorCreator, defaultMemoize } from 'reselect'; +import { isEqual } from 'lodash' +import { createSelectorCreator, defaultMemoize } from 'reselect' -const deepEqualsSelectorCreator = createSelectorCreator( - defaultMemoize, - isEqual -); +const deepEqualsSelectorCreator = createSelectorCreator(defaultMemoize, isEqual) ``` ### New features @@ -255,7 +257,7 @@ Selector creators can receive a variadic number of dependencies as well as an ar const selector = createSelector( [state => state.a, state => state.b], (a, b) => a * b -); +) ``` #### After @@ -265,7 +267,7 @@ const selector = createSelector( state => state.a, state => state.b, (a, b) => a * b -); +) ``` #### Access `ownProps` in Selector @@ -274,32 +276,32 @@ Selector dependencies can receive a variadic number of parameters allowing a sel ```js const selector = createSelector( - (state) => state.a, + state => state.a, (state, props) => state.b * props.c, (_, props) => props.d, (a, bc, d) => a + bc + d -); +) ``` #### Configurable Memoize Function ```js -import { createSelectorCreator } from 'reselect'; -import memoize from 'lodash.memoize'; +import { createSelectorCreator } from 'reselect' +import memoize from 'lodash.memoize' -let called = 0; -const customSelectorCreator = createSelectorCreator(memoize, JSON.stringify); +let called = 0 +const customSelectorCreator = createSelectorCreator(memoize, JSON.stringify) const selector = customSelectorCreator( state => state.a, state => state.b, (a, b) => { - called++; - return a + b; + called++ + return a + b } -); -assert.equal(selector({a: 1, b: 2}), 3); -assert.equal(selector({a: 1, b: 2}), 3); -assert.equal(called, 1); -assert.equal(selector({a: 2, b: 3}), 5); -assert.equal(called, 2); +) +assert.equal(selector({ a: 1, b: 2 }), 3) +assert.equal(selector({ a: 1, b: 2 }), 3) +assert.equal(called, 1) +assert.equal(selector({ a: 2, b: 3 }), 5) +assert.equal(called, 2) ``` diff --git a/test/reselect.spec.ts b/test/reselect.spec.ts index 36c8500b4..047dfb8bf 100644 --- a/test/reselect.spec.ts +++ b/test/reselect.spec.ts @@ -343,7 +343,7 @@ describe('Customizing selectors', () => { let memoizer2Calls = 0 let memoizer3Calls = 0 - const defaultMemoizeAcceptsFirstArgDirectly = createSelector( + const lruMemoizeAcceptsFirstArgDirectly = createSelector( (state: StateAB) => state.a, (state: StateAB) => state.b, (a, b) => a + b, @@ -356,12 +356,12 @@ describe('Customizing selectors', () => { } ) - defaultMemoizeAcceptsFirstArgDirectly({ a: 1, b: 2 }) - defaultMemoizeAcceptsFirstArgDirectly({ a: 1, b: 3 }) + lruMemoizeAcceptsFirstArgDirectly({ a: 1, b: 2 }) + lruMemoizeAcceptsFirstArgDirectly({ a: 1, b: 3 }) expect(memoizer1Calls).toBeGreaterThan(0) - const defaultMemoizeAcceptsArgsAsArray = createSelector( + const lruMemoizeAcceptsArgsAsArray = createSelector( (state: StateAB) => state.a, (state: StateAB) => state.b, (a, b) => a + b, @@ -376,8 +376,8 @@ describe('Customizing selectors', () => { } ) - defaultMemoizeAcceptsArgsAsArray({ a: 1, b: 2 }) - defaultMemoizeAcceptsArgsAsArray({ a: 1, b: 3 }) + lruMemoizeAcceptsArgsAsArray({ a: 1, b: 2 }) + lruMemoizeAcceptsArgsAsArray({ a: 1, b: 3 }) expect(memoizer2Calls).toBeGreaterThan(0) @@ -389,14 +389,14 @@ describe('Customizing selectors', () => { } ) - const defaultMemoizeAcceptsArgFromCSC = createSelectorWithSeparateArg( + const lruMemoizeAcceptsArgFromCSC = createSelectorWithSeparateArg( (state: StateAB) => state.a, (state: StateAB) => state.b, (a, b) => a + b ) - defaultMemoizeAcceptsArgFromCSC({ a: 1, b: 2 }) - defaultMemoizeAcceptsArgFromCSC({ a: 1, b: 3 }) + lruMemoizeAcceptsArgFromCSC({ a: 1, b: 2 }) + lruMemoizeAcceptsArgFromCSC({ a: 1, b: 3 }) expect(memoizer3Calls).toBeGreaterThan(0) }) diff --git a/type-tests/argsMemoize.test-d.ts b/type-tests/argsMemoize.test-d.ts index 33e7678b4..f7531f5f2 100644 --- a/type-tests/argsMemoize.test-d.ts +++ b/type-tests/argsMemoize.test-d.ts @@ -239,13 +239,13 @@ describe('memoize and argsMemoize', () => { argsMemoizeOptions: { maxSize: 2 } } ) - // const createSelectorDefaultMemoize = createSelectorCreator(lruMemoize) - const createSelectorDefaultMemoize = createSelectorCreator({ + + const createSelectorLruMemoize = createSelectorCreator({ memoize: lruMemoize }) const selectorWeakMapSeparateInlineArgsWithMemoizeOptions3 = // @ts-expect-error When argsMemoize is weakMapMemoize, type of argsMemoizeOptions needs to be the same as options args in weakMapMemoize. - createSelectorDefaultMemoize( + createSelectorLruMemoize( (state: RootState) => state.todos, // @ts-expect-error todos => todos.map(t => t.id), @@ -265,7 +265,7 @@ describe('memoize and argsMemoize', () => { } ) const selectorWeakMapSeparateInlineArgsWithMemoizeOptions4 = - createSelectorDefaultMemoize( + createSelectorLruMemoize( // @ts-expect-error (state: RootState) => state.todos, // @ts-expect-error @@ -279,7 +279,7 @@ describe('memoize and argsMemoize', () => { ) const selectorWeakMapSeparateInlineArgsWithMemoizeOptions5 = // @ts-expect-error - createSelectorDefaultMemoize( + createSelectorLruMemoize( [(state: RootState) => state.todos], // @ts-expect-error todos => todos.map(t => t.id), @@ -291,7 +291,7 @@ describe('memoize and argsMemoize', () => { } ) const selectorWeakMapSeparateInlineArgsWithMemoizeOptions6 = - createSelectorDefaultMemoize( + createSelectorLruMemoize( (state: RootState) => state.todos, todos => todos.map(t => t.id), { diff --git a/typescript_test/argsMemoize.typetest.ts b/typescript_test/argsMemoize.typetest.ts index 4dde2d54a..4e58b6a71 100644 --- a/typescript_test/argsMemoize.typetest.ts +++ b/typescript_test/argsMemoize.typetest.ts @@ -235,13 +235,13 @@ function overrideOnlyArgsMemoizeInCreateSelector() { argsMemoizeOptions: { maxSize: 2 } } ) - // const createSelectorDefaultMemoize = createSelectorCreator(lruMemoize) - const createSelectorDefaultMemoize = createSelectorCreator({ + + const createSelectorLruMemoize = createSelectorCreator({ memoize: lruMemoize }) const selectorWeakMapSeparateInlineArgsWithMemoizeOptions3 = // @ts-expect-error When argsMemoize is weakMapMemoize, type of argsMemoizeOptions needs to be the same as options args in weakMapMemoize. - createSelectorDefaultMemoize( + createSelectorLruMemoize( (state: RootState) => state.todos, // @ts-expect-error todos => todos.map(t => t.id), @@ -263,7 +263,7 @@ function overrideOnlyArgsMemoizeInCreateSelector() { const selectorWeakMapSeparateInlineArgsWithMemoizeOptions5 = // @ts-expect-error - createSelectorDefaultMemoize( + createSelectorLruMemoize( [(state: RootState) => state.todos], // @ts-expect-error todos => todos.map(t => t.id), @@ -274,7 +274,7 @@ function overrideOnlyArgsMemoizeInCreateSelector() { } ) const selectorWeakMapSeparateInlineArgsWithMemoizeOptions6 = - createSelectorDefaultMemoize( + createSelectorLruMemoize( (state: RootState) => state.todos, todos => todos.map(t => t.id), {