"use strict"; var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; var __spread = (this && this.__spread) || function () { for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i])); return ar; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var lru_cache_1 = __importDefault(require("lru-cache")); var events_1 = require("events"); var lodash_clonedeep_1 = __importDefault(require("lodash.clonedeep")); var freeze_1 = require("./freeze"); function syncMemoizer(options) { var cache = new lru_cache_1.default(options); var load = options.load; var hash = options.hash; var bypass = options.bypass; var itemMaxAge = options.itemMaxAge; var freeze = options.freeze; var clone = options.clone; var emitter = new events_1.EventEmitter(); var defaultResult = Object.assign({ del: del, reset: function () { return cache.reset(); }, keys: cache.keys.bind(cache), on: emitter.on.bind(emitter), once: emitter.once.bind(emitter) }, options); if (options.disable) { return Object.assign(load, defaultResult); } function del() { var key = hash.apply(void 0, __spread(arguments)); cache.del(key); } function emit(event) { var parameters = []; for (var _i = 1; _i < arguments.length; _i++) { parameters[_i - 1] = arguments[_i]; } emitter.emit.apply(emitter, __spread([event], parameters)); } function isPromise(result) { // detect native, bluebird, A+ promises return result && result.then && typeof result.then === 'function'; } function processResult(result) { var res = result; if (clone) { if (isPromise(res)) { res = res.then(lodash_clonedeep_1.default); } else { res = lodash_clonedeep_1.default(res); } } if (freeze) { if (isPromise(res)) { res = res.then(freeze_1.deepFreeze); } else { freeze_1.deepFreeze(res); } } return res; } var result = function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } if (bypass && bypass.apply(void 0, __spread(args))) { emit.apply(void 0, __spread(['miss'], args)); return load.apply(void 0, __spread(args)); } var key = hash.apply(void 0, __spread(args)); var fromCache = cache.get(key); if (fromCache) { emit.apply(void 0, __spread(['hit'], args)); return processResult(fromCache); } emit.apply(void 0, __spread(['miss'], args)); var result = load.apply(void 0, __spread(args)); if (itemMaxAge) { // @ts-ignore cache.set(key, result, itemMaxAge.apply(void 0, __spread(args.concat([result])))); } else { cache.set(key, result); } return processResult(result); }; return Object.assign(result, defaultResult); } exports.syncMemoizer = syncMemoizer; //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic3luYy5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3NyYy9zeW5jLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7QUFBQSx3REFBNEI7QUFDNUIsaUNBQXNDO0FBQ3RDLHNFQUF5QztBQUN6QyxtQ0FBc0M7QUF5RHRDLFNBQWdCLFlBQVksQ0FDeEIsT0FBb0Q7SUFFdEQsSUFBTSxLQUFLLEdBQVEsSUFBSSxtQkFBRyxDQUFDLE9BQU8sQ0FBQyxDQUFDO0lBQ3BDLElBQU0sSUFBSSxHQUFTLE9BQU8sQ0FBQyxJQUFJLENBQUM7SUFDaEMsSUFBTSxJQUFJLEdBQVMsT0FBTyxDQUFDLElBQUksQ0FBQztJQUNoQyxJQUFNLE1BQU0sR0FBTyxPQUFPLENBQUMsTUFBTSxDQUFDO0lBQ2xDLElBQU0sVUFBVSxHQUFHLE9BQU8sQ0FBQyxVQUFVLENBQUM7SUFDdEMsSUFBTSxNQUFNLEdBQU8sT0FBTyxDQUFDLE1BQU0sQ0FBQztJQUNsQyxJQUFNLEtBQUssR0FBUSxPQUFPLENBQUMsS0FBSyxDQUFDO0lBQ2pDLElBQU0sT0FBTyxHQUFNLElBQUkscUJBQVksRUFBRSxDQUFDO0lBRXRDLElBQU0sYUFBYSxHQUFHLE1BQU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