Last active
May 4, 2020 12:57
-
-
Save AndreiCalazans/094ae9653c4a29943bc517d8cc5cb0bd to your computer and use it in GitHub Desktop.
Reselect with control over cache (dumpCache and ReselectCache Map) import from `reselect/lib`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 'use strict'; | |
| exports.__esModule = true; | |
| exports.defaultMemoize = defaultMemoize; | |
| exports.createSelectorCreator = createSelectorCreator; | |
| exports.createStructuredSelector = createStructuredSelector; | |
| function defaultEqualityCheck(a, b) { | |
| return a === b; | |
| } | |
| function areArgumentsShallowlyEqual(equalityCheck, prev, next) { | |
| if (prev === null || next === null || prev.length !== next.length) { | |
| return false; | |
| } | |
| // Do this in a for loop (and not a `forEach` or an `every`) so we can determine equality as fast as possible. | |
| var length = prev.length; | |
| for (var i = 0; i < length; i++) { | |
| if (!equalityCheck(prev[i], next[i])) { | |
| return false; | |
| } | |
| } | |
| return true; | |
| } | |
| var poormanUniqueKey = 0; | |
| var ReselectCache = new Map(); | |
| exports.ReselectCache = ReselectCache; | |
| exports.dumpCache = function dumpCache() { | |
| ReselectCache.forEach((value, key, map) => { | |
| map.set(key, { | |
| lastArgs: null, | |
| lastResult: null | |
| }); | |
| }); | |
| } | |
| function defaultMemoize(func) { | |
| var equalityCheck = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultEqualityCheck; | |
| var uniqueKey = ++poormanUniqueKey; | |
| ReselectCache.set(uniqueKey, { | |
| lastArgs: null, | |
| lastResult: null, | |
| }); | |
| // var lastArgs = null; | |
| // var lastResult = null; | |
| // | |
| // we reference arguments instead of spreading them for performance reasons | |
| return function () { | |
| var currentCache = ReselectCache.get(uniqueKey); | |
| var lastResult = currentCache.lastResult; | |
| if (!areArgumentsShallowlyEqual(equalityCheck, currentCache.lastArgs, arguments)) { | |
| lastResult = func.apply(null, arguments); | |
| } | |
| ReselectCache.set(uniqueKey, { | |
| lastArgs: arguments, | |
| lastResult, | |
| }); | |
| // lastArgs = arguments; | |
| return lastResult; | |
| }; | |
| } | |
| function getDependencies(funcs) { | |
| var dependencies = Array.isArray(funcs[0]) ? funcs[0] : funcs; | |
| if (!dependencies.every(function (dep) { | |
| return typeof dep === 'function'; | |
| })) { | |
| var dependencyTypes = dependencies.map(function (dep) { | |
| return typeof dep; | |
| }).join(', '); | |
| throw new Error('Selector creators expect all input-selectors to be functions, ' + ('instead received the following types: [' + dependencyTypes + ']')); | |
| } | |
| return dependencies; | |
| } | |
| function createSelectorCreator(memoize) { | |
| for (var _len = arguments.length, memoizeOptions = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | |
| memoizeOptions[_key - 1] = arguments[_key]; | |
| } | |
| return function () { | |
| for (var _len2 = arguments.length, funcs = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | |
| funcs[_key2] = arguments[_key2]; | |
| } | |
| var recomputations = 0; | |
| var resultFunc = funcs.pop(); | |
| var dependencies = getDependencies(funcs); | |
| var memoizedResultFunc = memoize.apply(undefined, [function () { | |
| recomputations++; | |
| // apply arguments instead of spreading for performance. | |
| return resultFunc.apply(null, arguments); | |
| }].concat(memoizeOptions)); | |
| // If a selector is called with the exact same arguments we don't need to traverse our dependencies again. | |
| var selector = memoize(function () { | |
| var params = []; | |
| var length = dependencies.length; | |
| for (var i = 0; i < length; i++) { | |
| // apply arguments instead of spreading and mutate a local list of params for performance. | |
| params.push(dependencies[i].apply(null, arguments)); | |
| } | |
| // apply arguments instead of spreading for performance. | |
| return memoizedResultFunc.apply(null, params); | |
| }); | |
| selector.resultFunc = resultFunc; | |
| selector.dependencies = dependencies; | |
| selector.recomputations = function () { | |
| return recomputations; | |
| }; | |
| selector.resetRecomputations = function () { | |
| return recomputations = 0; | |
| }; | |
| return selector; | |
| }; | |
| } | |
| var createSelector = exports.createSelector = createSelectorCreator(defaultMemoize); | |
| function createStructuredSelector(selectors) { | |
| var selectorCreator = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : createSelector; | |
| if (typeof selectors !== 'object') { | |
| throw new Error('createStructuredSelector expects first argument to be an object ' + ('where each property is a selector, instead received a ' + typeof selectors)); | |
| } | |
| var objectKeys = Object.keys(selectors); | |
| return selectorCreator(objectKeys.map(function (key) { | |
| return selectors[key]; | |
| }), function () { | |
| for (var _len3 = arguments.length, values = Array(_len3), _key3 = 0; _key3 < _len3; _key3++) { | |
| values[_key3] = arguments[_key3]; | |
| } | |
| return values.reduce(function (composition, value, index) { | |
| composition[objectKeys[index]] = value; | |
| return composition; | |
| }, {}); | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Replace
node_modules/reselect/lib/index.jswith the code above