Skip to content

Commit

Permalink
export a 'createSelectorWithCacheSize' function, added more notes to …
Browse files Browse the repository at this point in the history
…the README
  • Loading branch information
ndbroadbent committed Mar 29, 2017
1 parent 9babf91 commit b074b0f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
22 changes: 19 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ Takes one or more selectors, or an array of selectors, computes their values and
`createSelector` determines if the value returned by an input-selector has changed between calls using reference equality (`===`). Inputs to selectors created with `createSelector` should be immutable.

Selectors created with `createSelector` have a cache size of 1. This means they always recalculate when the value of an input-selector changes, as a selector only stores the preceding value of each input-selector.
Most of the time this is all you need, but you can use `createSelectorWithCacheSize` to set a larger cache size. You can also create [a custom selector creator]((#customize-equalitycheck-for-defaultmemoize) to also configure the equality check.

```js
const mySelector = createSelector(
Expand Down Expand Up @@ -457,13 +458,16 @@ const totalSelector = createSelector(

```

### createSelectorWithCacheSize(cacheSize, ...inputSelectors | [inputSelectors], resultFunc)

Same as createSelector, except you can configure the cache size using the first argument. When the cache is full, it will overwrite the oldest value.


### defaultMemoize(func, equalityCheck = defaultEqualityCheck, cacheSize = 1)

`defaultMemoize` memoizes the function passed in the func parameter. It is the memoize function used by `createSelector`.

`defaultMemoize` has a default cache size of 1. This means it always recalculates when the value of an argument changes.

You can specify a cache size larger than one, and the selector will store the last n results. When the cache is full, it will replace the oldest result with the new result. It does not care about order in which results are accessed, only created.
`defaultMemoize` has a default cache size of 1. This means it always recalculates when the value of an argument changes. You can specify a larger cache size to store multiple results. When the cache is full, it will overwrite the oldest result.

`defaultMemoize` 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:

Expand Down Expand Up @@ -507,6 +511,18 @@ customMemoize(resultFunc, option1, option2, option3)

Here are some examples of how you might use `createSelectorCreator`:

#### Customize `cacheSize` for `defaultMemoize`

```js
import { createSelectorCreator, defaultMemoize, defaultEqualityCheck } from 'reselect'

const createDeepEqualSelector = createSelectorCreator(
defaultMemoize,
defaultEqualityCheck,
5 // cache up to 5 results
)
```

#### Customize `equalityCheck` for `defaultMemoize`

```js
Expand Down
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ export function createSelectorCreator(memoize, ...memoizeOptions) {
}

export const createSelector = createSelectorCreator(defaultMemoize)
export const createSelectorWithCacheSize = (cacheSize, ...args) => (
createSelectorCreator(defaultMemoize, defaultEqualityCheck, cacheSize)
)(...args)

export function createStructuredSelector(selectors, selectorCreator = createSelector) {
if (typeof selectors !== 'object') {
Expand Down
16 changes: 6 additions & 10 deletions test/test_selector.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// TODO: Add test for React Redux connect function

import chai from 'chai'
import { createSelector, createSelectorCreator, defaultMemoize,
memoizeOne, createStructuredSelector
import { createSelector, createSelectorWithCacheSize, createSelectorCreator,
defaultMemoize, memoizeOne, createStructuredSelector
} from '../src/index'
import { default as lodashMemoize } from 'lodash.memoize'

Expand Down Expand Up @@ -99,7 +99,7 @@ suite('selector', () => {
return // don't run performance tests for coverage
}

const selector = createSelectorCreator(defaultMemoize, undefined, 2)(
const selector = createSelectorWithCacheSize(2,
state => state.a,
state => state.b,
(a, b) => a + b
Expand Down Expand Up @@ -152,7 +152,7 @@ suite('selector', () => {
return // don't run performance tests for coverage
}

const selector = createSelectorCreator(defaultMemoize, undefined, 2)(
const selector = createSelectorWithCacheSize(2,
state => state.a,
state => state.b,
(a, b) => a + b
Expand Down Expand Up @@ -514,9 +514,7 @@ suite('selector', () => {
})

test('defaultMemoize with cacheSize 2', () => {
const createSelectorWithCacheSize2 = createSelectorCreator(defaultMemoize, undefined, 2 )

const selector = createSelectorWithCacheSize2([
const selector = createSelectorWithCacheSize(2, [
(state) => state.num
], (num) => num * num)

Expand Down Expand Up @@ -569,9 +567,7 @@ suite('selector', () => {
})

test('defaultMemoize with cacheSize 5', () => {
const createSelectorWithCacheSize5 = createSelectorCreator(defaultMemoize, undefined, 5)

const selector = createSelectorWithCacheSize5([
const selector = createSelectorWithCacheSize(5, [
(state) => state.num
], (num) => num * num)

Expand Down

0 comments on commit b074b0f

Please sign in to comment.