"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 __values = (this && this.__values) || function (o) { var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0; if (m) return m.call(o); return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; }; 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"); var sync_1 = require("./sync"); function asyncMemoizer(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 queueMaxAge = options.queueMaxAge || 1000; var loading = new Map(); var emitter = new events_1.EventEmitter(); var memoizerMethods = 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, memoizerMethods); } function del() { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } var key = hash.apply(void 0, __spread(args)); cache.del(key); } function add(key, parameters, result) { if (freeze) { result.forEach(freeze_1.deepFreeze); } if (itemMaxAge) { cache.set(key, result, itemMaxAge.apply(void 0, __spread(parameters.concat(result)))); } else { cache.set(key, result); } } function runCallbacks(callbacks, args) { var e_1, _a; try { for (var callbacks_1 = __values(callbacks), callbacks_1_1 = callbacks_1.next(); !callbacks_1_1.done; callbacks_1_1 = callbacks_1.next()) { var callback = callbacks_1_1.value; // Simulate async call when returning from cache // and yield between callback resolution if (clone) { setImmediate.apply(void 0, __spread([callback], args.map(lodash_clonedeep_1.default))); } else { setImmediate.apply(void 0, __spread([callback], args)); } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (callbacks_1_1 && !callbacks_1_1.done && (_a = callbacks_1.return)) _a.call(callbacks_1); } finally { if (e_1) throw e_1.error; } } } 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 memoizedFunction() { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } var parameters = args.slice(0, -1); var callback = args.slice(-1).pop(); var key; if (bypass && bypass.apply(void 0, __spread(parameters))) { emit.apply(void 0, __spread(['miss'], parameters)); return load.apply(void 0, __spread(args)); } if (parameters.length === 0 && !hash) { //the load function only receives callback. key = '_'; } else { key = hash.apply(void 0, __spread(parameters)); } var fromCache = cache.get(key); if (fromCache) { emit.apply(void 0, __spread(['hit'], parameters)); // found, invoke callback return runCallbacks([callback], [null].concat(fromCache)); } var pendingLoad = loading.get(key); if (pendingLoad && pendingLoad.expiresAt > Date.now()) { // request already in progress, queue and return pendingLoad.queue.push(callback); emit.apply(void 0, __spread(['queue'], parameters)); return; } emit.apply(void 0, __spread(['miss'], parameters)); var started = Date.now(); // no pending request or not resolved before expiration // create a new queue and invoke load var queue = [callback]; loading.set(key, { queue: queue, expiresAt: started + queueMaxAge }); var loadHandler = function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } var err = args[0]; if (!err) { add(key, parameters, args.slice(1)); } // this can potentially delete a different queue than `queue` if // this callback was called after expiration. // that will only cause a new call to be performed and a new queue to be // created loading.delete(key); emit.apply(void 0, __spread(['loaded', Date.now() - started], parameters)); runCallbacks(queue, args); }; load.apply(void 0, __spread(parameters, [loadHandler])); } ; return Object.assign(memoizedFunction, memoizerMethods); } exports.asyncMemoizer = asyncMemoizer; asyncMemoizer.sync = sync_1.syncMemoizer; //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYXN5bmMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvYXN5bmMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7QUFBQSx3REFBNEI7QUFDNUIsaUNBQXNDO0FBQ3RDLHNFQUF5QztBQUN6QyxtQ0FBc0M7QUFDdEMsK0JBQXNDO0FBOEV0QyxTQUFTLGFBQWEsQ0FDbEIsT0FBcUQ7SUFFdkQsSUFBTSxLQUFLLEdBQVEsSUFBSSxtQkFBRyxDQUFDLE9BQU8sQ0FBQyxDQUFDO0lBQ3BDLElBQU0sSUFBSSxHQUFTLE9BQU8sQ0FBQyxJQUFJLENBQUM7SUFDaEMsSUFBTSxJQUFJLEdBQVMsT0FBTyxDQUFDLElBQUksQ0FBQztJQUNoQyxJQUFNLE1BQU0sR0FBTyxPQUFPLENBQUMsTUFBTSxDQUFDO0lBQ2xDLElBQU0sVUFBVSxHQUFHLE9BQU8sQ0FBQyxVQUFVLENBQUM7SUFDdEMsSUFBTSxNQUFNLEdBQU8sT0FBTyxDQUFDLE1BQU0sQ0FBQztJQUNsQyxJQUFNLEtBQUssR0FBUSxPQUFPLENBQUMsS0FBSyxDQUFDO0lBQ2pDLElBQU0sV0FBVyxHQUFHLE9BQU8sQ0FBQyxXQUFXLElBQUksSUFBSSxDQUFDO0lBQ2hELElBQU0sT0FBTyxHQUFNLElBQUksR0FBRyxFQUF1QixDQUFDO0lBQ2xELElBQU0sT0FBTyxHQUFNLElBQUkscUJBQVksRUFBRSxDQUFDO0lBRXRDLElBQU0sZUFBZSxHQUFHLE1BQU0sQ0FB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