re-reselect
Advanced tools
Comparing version 0.6.3 to 1.0.0
@@ -1,30 +0,42 @@ | ||
# 0.6.3 | ||
# Change log | ||
## 1.0.0 | ||
### Breaking Changes | ||
- `selectorCreator` argument is deprecated in favor of an option object | ||
### New Features | ||
- Accept an option object to provide `cacheObject` and `selectorCreator` options | ||
- Extract caching logic into pluggable/customizable cache objects | ||
- Introduce `Rollup.js` as bundler | ||
- Make compilation cross-platform | ||
## 0.6.3 | ||
- Add TS type definition for optional `selectorCreator` | ||
# 0.6.2 | ||
## 0.6.2 | ||
- Fix wrong UMD reselect global import | ||
# 0.6.1 | ||
## 0.6.1 | ||
- Fix TypeScript `OutputCachedSelector` and `OutputParametricCachedSelector` type definitions | ||
# 0.6.0 | ||
## 0.6.0 | ||
- Add TypeScript type definitions | ||
# 0.5.0 | ||
## 0.5.0 | ||
- Expose `resultFunc` attribute | ||
# 0.4.0 | ||
## 0.4.0 | ||
- Expose `removeMatchingSelector` method | ||
- Expose `clearCache` method | ||
# 0.3.0 | ||
## 0.3.0 | ||
- Add UMD dist | ||
# 0.2.0 | ||
## 0.2.0 | ||
- Expose `getMatchingSelector` method to retrieve the instance of cached selectors | ||
# 0.1.0 | ||
## 0.1.0 | ||
- Allow resolver function to return keys of type number | ||
# 0.0.0 | ||
## 0.0.0 | ||
- Initial release |
(function (global, factory) { | ||
if (typeof define === "function" && define.amd) { | ||
define('Re-reselect', ['exports', 'reselect'], factory); | ||
} else if (typeof exports !== "undefined") { | ||
factory(exports, require('reselect')); | ||
} else { | ||
var mod = { | ||
exports: {} | ||
}; | ||
factory(mod.exports, global.Reselect); | ||
global.ReReselect = mod.exports; | ||
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'; | ||
var classCallCheck = function (instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a function"); | ||
} | ||
})(this, function (exports, _reselect) { | ||
'use strict'; | ||
}; | ||
exports.__esModule = true; | ||
exports.default = createCachedSelector; | ||
function createCachedSelector() { | ||
for (var _len = arguments.length, funcs = Array(_len), _key = 0; _key < _len; _key++) { | ||
funcs[_key] = arguments[_key]; | ||
var FlatCacheObject = function () { | ||
function FlatCacheObject() { | ||
classCallCheck(this, FlatCacheObject); | ||
this._cache = {}; | ||
} | ||
FlatCacheObject.prototype.set = function set$$1(key, selectorFn) { | ||
this._cache[key] = selectorFn; | ||
}; | ||
FlatCacheObject.prototype.get = function get$$1(key) { | ||
return this._cache[key]; | ||
}; | ||
FlatCacheObject.prototype.remove = function remove(key) { | ||
delete this._cache[key]; | ||
}; | ||
FlatCacheObject.prototype.clear = function clear() { | ||
this._cache = {}; | ||
}; | ||
return FlatCacheObject; | ||
}(); | ||
var FifoCacheObject = function () { | ||
function FifoCacheObject() { | ||
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, | ||
cacheSize = _ref.cacheSize; | ||
classCallCheck(this, FifoCacheObject); | ||
if (cacheSize === undefined) { | ||
throw new Error('Missing the required property `cacheSize`.'); | ||
} | ||
if (!Number.isInteger(cacheSize) || cacheSize <= 0) { | ||
throw new Error('The `cacheSize` property must be a positive integer value.'); | ||
} | ||
this._cache = {}; | ||
this._cacheOrdering = []; | ||
this._cacheSize = cacheSize; | ||
} | ||
var cache = {}; | ||
FifoCacheObject.prototype.set = function set$$1(key, selectorFn) { | ||
this._cache[key] = selectorFn; | ||
this._cacheOrdering.push(key); | ||
return function (resolver) { | ||
var createSelectorInstance = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _reselect.createSelector; | ||
if (this._cacheOrdering.length > this._cacheSize) { | ||
var earliest = this._cacheOrdering[0]; | ||
this.remove(earliest); | ||
} | ||
}; | ||
var selector = function selector() { | ||
// Application receives this function | ||
var cacheKey = resolver.apply(undefined, arguments); | ||
FifoCacheObject.prototype.get = function get$$1(key) { | ||
return this._cache[key]; | ||
}; | ||
if (typeof cacheKey === 'string' || typeof cacheKey === 'number') { | ||
var _cache; | ||
FifoCacheObject.prototype.remove = function remove(key) { | ||
var index = this._cacheOrdering.indexOf(key); | ||
if (cache[cacheKey] === undefined) { | ||
cache[cacheKey] = createSelectorInstance.apply(undefined, funcs); | ||
} | ||
return (_cache = cache)[cacheKey].apply(_cache, arguments); | ||
} | ||
return undefined; | ||
}; | ||
if (index > -1) { | ||
this._cacheOrdering.splice(index, 1); | ||
} | ||
delete this._cache[key]; | ||
}; | ||
// Further selector methods | ||
selector.getMatchingSelector = function () { | ||
var cacheKey = resolver.apply(undefined, arguments); | ||
return cache[cacheKey]; | ||
}; | ||
FifoCacheObject.prototype.clear = function clear() { | ||
this._cache = {}; | ||
this._cacheOrdering = []; | ||
}; | ||
selector.removeMatchingSelector = function () { | ||
var cacheKey = resolver.apply(undefined, arguments); | ||
if (cache[cacheKey] !== undefined) { | ||
cache[cacheKey] = undefined; | ||
return FifoCacheObject; | ||
}(); | ||
var LruCacheObject = function () { | ||
function LruCacheObject() { | ||
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, | ||
cacheSize = _ref.cacheSize; | ||
classCallCheck(this, LruCacheObject); | ||
if (cacheSize === undefined) { | ||
throw new Error('Missing the required property `cacheSize`.'); | ||
} | ||
if (!Number.isInteger(cacheSize) || cacheSize <= 0) { | ||
throw new Error('The `cacheSize` property must be a positive integer value.'); | ||
} | ||
this._cache = {}; | ||
this._cacheOrdering = []; | ||
this._cacheSize = cacheSize; | ||
} | ||
LruCacheObject.prototype.set = function set$$1(key, selectorFn) { | ||
this._cache[key] = selectorFn; | ||
this._registerCacheHit(key); | ||
if (this._cacheOrdering.length > this._cacheSize) { | ||
var earliest = this._cacheOrdering[0]; | ||
this.remove(earliest); | ||
} | ||
}; | ||
LruCacheObject.prototype.get = function get$$1(key) { | ||
this._registerCacheHit(key); | ||
return this._cache[key]; | ||
}; | ||
LruCacheObject.prototype.remove = function remove(key) { | ||
this._deleteCacheHit(key); | ||
delete this._cache[key]; | ||
}; | ||
LruCacheObject.prototype.clear = function clear() { | ||
this._cache = {}; | ||
this._cacheOrdering = []; | ||
}; | ||
LruCacheObject.prototype._registerCacheHit = function _registerCacheHit(key) { | ||
this._deleteCacheHit(key); | ||
this._cacheOrdering.push(key); | ||
}; | ||
LruCacheObject.prototype._deleteCacheHit = function _deleteCacheHit(key) { | ||
var index = this._cacheOrdering.indexOf(key); | ||
if (index > -1) { | ||
this._cacheOrdering.splice(index, 1); | ||
} | ||
}; | ||
return LruCacheObject; | ||
}(); | ||
function createCachedSelector() { | ||
for (var _len = arguments.length, funcs = Array(_len), _key = 0; _key < _len; _key++) { | ||
funcs[_key] = arguments[_key]; | ||
} | ||
var defaultCacheCreator = FlatCacheObject; | ||
return function (resolver) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var cache = void 0; | ||
var selectorCreator = void 0; | ||
// Allow "options" to be provided as a "selectorCreator" for backward compatibility | ||
// @TODO Remove "options" as a function in next breaking release | ||
if (typeof options === 'function') { | ||
console.warn('[re-reselect] Deprecation warning: "selectorCreator" argument is discouraged and will be removed in the upcoming major release. Please use "options.selectorCreator" instead.'); | ||
cache = new defaultCacheCreator(); | ||
selectorCreator = options; | ||
} else { | ||
cache = options.cacheObject || new defaultCacheCreator(); | ||
selectorCreator = options.selectorCreator || reselect.createSelector; | ||
} | ||
var selector = function selector() { | ||
// Application receives this function | ||
var cacheKey = resolver.apply(undefined, arguments); | ||
if (typeof cacheKey === 'string' || typeof cacheKey === 'number') { | ||
var cacheResponse = cache.get(cacheKey); | ||
if (cacheResponse === undefined) { | ||
cacheResponse = selectorCreator.apply(undefined, funcs); | ||
cache.set(cacheKey, cacheResponse); | ||
} | ||
}; | ||
selector.clearCache = function () { | ||
cache = {}; | ||
}; | ||
return cacheResponse.apply(undefined, arguments); | ||
} | ||
return undefined; | ||
}; | ||
selector.resultFunc = funcs[funcs.length - 1]; | ||
// Further selector methods | ||
selector.getMatchingSelector = function () { | ||
var cacheKey = resolver.apply(undefined, arguments); | ||
// @NOTE It might update cache hit count in LRU-like caches | ||
return cache.get(cacheKey); | ||
}; | ||
return selector; | ||
selector.removeMatchingSelector = function () { | ||
var cacheKey = resolver.apply(undefined, arguments); | ||
cache.remove(cacheKey); | ||
}; | ||
} | ||
}); | ||
selector.clearCache = function () { | ||
cache.clear(); | ||
}; | ||
selector.resultFunc = funcs[funcs.length - 1]; | ||
return selector; | ||
}; | ||
} | ||
exports['default'] = createCachedSelector; | ||
exports.FlatCacheObject = FlatCacheObject; | ||
exports.FifoCacheObject = FifoCacheObject; | ||
exports.LruCacheObject = LruCacheObject; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
}))); | ||
//# sourceMappingURL=index.js.map |
184
es/index.js
import { createSelector } from 'reselect'; | ||
export default function createCachedSelector() { | ||
var classCallCheck = function (instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a function"); | ||
} | ||
}; | ||
var FlatCacheObject = function () { | ||
function FlatCacheObject() { | ||
classCallCheck(this, FlatCacheObject); | ||
this._cache = {}; | ||
} | ||
FlatCacheObject.prototype.set = function set$$1(key, selectorFn) { | ||
this._cache[key] = selectorFn; | ||
}; | ||
FlatCacheObject.prototype.get = function get$$1(key) { | ||
return this._cache[key]; | ||
}; | ||
FlatCacheObject.prototype.remove = function remove(key) { | ||
delete this._cache[key]; | ||
}; | ||
FlatCacheObject.prototype.clear = function clear() { | ||
this._cache = {}; | ||
}; | ||
return FlatCacheObject; | ||
}(); | ||
var FifoCacheObject = function () { | ||
function FifoCacheObject() { | ||
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, | ||
cacheSize = _ref.cacheSize; | ||
classCallCheck(this, FifoCacheObject); | ||
if (cacheSize === undefined) { | ||
throw new Error('Missing the required property `cacheSize`.'); | ||
} | ||
if (!Number.isInteger(cacheSize) || cacheSize <= 0) { | ||
throw new Error('The `cacheSize` property must be a positive integer value.'); | ||
} | ||
this._cache = {}; | ||
this._cacheOrdering = []; | ||
this._cacheSize = cacheSize; | ||
} | ||
FifoCacheObject.prototype.set = function set$$1(key, selectorFn) { | ||
this._cache[key] = selectorFn; | ||
this._cacheOrdering.push(key); | ||
if (this._cacheOrdering.length > this._cacheSize) { | ||
var earliest = this._cacheOrdering[0]; | ||
this.remove(earliest); | ||
} | ||
}; | ||
FifoCacheObject.prototype.get = function get$$1(key) { | ||
return this._cache[key]; | ||
}; | ||
FifoCacheObject.prototype.remove = function remove(key) { | ||
var index = this._cacheOrdering.indexOf(key); | ||
if (index > -1) { | ||
this._cacheOrdering.splice(index, 1); | ||
} | ||
delete this._cache[key]; | ||
}; | ||
FifoCacheObject.prototype.clear = function clear() { | ||
this._cache = {}; | ||
this._cacheOrdering = []; | ||
}; | ||
return FifoCacheObject; | ||
}(); | ||
var LruCacheObject = function () { | ||
function LruCacheObject() { | ||
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, | ||
cacheSize = _ref.cacheSize; | ||
classCallCheck(this, LruCacheObject); | ||
if (cacheSize === undefined) { | ||
throw new Error('Missing the required property `cacheSize`.'); | ||
} | ||
if (!Number.isInteger(cacheSize) || cacheSize <= 0) { | ||
throw new Error('The `cacheSize` property must be a positive integer value.'); | ||
} | ||
this._cache = {}; | ||
this._cacheOrdering = []; | ||
this._cacheSize = cacheSize; | ||
} | ||
LruCacheObject.prototype.set = function set$$1(key, selectorFn) { | ||
this._cache[key] = selectorFn; | ||
this._registerCacheHit(key); | ||
if (this._cacheOrdering.length > this._cacheSize) { | ||
var earliest = this._cacheOrdering[0]; | ||
this.remove(earliest); | ||
} | ||
}; | ||
LruCacheObject.prototype.get = function get$$1(key) { | ||
this._registerCacheHit(key); | ||
return this._cache[key]; | ||
}; | ||
LruCacheObject.prototype.remove = function remove(key) { | ||
this._deleteCacheHit(key); | ||
delete this._cache[key]; | ||
}; | ||
LruCacheObject.prototype.clear = function clear() { | ||
this._cache = {}; | ||
this._cacheOrdering = []; | ||
}; | ||
LruCacheObject.prototype._registerCacheHit = function _registerCacheHit(key) { | ||
this._deleteCacheHit(key); | ||
this._cacheOrdering.push(key); | ||
}; | ||
LruCacheObject.prototype._deleteCacheHit = function _deleteCacheHit(key) { | ||
var index = this._cacheOrdering.indexOf(key); | ||
if (index > -1) { | ||
this._cacheOrdering.splice(index, 1); | ||
} | ||
}; | ||
return LruCacheObject; | ||
}(); | ||
function createCachedSelector() { | ||
for (var _len = arguments.length, funcs = Array(_len), _key = 0; _key < _len; _key++) { | ||
@@ -8,7 +147,21 @@ funcs[_key] = arguments[_key]; | ||
var cache = {}; | ||
var defaultCacheCreator = FlatCacheObject; | ||
return function (resolver) { | ||
var createSelectorInstance = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : createSelector; | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var cache = void 0; | ||
var selectorCreator = void 0; | ||
// Allow "options" to be provided as a "selectorCreator" for backward compatibility | ||
// @TODO Remove "options" as a function in next breaking release | ||
if (typeof options === 'function') { | ||
console.warn('[re-reselect] Deprecation warning: "selectorCreator" argument is discouraged and will be removed in the upcoming major release. Please use "options.selectorCreator" instead.'); | ||
cache = new defaultCacheCreator(); | ||
selectorCreator = options; | ||
} else { | ||
cache = options.cacheObject || new defaultCacheCreator(); | ||
selectorCreator = options.selectorCreator || createSelector; | ||
} | ||
var selector = function selector() { | ||
@@ -19,8 +172,10 @@ // Application receives this function | ||
if (typeof cacheKey === 'string' || typeof cacheKey === 'number') { | ||
var _cache; | ||
var cacheResponse = cache.get(cacheKey); | ||
if (cache[cacheKey] === undefined) { | ||
cache[cacheKey] = createSelectorInstance.apply(undefined, funcs); | ||
if (cacheResponse === undefined) { | ||
cacheResponse = selectorCreator.apply(undefined, funcs); | ||
cache.set(cacheKey, cacheResponse); | ||
} | ||
return (_cache = cache)[cacheKey].apply(_cache, arguments); | ||
return cacheResponse.apply(undefined, arguments); | ||
} | ||
@@ -33,3 +188,4 @@ return undefined; | ||
var cacheKey = resolver.apply(undefined, arguments); | ||
return cache[cacheKey]; | ||
// @NOTE It might update cache hit count in LRU-like caches | ||
return cache.get(cacheKey); | ||
}; | ||
@@ -39,9 +195,7 @@ | ||
var cacheKey = resolver.apply(undefined, arguments); | ||
if (cache[cacheKey] !== undefined) { | ||
cache[cacheKey] = undefined; | ||
} | ||
cache.remove(cacheKey); | ||
}; | ||
selector.clearCache = function () { | ||
cache = {}; | ||
cache.clear(); | ||
}; | ||
@@ -53,2 +207,6 @@ | ||
}; | ||
} | ||
} | ||
export { FlatCacheObject, FifoCacheObject, LruCacheObject }; | ||
export default createCachedSelector; | ||
//# sourceMappingURL=index.js.map |
189
lib/index.js
'use strict'; | ||
exports.__esModule = true; | ||
exports.default = createCachedSelector; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
var _reselect = require('reselect'); | ||
var reselect = require('reselect'); | ||
var classCallCheck = function (instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a function"); | ||
} | ||
}; | ||
var FlatCacheObject = function () { | ||
function FlatCacheObject() { | ||
classCallCheck(this, FlatCacheObject); | ||
this._cache = {}; | ||
} | ||
FlatCacheObject.prototype.set = function set$$1(key, selectorFn) { | ||
this._cache[key] = selectorFn; | ||
}; | ||
FlatCacheObject.prototype.get = function get$$1(key) { | ||
return this._cache[key]; | ||
}; | ||
FlatCacheObject.prototype.remove = function remove(key) { | ||
delete this._cache[key]; | ||
}; | ||
FlatCacheObject.prototype.clear = function clear() { | ||
this._cache = {}; | ||
}; | ||
return FlatCacheObject; | ||
}(); | ||
var FifoCacheObject = function () { | ||
function FifoCacheObject() { | ||
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, | ||
cacheSize = _ref.cacheSize; | ||
classCallCheck(this, FifoCacheObject); | ||
if (cacheSize === undefined) { | ||
throw new Error('Missing the required property `cacheSize`.'); | ||
} | ||
if (!Number.isInteger(cacheSize) || cacheSize <= 0) { | ||
throw new Error('The `cacheSize` property must be a positive integer value.'); | ||
} | ||
this._cache = {}; | ||
this._cacheOrdering = []; | ||
this._cacheSize = cacheSize; | ||
} | ||
FifoCacheObject.prototype.set = function set$$1(key, selectorFn) { | ||
this._cache[key] = selectorFn; | ||
this._cacheOrdering.push(key); | ||
if (this._cacheOrdering.length > this._cacheSize) { | ||
var earliest = this._cacheOrdering[0]; | ||
this.remove(earliest); | ||
} | ||
}; | ||
FifoCacheObject.prototype.get = function get$$1(key) { | ||
return this._cache[key]; | ||
}; | ||
FifoCacheObject.prototype.remove = function remove(key) { | ||
var index = this._cacheOrdering.indexOf(key); | ||
if (index > -1) { | ||
this._cacheOrdering.splice(index, 1); | ||
} | ||
delete this._cache[key]; | ||
}; | ||
FifoCacheObject.prototype.clear = function clear() { | ||
this._cache = {}; | ||
this._cacheOrdering = []; | ||
}; | ||
return FifoCacheObject; | ||
}(); | ||
var LruCacheObject = function () { | ||
function LruCacheObject() { | ||
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, | ||
cacheSize = _ref.cacheSize; | ||
classCallCheck(this, LruCacheObject); | ||
if (cacheSize === undefined) { | ||
throw new Error('Missing the required property `cacheSize`.'); | ||
} | ||
if (!Number.isInteger(cacheSize) || cacheSize <= 0) { | ||
throw new Error('The `cacheSize` property must be a positive integer value.'); | ||
} | ||
this._cache = {}; | ||
this._cacheOrdering = []; | ||
this._cacheSize = cacheSize; | ||
} | ||
LruCacheObject.prototype.set = function set$$1(key, selectorFn) { | ||
this._cache[key] = selectorFn; | ||
this._registerCacheHit(key); | ||
if (this._cacheOrdering.length > this._cacheSize) { | ||
var earliest = this._cacheOrdering[0]; | ||
this.remove(earliest); | ||
} | ||
}; | ||
LruCacheObject.prototype.get = function get$$1(key) { | ||
this._registerCacheHit(key); | ||
return this._cache[key]; | ||
}; | ||
LruCacheObject.prototype.remove = function remove(key) { | ||
this._deleteCacheHit(key); | ||
delete this._cache[key]; | ||
}; | ||
LruCacheObject.prototype.clear = function clear() { | ||
this._cache = {}; | ||
this._cacheOrdering = []; | ||
}; | ||
LruCacheObject.prototype._registerCacheHit = function _registerCacheHit(key) { | ||
this._deleteCacheHit(key); | ||
this._cacheOrdering.push(key); | ||
}; | ||
LruCacheObject.prototype._deleteCacheHit = function _deleteCacheHit(key) { | ||
var index = this._cacheOrdering.indexOf(key); | ||
if (index > -1) { | ||
this._cacheOrdering.splice(index, 1); | ||
} | ||
}; | ||
return LruCacheObject; | ||
}(); | ||
function createCachedSelector() { | ||
@@ -13,7 +151,21 @@ for (var _len = arguments.length, funcs = Array(_len), _key = 0; _key < _len; _key++) { | ||
var cache = {}; | ||
var defaultCacheCreator = FlatCacheObject; | ||
return function (resolver) { | ||
var createSelectorInstance = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _reselect.createSelector; | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var cache = void 0; | ||
var selectorCreator = void 0; | ||
// Allow "options" to be provided as a "selectorCreator" for backward compatibility | ||
// @TODO Remove "options" as a function in next breaking release | ||
if (typeof options === 'function') { | ||
console.warn('[re-reselect] Deprecation warning: "selectorCreator" argument is discouraged and will be removed in the upcoming major release. Please use "options.selectorCreator" instead.'); | ||
cache = new defaultCacheCreator(); | ||
selectorCreator = options; | ||
} else { | ||
cache = options.cacheObject || new defaultCacheCreator(); | ||
selectorCreator = options.selectorCreator || reselect.createSelector; | ||
} | ||
var selector = function selector() { | ||
@@ -24,8 +176,10 @@ // Application receives this function | ||
if (typeof cacheKey === 'string' || typeof cacheKey === 'number') { | ||
var _cache; | ||
var cacheResponse = cache.get(cacheKey); | ||
if (cache[cacheKey] === undefined) { | ||
cache[cacheKey] = createSelectorInstance.apply(undefined, funcs); | ||
if (cacheResponse === undefined) { | ||
cacheResponse = selectorCreator.apply(undefined, funcs); | ||
cache.set(cacheKey, cacheResponse); | ||
} | ||
return (_cache = cache)[cacheKey].apply(_cache, arguments); | ||
return cacheResponse.apply(undefined, arguments); | ||
} | ||
@@ -38,3 +192,4 @@ return undefined; | ||
var cacheKey = resolver.apply(undefined, arguments); | ||
return cache[cacheKey]; | ||
// @NOTE It might update cache hit count in LRU-like caches | ||
return cache.get(cacheKey); | ||
}; | ||
@@ -44,9 +199,7 @@ | ||
var cacheKey = resolver.apply(undefined, arguments); | ||
if (cache[cacheKey] !== undefined) { | ||
cache[cacheKey] = undefined; | ||
} | ||
cache.remove(cacheKey); | ||
}; | ||
selector.clearCache = function () { | ||
cache = {}; | ||
cache.clear(); | ||
}; | ||
@@ -58,2 +211,8 @@ | ||
}; | ||
} | ||
} | ||
exports['default'] = createCachedSelector; | ||
exports.FlatCacheObject = FlatCacheObject; | ||
exports.FifoCacheObject = FifoCacheObject; | ||
exports.LruCacheObject = LruCacheObject; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "re-reselect", | ||
"version": "0.6.3", | ||
"version": "1.0.0", | ||
"description": "Memoize selectors and avoid recalculation between calls with different inputs", | ||
@@ -8,3 +8,3 @@ "main": "lib/index.js", | ||
"jsnext:main": "es/index.js", | ||
"typings": "./index.d.ts", | ||
"typings": "src/index.d.ts", | ||
"files": [ | ||
@@ -14,3 +14,3 @@ "dist", | ||
"lib", | ||
"index.d.ts" | ||
"src" | ||
], | ||
@@ -21,7 +21,4 @@ "scripts": { | ||
"test:typescript": "typings-tester --dir typescript_test", | ||
"clean": "rm -rf dist es lib", | ||
"compile": "npm run clean && npm run compile:es && npm run compile:commonjs && npm run compile:umd", | ||
"compile:es": "babel -d es/ index.js", | ||
"compile:commonjs": "NODE_ENV=commonjs babel -d lib/ index.js", | ||
"compile:umd": "NODE_ENV=umd babel -d dist/ index.js", | ||
"clean": "rimraf dist es lib", | ||
"compile": "npm run clean && cross-env BABEL_ENV=rollup rollup -c", | ||
"contrib:add": "all-contributors add", | ||
@@ -32,3 +29,6 @@ "contrib:generate": "all-contributors generate", | ||
"postversion": "git push && git push --tags", | ||
"prepublish": "npm test && npm run compile" | ||
"prepublish": "npm test && npm run compile", | ||
"precommit": "lint-staged", | ||
"prettier": "prettier --single-quote --trailing-comma es5 --no-bracket-spacing --write", | ||
"prettier:src": "npm run prettier \"src/**/*.js\"" | ||
}, | ||
@@ -52,10 +52,16 @@ "keywords": [ | ||
"all-contributors-cli": "^4.3.0", | ||
"babel-cli": "^6.23.0", | ||
"babel-core": "^6.23.1", | ||
"babel-jest": "^19.0.0", | ||
"babel-plugin-transform-es2015-modules-umd": "^6.24.1", | ||
"babel-plugin-external-helpers": "^6.22.0", | ||
"babel-preset-es2015": "^6.22.0", | ||
"babel-register": "^6.23.0", | ||
"cross-env": "^5.0.5", | ||
"husky": "^0.14.3", | ||
"jest": "^19.0.2", | ||
"lint-staged": "^4.0.3", | ||
"prettier": "^1.5.3", | ||
"reselect": ">1.0.0", | ||
"rimraf": "^2.6.1", | ||
"rollup": "^0.47.4", | ||
"rollup-plugin-babel": "^3.0.1", | ||
"typescript": "^2.4.1", | ||
@@ -62,0 +68,0 @@ "typings-tester": "^0.2.2" |
# Re-reselect [![Build Status][ci-img]][ci] | ||
Improve **[Reselect][reselect] selectors performance** on a few edge cases, by initializing selectors on runtime, using a **memoized factory**. | ||
Improve **[Reselect][reselect] selectors performance/usage** on a few edge cases, by initializing selectors on runtime, using a **memoized factory**. | ||
@@ -10,5 +10,8 @@ **Re-reselect returns a reselect-like selector**, which is able to determine internally when **querying a new selector instance or a cached one** on the fly, depending on the supplied arguments. | ||
- a selector is **imported by different modules** at the same time | ||
- **sharing selectors** with props across multiple components (see [reselect example](https://github.com/reactjs/reselect#sharing-selectors-with-props-across-multiple-components) and [re-reselect solution](https://github.com/toomuchdesign/re-reselect#how-to-share-a-selector-across-multiple-components-while-passing-in-props-and-retaining-memoization)) | ||
- selectors need to be **instantiated on runtime** | ||
...or to: | ||
- **join similar selectors** into one | ||
- **share selectors** with props across multiple components (see [reselect example](https://github.com/reactjs/reselect#sharing-selectors-with-props-across-multiple-components) and [re-reselect solution](https://github.com/toomuchdesign/re-reselect#how-to-share-a-selector-across-multiple-components-while-passing-in-props-and-retaining-memoization)) | ||
- **instantiate** selectors **on runtime** | ||
[reselect]: https://github.com/reactjs/reselect | ||
@@ -18,3 +21,9 @@ [ci-img]: https://travis-ci.org/toomuchdesign/re-reselect.svg | ||
```js | ||
import reselect from 'reselect'; | ||
``` | ||
```js | ||
import createCachedSelector from 're-reselect'; | ||
@@ -80,2 +89,3 @@ | ||
- [How do I use multiple inputs to set the cache key?](#how-do-i-use-multiple-inputs-to-set-the-cache-key) | ||
- [How do I limit the cache size?](#how-do-i-limit-the-cache-size) | ||
- [How to share a selector across multiple components while passing in props and retaining memoization?](#how-to-share-a-selector-across-multiple-components-while-passing-in-props-and-retaining-memoization) | ||
@@ -214,2 +224,5 @@ - [How do I test a re-reselect selector?](#how-do-i-test-a-re-reselect-selector) | ||
### How do I limit the cache size? | ||
Use the [`cacheObject` option](#optionscacheobject). | ||
### How to share a selector across multiple components while passing in props and retaining memoization? | ||
@@ -313,3 +326,3 @@ This example is how `re-reselect` would solve the scenario described in [Reselect docs](https://github.com/reactjs/reselect#sharing-selectors-with-props-across-multiple-components). | ||
## API | ||
**Re-reselect** consists in just one method exported as default. | ||
`Re-reselect` exposes its **cached selector creator** as **default export**. | ||
@@ -320,3 +333,3 @@ ```js | ||
### reReselect([reselect's createSelector arguments])(resolverFunction, selectorCreator = selectorCreator) | ||
### reReselect([reselect's createSelector arguments])(resolverFunction, { cacheObject, selectorCreator }) | ||
@@ -326,3 +339,3 @@ **Re-reselect** accepts your original [selector creator arguments](https://github.com/reactjs/reselect/tree/v2.5.4#createselectorinputselectors--inputselectors-resultfunc) and returns a new function which accepts **2 arguments**: | ||
- `resolverFunction` | ||
- `selectorCreator` *(optional)* | ||
- `options { cacheObject, selectorCreator }` *(optional)* | ||
@@ -336,5 +349,42 @@ #### resolverFunction | ||
#### selectorCreator | ||
`selectorCreator` is an optional function in case you want to use [custom selectors](https://github.com/reactjs/reselect/tree/v3.0.0#createselectorcreatormemoize-memoizeoptions). By default it uses Reselect's `createSelector`. | ||
#### options.cacheObject | ||
An optional custom [strategy object](https://sourcemaking.com/design_patterns/strategy) to handle the caching behaviour. It must adhere to the following interface: | ||
```js | ||
interface ICacheObject { | ||
set (key: string|number, selectorFn: Function): void; | ||
get (key: string|number): Function; | ||
remove (key: string|number): void; | ||
clear (): void; | ||
} | ||
``` | ||
`re-reselect` provides **3 ready to use cache object creators**: | ||
- [`FlatCacheObject`](src/cache/FlatCacheObject.js) (default) | ||
- [`FifoCacheObject`](src/cache/FifoCacheObject.js) ([first in first out cache](https://en.wikipedia.org/wiki/Cache_replacement_policies#First_In_First_Out_.28FIFO.29)) | ||
- [`LruCacheObject`](src/cache/LruCacheObject.js) ([least recently used cache](https://en.wikipedia.org/wiki/Cache_replacement_policies#Least_Recently_Used_.28LRU.29)) | ||
```js | ||
import createCachedSelector, { LruCacheObject, FifoCacheObject } from re-reselect; | ||
createCachedSelector( | ||
// ... | ||
)( | ||
resolverFunction, | ||
{ | ||
cacheObject: new LruCacheObject({ cacheSize: 5 }); | ||
// or: | ||
// cacheObject: new FifoCacheObject({ cacheSize: 5 }); | ||
} | ||
) | ||
``` | ||
The default cache strategy, `FlatCache` doesn't limit cache. | ||
You can provide **any kind of caching strategy**. Just write your own. You can use the [existing ones](src/cache/) as starting point. | ||
#### options.selectorCreator | ||
`selectorCreator` is an optional function describing a [custom selectors](https://github.com/reactjs/reselect/tree/v3.0.0#createselectorcreatormemoize-memoizeoptions). By default it uses Reselect's `createSelector`. | ||
#### Returns | ||
@@ -361,3 +411,2 @@ (Function): a `reReselectInstance` selector ready to be used **like a normal reselect selector**. | ||
## Todo's | ||
- Named exports? | ||
- Flow type definitions? | ||
@@ -372,3 +421,3 @@ - Improve TS tests readability | ||
| :---: | :---: | :---: | :---: | :---: | :---: | :---: | | ||
| [<img src="https://avatars3.githubusercontent.com/u/1177323?v=4" width="100px;"/><br /><sub>Denis Sikuler</sub>](https://github.com/gamtiq)<br />[💬](#question-gamtiq "Answering Questions") | [<img src="https://avatars1.githubusercontent.com/u/1586931?v=4" width="100px;"/><br /><sub>Sigfried Gold</sub>](http://sigfried.org)<br />[💬](#question-Sigfried "Answering Questions") | [<img src="https://avatars1.githubusercontent.com/u/132278?v=4" width="100px;"/><br /><sub>Daniel Karp</sub>](https://twitter.com/karptonite)<br />[💬](#question-karptonite "Answering Questions") | [<img src="https://avatars3.githubusercontent.com/u/10407025?v=4" width="100px;"/><br /><sub>Maarten Schumacher</sub>](https://github.com/maartenschumacher)<br />[💬](#question-maartenschumacher "Answering Questions") | [<img src="https://avatars2.githubusercontent.com/u/1428826?v=4" width="100px;"/><br /><sub>Gaurav Lahoti</sub>](https://github.com/Dante-101)<br />[🐛](https://github.com/toomuchdesign/re-reselect/issues?q=author%3ADante-101 "Bug reports") | [<img src="https://avatars3.githubusercontent.com/u/13602053?v=4" width="100px;"/><br /><sub>Lon</sub>](http://lon.im)<br />[🐛](https://github.com/toomuchdesign/re-reselect/issues?q=author%3Acnlon "Bug reports") | | ||
| [<img src="https://avatars2.githubusercontent.com/u/1428826?v=4" width="100px;"/><br /><sub>Gaurav Lahoti</sub>](https://github.com/Dante-101)<br />[🐛](https://github.com/toomuchdesign/re-reselect/issues?q=author%3ADante-101 "Bug reports") | [<img src="https://avatars3.githubusercontent.com/u/13602053?v=4" width="100px;"/><br /><sub>Lon</sub>](http://lon.im)<br />[🐛](https://github.com/toomuchdesign/re-reselect/issues?q=author%3Acnlon "Bug reports") | [<img src="https://avatars2.githubusercontent.com/u/5492495?v=4" width="100px;"/><br /><sub>bratushka</sub>](https://github.com/bratushka)<br />[💻](https://github.com/toomuchdesign/re-reselect/commits?author=bratushka "Code") | | ||
<!-- ALL-CONTRIBUTORS-LIST:END --> |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
107327
19
1520
1
414
17
1