Skip to content

Commit

Permalink
Fix README for v5.
Browse files Browse the repository at this point in the history
- Add documentation regarding new features.
- Redo `CodeSandbox` examples to align better with documentation. Solves reduxjs#598.
- Fix GitHub workflow badge URL reduxjs#628.
- Add instructions for `bun` and `pnpm`. Solves reduxjs#621.
- Fix minor type issues regarding `createStructuredSelector`. Solves reduxjs#499.
- `argsMemoize` and `argsMemoizeOptions` solve reduxjs#359.
- Remove `Redux` legacy patterns including `connect` and `switch` statements from docs.  Solves reduxjs#515.
- Replace legacy code patterns with modern syntax like `useSelector` and functional components.
- Implementation of `argsMemoize` solves reduxjs#376.
- Document order of execution in `Reselect`. Solves reduxjs#455.
- Add benchmarks to confirm the info inside the documentation.
- Add more type tests with `vitest`.
- Fix more hover preview issues with types.
  • Loading branch information
aryaemami59 committed Nov 11, 2023
1 parent e4fe262 commit f8cdab2
Show file tree
Hide file tree
Showing 35 changed files with 5,027 additions and 1,013 deletions.
2,189 changes: 1,676 additions & 513 deletions README.md

Large diffs are not rendered by default.

31 changes: 31 additions & 0 deletions docs/assets/diagrams/normal-memoization-function.drawio
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<mxfile host="65bd71144e" scale="1" border="0">
<diagram id="s5zgOlhoY8HzJwKQ5w-s" name="Page-1">
<mxGraphModel dx="1244" dy="636" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="2000" pageHeight="2000" background="#C7FFFB" math="0" shadow="1">
<root>
<mxCell id="0"/>
<mxCell id="1" parent="0"/>
<mxCell id="7" value="" style="group;aspect=fixed;shadow=1;" vertex="1" connectable="0" parent="1">
<mxGeometry x="60" y="460" width="681.5" height="290" as="geometry"/>
</mxCell>
<mxCell id="2" value="Are arguments same as last time?" style="whiteSpace=wrap;html=1;rounded=1;shadow=1;aspect=fixed;" parent="7" vertex="1">
<mxGeometry x="250" y="20" width="150" height="80" as="geometry"/>
</mxCell>
<mxCell id="3" value="Return result." style="whiteSpace=wrap;html=1;rounded=1;shadow=1;" parent="7" vertex="1">
<mxGeometry x="460" y="180" width="110" height="60" as="geometry"/>
</mxCell>
<object label="Yes" id="4">
<mxCell style="edgeStyle=none;html=1;entryX=0;entryY=0;entryDx=0;entryDy=0;shadow=1;" parent="7" source="2" target="3" edge="1">
<mxGeometry relative="1" as="geometry"/>
</mxCell>
</object>
<mxCell id="5" value="Run function again." style="whiteSpace=wrap;html=1;rounded=1;shadow=1;" parent="7" vertex="1">
<mxGeometry x="30" y="180" width="150" height="60" as="geometry"/>
</mxCell>
<mxCell id="6" value="No" style="edgeStyle=none;html=1;entryX=1;entryY=0;entryDx=0;entryDy=0;shadow=1;" parent="7" source="2" target="5" edge="1">
<mxGeometry relative="1" as="geometry"/>
</mxCell>
<mxCell id="8" value="Untitled Layer" parent="0"/>
</root>
</mxGraphModel>
</diagram>
</mxfile>
40 changes: 40 additions & 0 deletions docs/assets/diagrams/reselect-memoization.drawio
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<mxfile host="65bd71144e">
<diagram id="hFjyuBP9kS8yiWHOacYP" name="Page-1">
<mxGraphModel dx="792" dy="1668" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="850" pageHeight="1100" background="#C7FFFB" math="0" shadow="0">
<root>
<mxCell id="0"/>
<mxCell id="1" parent="0"/>
<mxCell id="15" value="" style="group" vertex="1" connectable="0" parent="1">
<mxGeometry x="70" y="-110" width="930" height="440" as="geometry"/>
</mxCell>
<mxCell id="2" value="Are arguments same as last time?" style="whiteSpace=wrap;html=1;rounded=1;shadow=1;" parent="15" vertex="1">
<mxGeometry x="470.735" y="35.2" width="139.57750000000001" height="58.036" as="geometry"/>
</mxCell>
<mxCell id="3" value="Return result." style="whiteSpace=wrap;html=1;rounded=1;shadow=1;" parent="15" vertex="1">
<mxGeometry x="680" y="159.2" width="132.19" height="80" as="geometry"/>
</mxCell>
<mxCell id="8" value="Yes" style="edgeStyle=none;html=1;entryX=0;entryY=0;entryDx=0;entryDy=0;shadow=1;" parent="15" source="2" target="3" edge="1">
<mxGeometry relative="1" as="geometry"/>
</mxCell>
<mxCell id="4" value="Run input selectors.&lt;br&gt;Are the results of input selectors same as last time?" style="whiteSpace=wrap;html=1;rounded=1;shadow=1;" parent="15" vertex="1">
<mxGeometry x="251.88" y="158.4" width="168.12" height="81.6" as="geometry"/>
</mxCell>
<mxCell id="9" value="No" style="edgeStyle=none;html=1;entryX=1;entryY=0;entryDx=0;entryDy=0;shadow=1;" parent="15" source="2" target="4" edge="1">
<mxGeometry relative="1" as="geometry"/>
</mxCell>
<mxCell id="6" value="Return result." style="whiteSpace=wrap;html=1;rounded=1;shadow=1;" parent="15" vertex="1">
<mxGeometry x="470.7375" y="310" width="135.625" height="44" as="geometry"/>
</mxCell>
<mxCell id="13" value="Yes" style="edgeStyle=none;html=1;entryX=0;entryY=0;entryDx=0;entryDy=0;shadow=1;" parent="15" source="4" target="6" edge="1">
<mxGeometry relative="1" as="geometry"/>
</mxCell>
<mxCell id="7" value="Run result function." style="whiteSpace=wrap;html=1;rounded=1;shadow=1;" parent="15" vertex="1">
<mxGeometry x="59.9975" y="310" width="135.625" height="44" as="geometry"/>
</mxCell>
<mxCell id="14" value="No" style="edgeStyle=none;html=1;entryX=1;entryY=0;entryDx=0;entryDy=0;shadow=1;" parent="15" source="4" target="7" edge="1">
<mxGeometry relative="1" as="geometry"/>
</mxCell>
</root>
</mxGraphModel>
</diagram>
</mxfile>
Binary file added docs/assets/normal-memoization-function.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/assets/reselect-memoization.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@
"format": "prettier --write \"{src,test}/**/*.{js,ts}\" \"docs/**/*.md\"",
"lint": "eslint src test",
"prepack": "yarn build",
"bench": "vitest --run bench",
"bench": "vitest --run bench --mode production",
"test": "node --expose-gc ./node_modules/vitest/dist/cli-wrapper.js run",
"test:cov": "vitest run --coverage",
"type-check": "vitest --run typecheck",
"test:typescript": "tsc --noEmit -p typescript_test/tsconfig.json"
},
"keywords": [
Expand Down
14 changes: 10 additions & 4 deletions src/autotrackMemoize/autotrackMemoize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import {
createCacheKeyComparator,
defaultEqualityCheck
} from '@internal/defaultMemoize'
import type { AnyFunction } from '@internal/types'
import type {
AnyFunction,
DefaultMemoizeFields,
Simplify
} from '@internal/types'
import { createCache } from './autotracking'

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

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

return memoized as Func & { clearCache: () => void }
return memoized as Func & Simplify<DefaultMemoizeFields>
}
132 changes: 132 additions & 0 deletions src/createCurriedSelectorCreator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import type { CreateSelectorFunction } from './createSelectorCreator'
import { createSelectorCreator } from './createSelectorCreator'

import { defaultMemoize } from './defaultMemoize'
import type {
Combiner,
CreateSelectorOptions,
CurriedOutputSelector,
DropFirstParameter,
InterruptRecursion,
SelectorArray,
Simplify,
UnknownMemoizer
} from './types'

/**
* @WIP
*/
export interface CreateCurriedSelector<
MemoizeFunction extends UnknownMemoizer = typeof defaultMemoize,
ArgsMemoizeFunction extends UnknownMemoizer = typeof defaultMemoize
> {
/**
* One arg
*/
<InputSelectors extends SelectorArray, Result>(
...createSelectorArgs: [
...inputSelectors: InputSelectors,
combiner: Combiner<InputSelectors, Result>
]
): CurriedOutputSelector<
InputSelectors,
Result,
MemoizeFunction,
ArgsMemoizeFunction
> &
InterruptRecursion

/**
* inline args
*/
<
InputSelectors extends SelectorArray,
Result,
OverrideMemoizeFunction extends UnknownMemoizer = MemoizeFunction,
OverrideArgsMemoizeFunction extends UnknownMemoizer = ArgsMemoizeFunction
>(
...createSelectorArgs: [
...inputSelectors: InputSelectors,
combiner: Combiner<InputSelectors, Result>,
createSelectorOptions: Simplify<
CreateSelectorOptions<
MemoizeFunction,
ArgsMemoizeFunction,
OverrideMemoizeFunction,
OverrideArgsMemoizeFunction
>
>
]
): CurriedOutputSelector<
InputSelectors,
Result,
OverrideMemoizeFunction,
OverrideArgsMemoizeFunction
> &
InterruptRecursion

/**
* array args
*/
<
InputSelectors extends SelectorArray,
Result,
OverrideMemoizeFunction extends UnknownMemoizer = MemoizeFunction,
OverrideArgsMemoizeFunction extends UnknownMemoizer = ArgsMemoizeFunction
>(
inputSelectors: [...InputSelectors],
combiner: Combiner<InputSelectors, Result>,
createSelectorOptions?: Simplify<
CreateSelectorOptions<
MemoizeFunction,
ArgsMemoizeFunction,
OverrideMemoizeFunction,
OverrideArgsMemoizeFunction
>
>
): CurriedOutputSelector<
InputSelectors,
Result,
OverrideMemoizeFunction,
OverrideArgsMemoizeFunction
> &
InterruptRecursion
}

/**
* @WIP
*/
export function createCurriedSelectorCreator<
MemoizeFunction extends UnknownMemoizer = typeof defaultMemoize,
ArgsMemoizeFunction extends UnknownMemoizer = typeof defaultMemoize
>(...createSelectorCreatorArgs: Parameters<typeof createSelectorCreator>) {
const createSelector = createSelectorCreator(...createSelectorCreatorArgs)

const createCurriedSelector = (
...createSelectorArgs: Parameters<
CreateSelectorFunction<MemoizeFunction, ArgsMemoizeFunction>
>
) => {
// @ts-ignore
const selector = createSelector.apply(null, createSelectorArgs)
// const selector = createSelector(...createSelectorArgs)
const curriedSelector = selector.argsMemoize(
(...params: DropFirstParameter<typeof selector>) => {
return selector.argsMemoize((state: Parameters<typeof selector>[0]) => {
return selector(state, ...params)
})
}
)
return Object.assign(curriedSelector, selector) as CurriedOutputSelector
}
return createCurriedSelector as unknown as CreateCurriedSelector<
MemoizeFunction,
ArgsMemoizeFunction
>
}

/**
* @WIP
*/
export const createCurriedSelector =
/* #__PURE__ */ createCurriedSelectorCreator(defaultMemoize)
Loading

0 comments on commit f8cdab2

Please sign in to comment.