Last active
February 14, 2017 11:52
-
-
Save neekey/f6e3a21691596ba8a58908743c22c2ed to your computer and use it in GitHub Desktop.
multipleCacheMemoize
This file contains 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
/* 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