re-reselect
Advanced tools
Comparing version 0.1.0 to 0.2.0
@@ -0,1 +1,4 @@ | ||
# 0.2.0 | ||
- Expose `getMatchingSelector` method to retrieve the instance of cached selectors | ||
# 0.1.0 | ||
@@ -2,0 +5,0 @@ - Allow resolver function to return keys of type number |
@@ -12,3 +12,4 @@ import { createSelector } from 'reselect'; | ||
var createSelectorInstance = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : createSelector; | ||
return function () { | ||
var selector = function selector() { | ||
// Application receives this function | ||
@@ -25,3 +26,10 @@ var cacheKey = resolver.apply(undefined, arguments); | ||
}; | ||
selector.getMatchingSelector = function () { | ||
var cacheKey = resolver.apply(undefined, arguments); | ||
return cache[cacheKey]; | ||
}; | ||
return selector; | ||
}; | ||
} |
@@ -17,3 +17,4 @@ 'use strict'; | ||
var createSelectorInstance = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _reselect.createSelector; | ||
return function () { | ||
var selector = function selector() { | ||
// Application receives this function | ||
@@ -30,3 +31,10 @@ var cacheKey = resolver.apply(undefined, arguments); | ||
}; | ||
selector.getMatchingSelector = function () { | ||
var cacheKey = resolver.apply(undefined, arguments); | ||
return cache[cacheKey]; | ||
}; | ||
return selector; | ||
}; | ||
} |
{ | ||
"name": "re-reselect", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Memoize selectors and avoid recalculation between calls with different inputs", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
# Re-reselect [![Build Status][ci-img]][ci] | ||
Enhance **[Reselect](reselect) selectors** by wrapping `createSelector` function and returning a memoized **collection of selectors** indexed with the **cache key** returned by a custom **resolver function**. | ||
Improve **[Reselect][reselect] performance** on few edge cases, by initializing selectors on the fly, using a **memoized factory**. | ||
Useful to **reduce selectors recalculation** when the same selector is repeatedly **called with one/few different arguments**. | ||
The resulting selector acts like a normal one, but It's able to determine when **querying a new selector instance or a cached one** on the fly, depending on the supplied arguments. | ||
Useful to **reduce selectors recalculation** when: | ||
- the same selector is repeatedly **called with one/few different arguments** | ||
- the same selector is **imported by different modules** at the same time | ||
[reselect]: https://github.com/reactjs/reselect | ||
@@ -106,4 +110,7 @@ [ci-img]: https://travis-ci.org/toomuchdesign/re-reselect.svg | ||
### How to wrap my existing selector with re-reselect? | ||
## FAQ | ||
### Q: How do I wrap my existing selector with re-reselect? | ||
A: Given your `reselect` selectors: | ||
```js | ||
@@ -120,3 +127,3 @@ import { createSelector } from 'reselect'; | ||
Becomes: | ||
...it becomes: | ||
```js | ||
@@ -136,4 +143,43 @@ import createCachedSelector from 're-reselect'; | ||
### Q: How do I test a re-reselect selector? | ||
Just like a normal reselect selector! Read more [here](https://github.com/reactjs/reselect#q-how-do-i-test-a-selector). | ||
Each **re-reselect** cached selector exposes a `getMatchingSelector` method which returns the **underlying matching selector** instance for the given arguments, **instead of the result**. | ||
`getMatchingSelector` expects the same arguments as a normal selector call **BUT returns the instance of the cached selector itself**. | ||
Once you get a selector instance you can call [its public methods](https://github.com/reactjs/reselect/blob/v3.0.0/src/index.js#L81) like: | ||
- `resultFunc` | ||
- `recomputations` | ||
- `resetRecomputations` | ||
```js | ||
import createCachedSelector from 're-reselect'; | ||
export const getMyData = createCachedSelector( | ||
selectorA, | ||
selectorB, | ||
(A, B) => doSomethingWith(A, B), | ||
)( | ||
(state, arg1) => arg1, // Use arg2 as cache key | ||
); | ||
// ... | ||
// Call the selector to retrieve data | ||
const myFooData = getMyData(state, 'foo'); | ||
const myBarData = getMyData(state, 'bar'); | ||
// Call getMatchingSelector to retrieve the selectors | ||
// which generated "myFooData" and "myBarData" results | ||
const myFooDataSelector = getMyData.getMatchingSelector(state, 'foo'); | ||
const myBarDataSelector = getMyData.getMatchingSelector(state, 'bar'); | ||
// Call reselect's selectors methods | ||
myFooDataSelector.recomputations(); | ||
myFooDataSelector.resetRecomputations(); | ||
``` | ||
## API | ||
**Re-reselect** consists in just one method exported as default. | ||
@@ -152,3 +198,3 @@ | ||
`resolverFunction` is a function which receives the same arguments of your selectors (and `inputSelectors`) and *must return a string or number*. The result is used as cache key to store/retrieve selector instances. | ||
`resolverFunction` is a function which receives the same arguments of your selectors (and `inputSelectors`) and *must return a **string** or **number***. The result is used as cache key to store/retrieve selector instances. | ||
@@ -161,4 +207,13 @@ Cache keys of type `number` are treated like strings, since they are assigned to a JS object as arguments. | ||
#### Returns | ||
(Function): a `reReselectInstance` ready to be called to retrieve data from your store. | ||
### reReselectInstance(selectorArguments) | ||
Retrieve data for given arguments. | ||
### reReselectInstance.getMatchingSelector(selectorArguments) | ||
Retrieve the selector object being called and cached for given arguments. | ||
## Todo's | ||
- Consider to expose a cache clearing method | ||
- Named exports? |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12800
53
215