Socket
Socket
Sign inDemoInstall

re-reselect

Package Overview
Dependencies
1
Maintainers
1
Versions
25
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 3.3.0 to 3.4.0

6

CHANGELOG.md
# Change log
## 3.4.0
### New Features
- Introduce single argument signature
## 3.3.0

@@ -4,0 +10,0 @@

54

dist/index.js
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('reselect')) :
typeof define === 'function' && define.amd ? define(['exports', 'reselect'], factory) :
(factory((global['Re-reselect'] = {}),global.Reselect));
}(this, (function (exports,reselect) { 'use strict';
(global = global || self, factory(global['Re-reselect'] = {}, global.Reselect));
}(this, function (exports, reselect) { 'use strict';

@@ -54,10 +54,16 @@ function isStringOrNumber(value) {

return function (keySelector, options) {
if (options === void 0) {
options = {};
return function (polymorphicOptions, legacyOptions) {
// @NOTE Versions 0.x/1.x accepted "options" as a function
if (typeof legacyOptions === 'function') {
throw new Error('[re-reselect] Second argument "options" must be an object. Please use "options.selectorCreator" to provide a custom selectorCreator.');
}
// @NOTE Versions 0.x/1.x accepted "options" as a function
if (typeof options === 'function') {
throw new Error('[re-reselect] Second argument "options" must be an object. Please use "options.selectorCreator" to provide a custom selectorCreator.');
var options = {};
if (typeof polymorphicOptions === 'function') {
Object.assign(options, legacyOptions, {
keySelector: polymorphicOptions
}); // @TODO add legacyOptions deprecation notice in next major release
} else {
Object.assign(options, polymorphicOptions);
} // https://github.com/reduxjs/reselect/blob/v4.0.0/src/index.js#L54

@@ -81,6 +87,6 @@

if (options.keySelectorCreator) {
keySelector = options.keySelectorCreator({
options.keySelector = options.keySelectorCreator({
keySelector: options.keySelector,
inputSelectors: dependencies,
resultFunc: resultFunc,
keySelector: keySelector
resultFunc: resultFunc
});

@@ -91,3 +97,3 @@ } // Application receives this function

var selector = function selector() {
var cacheKey = keySelector.apply(void 0, arguments);
var cacheKey = options.keySelector.apply(options, arguments);

@@ -111,3 +117,3 @@ if (isValidCacheKey(cacheKey)) {

selector.getMatchingSelector = function () {
var cacheKey = keySelector.apply(void 0, arguments); // @NOTE It might update cache hit count in LRU-like caches
var cacheKey = options.keySelector.apply(options, arguments); // @NOTE It might update cache hit count in LRU-like caches

@@ -118,3 +124,3 @@ return cache.get(cacheKey);

selector.removeMatchingSelector = function () {
var cacheKey = keySelector.apply(void 0, arguments);
var cacheKey = options.keySelector.apply(options, arguments);
cache.remove(cacheKey);

@@ -139,3 +145,3 @@ };

selector.keySelector = keySelector;
selector.keySelector = options.keySelector;
return selector;

@@ -390,17 +396,17 @@ };

exports.default = createCachedSelector;
exports.createStructuredCachedSelector = createStructuredCachedSelector;
exports.FlatObjectCache = FlatObjectCache;
exports.FifoCacheObject = FifoObjectCache;
exports.FifoMapCache = FifoMapCache;
exports.FifoObjectCache = FifoObjectCache;
exports.LruObjectCache = LruObjectCache;
exports.FlatCacheObject = FlatObjectCache;
exports.FlatMapCache = FlatMapCache;
exports.FifoMapCache = FifoMapCache;
exports.FlatObjectCache = FlatObjectCache;
exports.LruCacheObject = LruMapCache;
exports.LruMapCache = LruMapCache;
exports.FlatCacheObject = FlatObjectCache;
exports.FifoCacheObject = FifoObjectCache;
exports.LruCacheObject = LruMapCache;
exports.LruObjectCache = LruObjectCache;
exports.createStructuredCachedSelector = createStructuredCachedSelector;
exports.default = createCachedSelector;
Object.defineProperty(exports, '__esModule', { value: true });
})));
}));
//# sourceMappingURL=index.js.map

@@ -50,10 +50,16 @@ import { createSelector, createStructuredSelector } from 'reselect';

return function (keySelector, options) {
if (options === void 0) {
options = {};
return function (polymorphicOptions, legacyOptions) {
// @NOTE Versions 0.x/1.x accepted "options" as a function
if (typeof legacyOptions === 'function') {
throw new Error('[re-reselect] Second argument "options" must be an object. Please use "options.selectorCreator" to provide a custom selectorCreator.');
}
// @NOTE Versions 0.x/1.x accepted "options" as a function
if (typeof options === 'function') {
throw new Error('[re-reselect] Second argument "options" must be an object. Please use "options.selectorCreator" to provide a custom selectorCreator.');
var options = {};
if (typeof polymorphicOptions === 'function') {
Object.assign(options, legacyOptions, {
keySelector: polymorphicOptions
}); // @TODO add legacyOptions deprecation notice in next major release
} else {
Object.assign(options, polymorphicOptions);
} // https://github.com/reduxjs/reselect/blob/v4.0.0/src/index.js#L54

@@ -77,6 +83,6 @@

if (options.keySelectorCreator) {
keySelector = options.keySelectorCreator({
options.keySelector = options.keySelectorCreator({
keySelector: options.keySelector,
inputSelectors: dependencies,
resultFunc: resultFunc,
keySelector: keySelector
resultFunc: resultFunc
});

@@ -87,3 +93,3 @@ } // Application receives this function

var selector = function selector() {
var cacheKey = keySelector.apply(void 0, arguments);
var cacheKey = options.keySelector.apply(options, arguments);

@@ -107,3 +113,3 @@ if (isValidCacheKey(cacheKey)) {

selector.getMatchingSelector = function () {
var cacheKey = keySelector.apply(void 0, arguments); // @NOTE It might update cache hit count in LRU-like caches
var cacheKey = options.keySelector.apply(options, arguments); // @NOTE It might update cache hit count in LRU-like caches

@@ -114,3 +120,3 @@ return cache.get(cacheKey);

selector.removeMatchingSelector = function () {
var cacheKey = keySelector.apply(void 0, arguments);
var cacheKey = options.keySelector.apply(options, arguments);
cache.remove(cacheKey);

@@ -135,3 +141,3 @@ };

selector.keySelector = keySelector;
selector.keySelector = options.keySelector;
return selector;

@@ -387,3 +393,3 @@ };

export default createCachedSelector;
export { createStructuredCachedSelector, FlatObjectCache, FifoObjectCache, LruObjectCache, FlatMapCache, FifoMapCache, LruMapCache, FlatObjectCache as FlatCacheObject, FifoObjectCache as FifoCacheObject, LruMapCache as LruCacheObject };
export { FifoObjectCache as FifoCacheObject, FifoMapCache, FifoObjectCache, FlatObjectCache as FlatCacheObject, FlatMapCache, FlatObjectCache, LruMapCache as LruCacheObject, LruMapCache, LruObjectCache, createStructuredCachedSelector };
//# sourceMappingURL=index.js.map

@@ -54,10 +54,16 @@ 'use strict';

return function (keySelector, options) {
if (options === void 0) {
options = {};
return function (polymorphicOptions, legacyOptions) {
// @NOTE Versions 0.x/1.x accepted "options" as a function
if (typeof legacyOptions === 'function') {
throw new Error('[re-reselect] Second argument "options" must be an object. Please use "options.selectorCreator" to provide a custom selectorCreator.');
}
// @NOTE Versions 0.x/1.x accepted "options" as a function
if (typeof options === 'function') {
throw new Error('[re-reselect] Second argument "options" must be an object. Please use "options.selectorCreator" to provide a custom selectorCreator.');
var options = {};
if (typeof polymorphicOptions === 'function') {
Object.assign(options, legacyOptions, {
keySelector: polymorphicOptions
}); // @TODO add legacyOptions deprecation notice in next major release
} else {
Object.assign(options, polymorphicOptions);
} // https://github.com/reduxjs/reselect/blob/v4.0.0/src/index.js#L54

@@ -81,6 +87,6 @@

if (options.keySelectorCreator) {
keySelector = options.keySelectorCreator({
options.keySelector = options.keySelectorCreator({
keySelector: options.keySelector,
inputSelectors: dependencies,
resultFunc: resultFunc,
keySelector: keySelector
resultFunc: resultFunc
});

@@ -91,3 +97,3 @@ } // Application receives this function

var selector = function selector() {
var cacheKey = keySelector.apply(void 0, arguments);
var cacheKey = options.keySelector.apply(options, arguments);

@@ -111,3 +117,3 @@ if (isValidCacheKey(cacheKey)) {

selector.getMatchingSelector = function () {
var cacheKey = keySelector.apply(void 0, arguments); // @NOTE It might update cache hit count in LRU-like caches
var cacheKey = options.keySelector.apply(options, arguments); // @NOTE It might update cache hit count in LRU-like caches

@@ -118,3 +124,3 @@ return cache.get(cacheKey);

selector.removeMatchingSelector = function () {
var cacheKey = keySelector.apply(void 0, arguments);
var cacheKey = options.keySelector.apply(options, arguments);
cache.remove(cacheKey);

@@ -139,3 +145,3 @@ };

selector.keySelector = keySelector;
selector.keySelector = options.keySelector;
return selector;

@@ -390,13 +396,13 @@ };

exports.default = createCachedSelector;
exports.createStructuredCachedSelector = createStructuredCachedSelector;
exports.FlatObjectCache = FlatObjectCache;
exports.FifoCacheObject = FifoObjectCache;
exports.FifoMapCache = FifoMapCache;
exports.FifoObjectCache = FifoObjectCache;
exports.LruObjectCache = LruObjectCache;
exports.FlatCacheObject = FlatObjectCache;
exports.FlatMapCache = FlatMapCache;
exports.FifoMapCache = FifoMapCache;
exports.FlatObjectCache = FlatObjectCache;
exports.LruCacheObject = LruMapCache;
exports.LruMapCache = LruMapCache;
exports.FlatCacheObject = FlatObjectCache;
exports.FifoCacheObject = FifoObjectCache;
exports.LruCacheObject = LruMapCache;
exports.LruObjectCache = LruObjectCache;
exports.createStructuredCachedSelector = createStructuredCachedSelector;
exports.default = createCachedSelector;
//# sourceMappingURL=index.js.map
{
"name": "re-reselect",
"version": "3.3.0",
"version": "3.4.0",
"description": "Enhance Reselect selectors with deeper memoization and cache management",

@@ -19,5 +19,7 @@ "main": "lib/index.js",

"scripts": {
"test": "npm run test:typescript && jest",
"test": "npm run test:typescript && jest ./src",
"test:typescript": "typings-tester --config typescript_test/tsconfig.json --dir typescript_test",
"test:bundles": "jest --config ./jest/es.config.js && jest --config ./jest/lib.config.js && jest --config ./jest/dist.config.js",
"test:bundles": "npm run test:bundles:snapshot && npm run test:bundles:unit",
"test:bundles:unit": "jest ./src --config ./jest/es.config.js && jest ./src --config ./jest/lib.config.js && jest ./src --config ./jest/dist.config.js",
"test:bundles:snapshot": "jest ./jest/bundles-snapshot.test.js",
"clean": "rimraf dist es lib",

@@ -60,9 +62,9 @@ "compile": "npm run clean && rollup -c",

"coveralls": "^3.0.4",
"husky": "^1.1.2",
"husky": "^3.0.0",
"jest": "^24.8.0",
"lint-staged": "^7.3.0",
"lint-staged": "^9.0.0",
"prettier": "^1.16.2",
"reselect": "^4.0.0",
"rimraf": "^2.6.1",
"rollup": "^0.66.6",
"rollup": "^1.16.4",
"rollup-plugin-babel": "^4.0.3",

@@ -69,0 +71,0 @@ "typescript": "^3.2.2",

@@ -31,29 +31,27 @@ # Re-reselect

// Normal reselect routine: declare "inputSelectors" and "resultFunc"
const selectorA = state => state.a;
const selectorB = (state, itemName) => state.items[itemName];
const getUsers = state => state.users;
const getLibraryId = (state, libName) => state.libraries[libName].id;
const cachedSelector = createCachedSelector(
const getUsersByLibrary = createCachedSelector(
// inputSelectors
selectorA,
selectorB,
getUsers,
getLibraryId,
// resultFunc
(A, B) => expensiveComputation(A, B)
(users, libraryId) => expensiveComputation(users, libraryId),
)(
// keySelector
// Instruct re-reselect to use "itemName" as cacheKey
(state, itemName) => itemName
// re-reselect keySelector
// Use "libraryId" as cacheKey
(_, libraryId) => libraryId
);
// Use the cached selector like a normal selector:
const fooResult = cachedSelector(state, 'foo');
const barResult = cachedSelector(state, 'bar');
// Cached selector behave like normal selectors:
// 2 reselect selectors are created, called and cached
const reactUsers = getUsersByLibrary(state, 'react');
const vueUsers = getUsersByLibrary(state, 'vue');
// 2 reselect selectors were created, called and cached behind the scenes
const fooResultAgain = cachedSelector(state, 'foo');
// fooResult === fooResultAgain
// Cache was not invalidated by calling "cachedSelector(state, 'bar')"
// "expensiveComputation" totally called twice
// This 3rd call hits the cache
const reactUsersAgain = getUsersByLibrary(state, 'react');
// reactUsers === reactUsersAgain
// "expensiveComputation" called twice in total
```

@@ -88,30 +86,26 @@

I found myself wrapping a library of data elaboration (quite heavy stuff) with reselect selectors (`getPieceOfData` in the example).
Let's say `getData` is a `reselect` selector.
On each store update, I had to repeatedly call the selector in order to retrieve all the pieces of data needed by my UI. Like this:
```js
getPieceOfData(state, itemId, 'dataA');
getPieceOfData(state, itemId, 'dataB');
getPieceOfData(state, itemId, 'dataC');
getData(state, itemId, 'dataA');
getData(state, itemId, 'dataB');
getData(state, itemId, 'dataC');
```
What happens, here? `getPieceOfData` **selector cache is invalidated** on each call because of the different 3rd `'dataX'` argument.
The **3rd argument invalidates `reselect` cache** on each call, forcing `getData` to re-evaluate and return a new value.
### re-reselect solution
`re-reselect` selectors keep a **cache of `reselect` selectors** and store/retrieve them by `cacheKey`.
`re-reselect` selectors keep a **cache of `reselect` selectors** stored by `cacheKey`.
<!-- Please note that part of this lines are repeated in #api chapter -->
`cacheKey` is by default a `string` or `number` but can be anything depending on the chosen cache strategy (see [cache objects docs][cache-objects-docs]).
`cacheKey` is the return value of the `keySelector` function. It's by default a `string` or `number` but it can be anything depending on the chosen cache strategy (see [cache objects docs][cache-objects-docs]).
`cacheKey` is the output of `keySelector`, declared at selector initialization.
`keySelector` 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`.
- returns a `cacheKey`
Note that the **same `reselect` selector instance** stored in cache will be used for computing data for the **same `cacheKey`** (1:1).
A **unique persisting `reselect` selector instance** stored in cache is used to compute data for a given `cacheKey` (1:1).

@@ -124,3 +118,3 @@ Back to the example, `re-reselect` retrieves data by **querying one of the cached selectors** using the 3rd argument as `cacheKey`, allowing cache invalidation only when `state` or `itemId` change (but not `dataType`):

state => state,
(state, itemId) => itemId,
(_state_, itemId) => itemId,
(state, itemId, dataType) => dataType,

@@ -133,5 +127,5 @@ (state, itemId, dataType) => expensiveComputation(state, itemId, dataType)

`createCachedSelector` returns a selector with the **same signature as a normal `reselect` selector**.
**Replacing a selector with a cached selector is invisible to the consuming application since the API is the same.**
But now, **each time the selector is called**, the following happens behind the scenes:
**When a cached selector is called**, the following happens behind the scenes:

@@ -143,4 +137,2 @@ 1. **Evaluate the `cacheKey`** for current call by executing `keySelector`

**re-reselect** stays completely optional and consumes **your installed reselect** module (`reselect` is declared as **peer dependency**).
### Other viable solutions

@@ -162,3 +154,3 @@

This is what `re-reselect` actually does! :-) It's quite verbose (since has to be repeated for each selector), **that's why re-reselect is here**.
This is what `re-reselect` actually does. 😀

@@ -170,2 +162,3 @@ ## Examples

- [Cache API calls][example-3]
- [Programmatic keySelector composition][example-4]

@@ -339,12 +332,8 @@ ## FAQ

)(
keySelector,
{ options }
keySelector | { options }
)
```
Takes the same arguments as reselect's [`createSelector`][reselect-create-selector] and returns a new function which accepts **2 arguments**:
Takes the same arguments as reselect's [`createSelector`][reselect-create-selector] and returns a new function which accepts a [`keySelector`](#keyselector) or an [`options`](#options) object.
- `keySelector`
- `{ options }` _(optional)_
**Returns** a [selector instance][selector-instance-docs].

@@ -361,12 +350,8 @@

)(
keySelector,
{ options }
keySelector | { options }
)
```
Takes the same arguments as reselect's [`createStructuredSelector`][reselect-create-structured-selector] and returns a new function which accepts **2 arguments**:
Takes the same arguments as reselect's [`createStructuredSelector`][reselect-create-structured-selector] and returns a new function which accepts a [`keySelector`](#keyselector) or an [`options`](#options) object.
- `keySelector`
- `{ options }` _(optional)_
**Returns** a [selector instance][selector-instance-docs].

@@ -384,2 +369,9 @@

#### keySelector
Type: `function`<br />
Default: `undefined`
The [`keySelector`](#keyselector) used by the cached selector.
#### cacheObject

@@ -392,9 +384,2 @@

#### selectorCreator
Type: `function`<br />
Default: `reselect`'s [`createSelector`][reselect-create-selector]
An optional function describing a [custom version of createSelector][reselect-create-selector-creator].
#### keySelectorCreator

@@ -408,3 +393,3 @@

```typescript
export type keySelectorCreator = (selectorInputs: {
type keySelectorCreator = (selectorInputs: {
inputSelectors: InputSelector[];

@@ -416,4 +401,13 @@ resultFunc: ResultFunc;

This allows to dynamically **generate `keySelectors` on runtime** based on provided `inputSelectors`/`resultFunc` supporting [**key selectors composition**](https://github.com/toomuchdesign/re-reselect/pull/73).
This allows to dynamically **generate `keySelectors` on runtime** based on provided `inputSelectors`/`resultFunc` supporting [**key selectors composition**](https://github.com/toomuchdesign/re-reselect/pull/73). It overrides any provided `keySelector`.
See [programmatic keySelector composition][example-4] example.
#### selectorCreator
Type: `function`<br />
Default: `reselect`'s [`createSelector`][reselect-create-selector]
An optional function describing a [custom version of createSelector][reselect-create-selector-creator].
### re-reselect selector instance

@@ -507,4 +501,5 @@

[example-3]: examples/3-cache-api-calls.md
[example-4]: examples/4-programmatic-keyselector-composition.md
[selector-instance-docs]: #re-reselect-selector-instance
[cache-objects-docs]: src/cache#readme
[docs-all-contributors]: https://allcontributors.org/docs/en/emoji-key

@@ -24,10 +24,8 @@ # Re-reselect cache objects

// ...
)(
)({
keySelector,
{
cacheObject: new LruObjectCache({cacheSize: 5}),
// or:
// cacheObject: new LruMapCache({cacheSize: 5}),
}
);
cacheObject: new LruObjectCache({cacheSize: 5}),
// or:
// cacheObject: new LruMapCache({cacheSize: 5}),
});
```

@@ -34,0 +32,0 @@

@@ -8,5 +8,5 @@ import {createSelector} from 'reselect';

function createCachedSelector(...funcs) {
return (keySelector, options = {}) => {
return (polymorphicOptions, legacyOptions) => {
// @NOTE Versions 0.x/1.x accepted "options" as a function
if (typeof options === 'function') {
if (typeof legacyOptions === 'function') {
throw new Error(

@@ -17,2 +17,10 @@ '[re-reselect] Second argument "options" must be an object. Please use "options.selectorCreator" to provide a custom selectorCreator.'

const options = {};
if (typeof polymorphicOptions === 'function') {
Object.assign(options, legacyOptions, {keySelector: polymorphicOptions});
// @TODO add legacyOptions deprecation notice in next major release
} else {
Object.assign(options, polymorphicOptions);
}
// https://github.com/reduxjs/reselect/blob/v4.0.0/src/index.js#L54

@@ -34,6 +42,6 @@ let recomputations = 0;

if (options.keySelectorCreator) {
keySelector = options.keySelectorCreator({
options.keySelector = options.keySelectorCreator({
keySelector: options.keySelector,
inputSelectors: dependencies,
resultFunc,
keySelector,
});

@@ -44,3 +52,3 @@ }

const selector = function(...args) {
const cacheKey = keySelector(...args);
const cacheKey = options.keySelector(...args);

@@ -65,3 +73,3 @@ if (isValidCacheKey(cacheKey)) {

selector.getMatchingSelector = (...args) => {
const cacheKey = keySelector(...args);
const cacheKey = options.keySelector(...args);
// @NOTE It might update cache hit count in LRU-like caches

@@ -72,3 +80,3 @@ return cache.get(cacheKey);

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

@@ -91,3 +99,3 @@ };

selector.keySelector = keySelector;
selector.keySelector = options.keySelector;

@@ -94,0 +102,0 @@ return selector;

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc