@mic-rexjs/usecases-react
Version:
React-based solution for use usecases of Clean Architecture
27 lines (26 loc) • 768 B
JavaScript
import { isSameArray } from '../isSameArray';
export const cacheCall = (factory, options) => {
let cache;
const { onShouldCache = () => {
return true;
}, onCompare = (newArgs, oldArgs) => {
return isSameArray(newArgs, oldArgs);
}, } = options || {};
return ((...args) => {
const cacheable = onShouldCache();
if (cache && cacheable) {
const { parameters, returnValue } = cache;
if (onCompare(parameters, args)) {
return returnValue;
}
}
const returnValue = factory(...args);
if (cacheable) {
cache = {
parameters: args,
returnValue,
};
}
return returnValue;
});
};