Skip to content

Commit

Permalink
Rename resolver as keySelector
Browse files Browse the repository at this point in the history
  • Loading branch information
toomuchdesign committed Feb 10, 2019
1 parent a13d01e commit 80c516e
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 28 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -7,6 +7,7 @@
- Update TypeScript typings
- Introduce typings for heterogeneous selectors
- Introduce typings for any number of uniform selectors
- Rename "resolver" as "keySelector"

### New Features

Expand Down
30 changes: 15 additions & 15 deletions README.md
Expand Up @@ -37,7 +37,7 @@ const cachedSelector = createCachedSelector(
// resultFunc
(A, B) => expensiveComputation(A, B)
)(
// resolverFunction
// keySelector
// Instruct re-reselect to use "itemName" as cacheKey
(state, itemName) => itemName
);
Expand Down Expand Up @@ -75,8 +75,8 @@ const fooResultAgain = cachedSelector(state, 'foo');
- [How do I test a re-reselect selector?](#how-do-i-test-a-re-reselect-selector)
- [Testing `reselect` selectors stored in the cache](#testing-reselect-selectors-stored-in-the-cache)
- [API](#api)
- [reReselect([reselect's createSelector arguments])(resolverFunction, { cacheObject, selectorCreator })](#rereselectreselects-createselector-argumentsresolverfunction--cacheobject-selectorcreator)
- [resolverFunction](#resolverfunction)
- [reReselect([reselect's createSelector arguments])(keySelector, { cacheObject, selectorCreator })](#rereselectreselects-createselector-argumentskeyselector--cacheobject-selectorcreator-)
- [keySelector](#keyselector)
- [options.cacheObject](#optionscacheobject)
- [Custom cache strategy object](#custom-cache-strategy-object)
- [options.selectorCreator](#optionsselectorcreator)
Expand Down Expand Up @@ -122,9 +122,9 @@ What happens, here? `getPieceOfData` **selector cache is invalidated** on each c

`cacheKey` is by default a `string` or `number` but can be anything depending on the chosen cache strategy (see [`cacheObject` option](#optionscacheobject)).

`cacheKey` is the output of `resolverFunction`, declared at selector initialization.
`cacheKey` is the output of `keySelector`, declared at selector initialization.

`resolverFunction` is a **custom function** which:
`keySelector` is a **custom function** which:

- takes the same arguments as the final selector (in the example: `state`, `itemId`, `'dataX'`)
- returns a `cacheKey`.
Expand All @@ -149,7 +149,7 @@ const getPieceOfData = createCachedSelector(

But now, **each time the selector is called**, the following happens behind the scenes:

1. **Evaluate the `cacheKey`** for current call by executing `resolverFunction`
1. **Evaluate the `cacheKey`** for current call by executing `keySelector`
2. **Retrieve** from cache the **`reselect` selector** stored under the given `cacheKey`
3. **Return found selector or create a new one** if no selector was found
4. **Call returned selector** with provided arguments
Expand Down Expand Up @@ -198,7 +198,7 @@ export const getMyData = createSelector(
);
```

...add `resolverFunction` in the second function call:
...add `keySelector` in the second function call:

<!-- prettier-ignore -->
```js
Expand All @@ -222,9 +222,9 @@ const myData = getMyData(state, 'foo', 'bar');

### How do I use multiple inputs to set the cacheKey?

`cacheKey` is the return value of `resolverFunction`.
`cacheKey` is the return value of `keySelector`.

`resolverFunction` receives the same arguments of your `inputSelectors` and (by default) **must return a `string` or `number`.**
`keySelector` receives the same arguments of your `inputSelectors` and (by default) **must return a `string` or `number`.**

A few good examples and [a bonus](https://github.com/toomuchdesign/re-reselect/issues/3):

Expand Down Expand Up @@ -319,20 +319,20 @@ import reReselect from 're-reselect';
import createCachedSelector from 're-reselect';
```

### reReselect([reselect's createSelector arguments])(resolverFunction, { cacheObject, selectorCreator })
### reReselect([reselect's createSelector arguments])(keySelector, { cacheObject, selectorCreator })

**Re-reselect** accepts reselect's original [`createSelector` arguments][reselect-create-selector] and returns a new function which accepts **2 arguments**:

- `resolverFunction`
- `keySelector`
- `options { cacheObject, selectorCreator }` _(optional)_

#### resolverFunction
#### keySelector

`resolverFunction` is a custom function receiving the same arguments as your selectors (and `inputSelectors`) and **returning a `cacheKey`**.
`keySelector` is a custom function receiving the same arguments as your selectors (and `inputSelectors`) and **returning a `cacheKey`**.

`cacheKey` is **by default a `string` or `number`** but can be anything depending on the chosen cache strategy (see [`cacheObject` option](#optionscacheobject)).

The `resolverFunction` idea comes from [Lodash's .memoize][lodash-memoize].
The `keySelector` idea comes from [Lodash's .memoize][lodash-memoize].

#### options.cacheObject

Expand All @@ -358,7 +358,7 @@ import createCachedSelector, {LruObjectCache, LruMapCache} from 're-reselect';
createCachedSelector(
// ...
)(
resolverFunction,
keySelector,
{
cacheObject: new LruObjectCache({cacheSize: 5}),
// or:
Expand Down
8 changes: 4 additions & 4 deletions src/__tests__/index.spec.js
Expand Up @@ -15,24 +15,24 @@ beforeEach(() => {

function selectorWithMockedResultFunc() {
return createCachedSelector([], resultFuncMock)(
(arg1, arg2) => arg2 // Resolver
(arg1, arg2) => arg2 // keySelector
);
}

describe('createCachedSelector', () => {
describe('"recomputations" property and cached selectors', () => {
describe('Resolver returns the same value', () => {
describe('keySelector returns the same value', () => {
it('Should create and use the same cached selector', () => {
const cachedSelector = selectorWithMockedResultFunc();
const firstCall = cachedSelector('foo', 'bar');
const secondCallWithSameResolver = cachedSelector('foo', 'bar');
const secondCallWithSamekeySelector = cachedSelector('foo', 'bar');

expect(createSelectorSpy).toHaveBeenCalledTimes(1);
expect(cachedSelector.recomputations()).toBe(1);
});
});

describe('Resolver returns 2 different values', () => {
describe('keySelector returns 2 different values', () => {
it('Should create 2 selectors only and produce 2 recomputations', () => {
const cachedSelector = selectorWithMockedResultFunc();
const firstCallResult = cachedSelector('foo', 'bar');
Expand Down
8 changes: 4 additions & 4 deletions src/index.d.ts
Expand Up @@ -7,8 +7,8 @@ export type ParametricSelector<S, P, R> = (
...args: any[]
) => R;

export type Resolver<S> = (state: S, ...args: any[]) => any;
export type ParametricResolver<S, P> = (
export type KeySelector<S> = (state: S, ...args: any[]) => any;
export type ParametricKeySelector<S, P> = (
state: S,
props: P,
...args: any[]
Expand Down Expand Up @@ -45,7 +45,7 @@ type Options =
| CreateSelectorInstance;

export type OutputCachedSelector<S, R, C, D> = (
resolver: Resolver<S>,
keySelector: KeySelector<S>,
optionsOrSelectorCreator?: Options
) => OutputSelector<S, R, C, D> & {
getMatchingSelector: (state: S, ...args: any[]) => OutputSelector<S, R, C, D>;
Expand All @@ -55,7 +55,7 @@ export type OutputCachedSelector<S, R, C, D> = (
};

export type OutputParametricCachedSelector<S, P, R, C, D> = (
resolver: ParametricResolver<S, P>,
keySelector: ParametricKeySelector<S, P>,
optionsOrSelectorCreator?: Options
) => OutputParametricSelector<S, P, R, C, D> & {
getMatchingSelector: (
Expand Down
10 changes: 5 additions & 5 deletions src/index.js
Expand Up @@ -5,7 +5,7 @@ const defaultCacheCreator = FlatObjectCache;
const defaultCacheKeyValidator = () => true;

function createCachedSelector(...funcs) {
return (resolver, options = {}) => {
return (keySelector, options = {}) => {
// @NOTE Versions 0.x/1.x accepted "options" as a function
if (typeof options === 'function') {
throw new Error(
Expand All @@ -30,7 +30,7 @@ function createCachedSelector(...funcs) {

// Application receives this function
const selector = function(...args) {
const cacheKey = resolver(...args);
const cacheKey = keySelector(...args);

if (isValidCacheKey(cacheKey)) {
let cacheResponse = cache.get(cacheKey);
Expand All @@ -43,20 +43,20 @@ function createCachedSelector(...funcs) {
return cacheResponse(...args);
}
console.warn(
`[re-reselect] Invalid cache key "${cacheKey}" has been returned by resolver function.`
`[re-reselect] Invalid cache key "${cacheKey}" has been returned by keySelector function.`
);
return undefined;
};

// Further selector methods
selector.getMatchingSelector = (...args) => {
const cacheKey = resolver(...args);
const cacheKey = keySelector(...args);
// @NOTE It might update cache hit count in LRU-like caches
return cache.get(cacheKey);
};

selector.removeMatchingSelector = (...args) => {
const cacheKey = resolver(...args);
const cacheKey = keySelector(...args);
cache.remove(cacheKey);
};

Expand Down

0 comments on commit 80c516e

Please sign in to comment.