@mic-rexjs/usecases-react
Version:
React-based solution for use usecases of Clean Architecture
41 lines (40 loc) • 1.24 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.cacheCall = void 0;
var _isSameArray = require("../isSameArray");
var cacheCall = exports.cacheCall = function cacheCall(factory, options) {
var cache;
var _ref = options || {},
_ref$onShouldCache = _ref.onShouldCache,
onShouldCache = _ref$onShouldCache === void 0 ? function () {
return true;
} : _ref$onShouldCache,
_ref$onCompare = _ref.onCompare,
onCompare = _ref$onCompare === void 0 ? function (newArgs, oldArgs) {
return (0, _isSameArray.isSameArray)(newArgs, oldArgs);
} : _ref$onCompare;
return function () {
var cacheable = onShouldCache();
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
if (cache && cacheable) {
var _cache = cache,
parameters = _cache.parameters,
_returnValue = _cache.returnValue;
if (onCompare(parameters, args)) {
return _returnValue;
}
}
var returnValue = factory.apply(void 0, args);
if (cacheable) {
cache = {
parameters: args,
returnValue: returnValue
};
}
return returnValue;
};
};