Skip to content

Instantly share code, notes, and snippets.

@neekey
Last active February 14, 2017 11:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neekey/f6e3a21691596ba8a58908743c22c2ed to your computer and use it in GitHub Desktop.
Save neekey/f6e3a21691596ba8a58908743c22c2ed to your computer and use it in GitHub Desktop.
multipleCacheMemoize
/* eslint-disable */
import { createSelectorCreator } from 'reselect';
function execFunc(func, ...args) {
return func(...args);
}
function multipleCacheMemoize(func) {
const argsArray = [];
const resultArray = [];
function matchArgs(args) {
let targetIndex = -1;
const argsArrayLen = argsArray.length;
const argsLen = args.length;
for(let index = 0; index < argsArrayLen; index++) {
const values = argsArray[index];
if (values.length === argsLen) {
let match = true;
for(let i = 0; i < argsLen; i++) {
if (args[i] !== values[i]) {
match = false;
break;
}
}
if (match) {
targetIndex = index;
break;
}
}
}
return targetIndex;
}
return (...args) => {
const list = [...args];
let index = matchArgs(list);
if (index < 0) {
const result = execFunc(func, ...args);
argsArray.push(list);
resultArray.push(result);
index = argsArray.length - 1;
}
return resultArray[index];
};
}
export const multipleCacheSelectorCreator = createSelectorCreator(multipleCacheMemoize);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment