123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- "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 s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
- if (m) return m.call(o);
- if (o && typeof o.length === "number") return {
- next: function () {
- if (o && i >= o.length) o = void 0;
- return { value: o && o[i++], done: !o };
- }
- };
- throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
- };
- var __importDefault = (this && this.__importDefault) || function (mod) {
- return (mod && mod.__esModule) ? mod : { "default": mod };
- };
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.asyncMemoizer = void 0;
- 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;
-
-
- 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) {
-
- key = '_';
- }
- else {
- key = hash.apply(void 0, __spread(parameters));
- }
- var fromCache = cache.get(key);
- if (fromCache) {
- emit.apply(void 0, __spread(['hit'], parameters));
-
- return runCallbacks([callback], [null].concat(fromCache));
- }
- var pendingLoad = loading.get(key);
- if (pendingLoad && pendingLoad.expiresAt > Date.now()) {
-
- pendingLoad.queue.push(callback);
- emit.apply(void 0, __spread(['queue'], parameters));
- return;
- }
- emit.apply(void 0, __spread(['miss'], parameters));
- var started = Date.now();
-
-
- 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));
- }
-
-
-
-
- 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;
|