UNPKG

@mic-rexjs/usecases-react

Version:

React-based solution for use usecases of Clean Architecture

79 lines (78 loc) 2.63 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.methodUseCase = void 0; var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray")); var _valueUseCase2 = require("../valueUseCase"); var methodUseCase = exports.methodUseCase = function methodUseCase() { var _valueUseCase = (0, _valueUseCase2.valueUseCase)(), matchValue = _valueUseCase.matchValue; var 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 matchValue(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; }; }; var cacheCalls = function cacheCalls(sourceFactories, options) { var cachedFactories = {}; Object.entries(sourceFactories).forEach(function (_ref2) { var _ref3 = (0, _slicedToArray2["default"])(_ref2, 2), key = _ref3[0], factory = _ref3[1]; cachedFactories[key] = cacheCall(factory, options); }); return cachedFactories; }; var captureCalls = function captureCalls(obj, callback) { var newObj = {}; Object.keys(obj).forEach(function (key) { var 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: cacheCall, cacheCalls: cacheCalls, captureCalls: captureCalls }; };