"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const composite_abort_controller_1 = __importDefault(require("composite-abort-controller")); const mem_1 = __importDefault(require("mem")); const mimic_fn_1 = __importDefault(require("mimic-fn")); const race_abort_1 = __importDefault(require("race-abort")); function memoizeConcurrent(fn, opts) { const cache = opts?.cache ?? new Map(); const cacheKey = opts?.cacheKey ?? ((args) => args[0]); const getSignal = opts?.signalAccessors?.get ?? ((args) => null); const setSignal = opts?.signalAccessors?.set ?? ((signal, args) => args); const memoized = mem_1.default(mimic_fn_1.default(async (...args) => { const key = cacheKey(args); // @ts-ignore const controllerKey = `__nodeMemoizeConcurrent:compositeAbortController:${key}`; try { return await fn(...args); } finally { cache.delete(key); cache.delete(controllerKey); } }, fn), { ...opts, cacheKey, cache, }); if (opts?.signalAccessors == null) return memoized; return function wrapper(...args) { // @ts-ignore const controllerKey = `__nodeMemoizeConcurrent:compositeAbortController:${cacheKey(args)}`; // @ts-ignore const compositeAbortController = cache.get(controllerKey)?.data ?? new composite_abort_controller_1.default(); cache.set(controllerKey, { // @ts-ignore data: compositeAbortController, maxAge: Infinity, }); const newSignal = getSignal(args); const argsWithSignal = setSignal(compositeAbortController.signal, args); if (newSignal != null) { compositeAbortController.addSignal(newSignal); return race_abort_1.default(newSignal, memoized(...argsWithSignal)); } return memoized(...argsWithSignal); }; } exports.default = memoizeConcurrent; //# sourceMappingURL=index.js.map