Skip to content

Commit

Permalink
Slightly modify examples
Browse files Browse the repository at this point in the history
  - This change was made to emphasize the switch from `lruMemoize` to `weakMapMemoize` inside `createSelector`
  • Loading branch information
aryaemami59 committed Jan 3, 2024
1 parent 531a496 commit c8263b5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
9 changes: 3 additions & 6 deletions docs/examples/weakMapMemoize/cacheSizeSolution.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createSelector, weakMapMemoize } from 'reselect'
import { createSelector } from 'reselect'
import type { RootState } from './cacheSizeProblem'

const state: RootState = {
Expand All @@ -10,16 +10,13 @@ const state: RootState = {
]
}

// `createSelector` uses `weakMapMemoize` by default.
const selectItemsByCategory = createSelector(
[
(state: RootState) => state.items,
(state: RootState, category: string) => category
],
(items, category) => items.filter(item => item.category === category),
{
memoize: weakMapMemoize,
argsMemoize: weakMapMemoize
}
(items, category) => items.filter(item => item.category === category)
)

selectItemsByCategory(state, 'Electronics') // Selector runs
Expand Down
9 changes: 8 additions & 1 deletion docs/examples/weakMapMemoize/usingWithCreateSelector.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { shallowEqual } from 'react-redux'
import { createSelector, weakMapMemoize } from 'reselect'
import type { RootState } from './cacheSizeProblem'

Expand All @@ -18,7 +19,13 @@ const selectItemsByCategory = createSelector(
(items, category) => items.filter(item => item.category === category),
{
memoize: weakMapMemoize,
argsMemoize: weakMapMemoize
argsMemoize: weakMapMemoize,
argsMemoizeOptions: {
resultEqualityCheck: shallowEqual
},
memoizeOptions: {
resultEqualityCheck: shallowEqual
}
}
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { shallowEqual } from 'react-redux'
import { createSelectorCreator, weakMapMemoize } from 'reselect'
import type { RootState } from './cacheSizeProblem'

Expand All @@ -12,7 +13,13 @@ const state: RootState = {

const createSelectorWeakMap = createSelectorCreator({
memoize: weakMapMemoize,
argsMemoize: weakMapMemoize
argsMemoize: weakMapMemoize,
argsMemoizeOptions: {
resultEqualityCheck: shallowEqual
},
memoizeOptions: {
resultEqualityCheck: shallowEqual
}
})

const selectItemsByCategory = createSelectorWeakMap(
Expand Down

0 comments on commit c8263b5

Please sign in to comment.