UNPKG

@mic-rexjs/usecases-react

Version:

React-based solution for use usecases of Clean Architecture

72 lines 2.21 kB
import _slicedToArray from "@babel/runtime/helpers/slicedToArray"; import { valueUseCase } from '../valueUseCase'; export const methodUseCase = () => { const _valueUseCase = valueUseCase(), matchValue = _valueUseCase.matchValue; const cacheCall = (factory, options) => { let cache; const _ref = options || {}, _ref$onShouldCache = _ref.onShouldCache, onShouldCache = _ref$onShouldCache === void 0 ? () => { return true; } : _ref$onShouldCache, _ref$onCompare = _ref.onCompare, onCompare = _ref$onCompare === void 0 ? (newArgs, oldArgs) => { return matchValue(newArgs, oldArgs); } : _ref$onCompare; return function () { const cacheable = onShouldCache(); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } if (cache && cacheable) { const _cache = cache, parameters = _cache.parameters, returnValue = _cache.returnValue; if (onCompare(parameters, args)) { return returnValue; } } const returnValue = factory(...args); if (cacheable) { cache = { parameters: args, returnValue }; } return returnValue; }; }; const cacheCalls = (sourceFactories, options) => { const cachedFactories = {}; Object.entries(sourceFactories).forEach(_ref2 => { let _ref3 = _slicedToArray(_ref2, 2), key = _ref3[0], factory = _ref3[1]; cachedFactories[key] = cacheCall(factory, options); }); return cachedFactories; }; const captureCalls = (obj, callback) => { const newObj = {}; Object.keys(obj).forEach(key => { const value = obj[key]; if (typeof value === 'function') { newObj[key] = function () { for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { args[_key2] = arguments[_key2]; } return callback(key, args); }; return; } newObj[key] = value; }); return newObj; }; return { cacheCall, cacheCalls, captureCalls }; };