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S,kBACT,MAAM,qBAAqB,uBAC3B,aAAa,EAAS,kBAE1B,MAAM,EAAyB,UAAU,UACzC,EAAS,mBAAqB,EAC9B,MAAM,EAAQ,EAAS,KAAK,MAC5B,GAAI,GAAS,EAIT,OAFA,EAAI,OAAO,QACX,EAAuB,UAI3B,MAgBM,EAAsB,YAhBR,IAEhB,GADA,MAAM,4BAA4B,KAC9B,EACA,IACI,EAAS,EAAK,EAAI,IAAI,GAC1B,CACA,MAAO,GAEH,MAAM,yDAAyD,MAAQ,EAAI,UAC/E,CAGJ,EAAI,OAAO,GACX,EAAuB,UACvB,EAAS,OAAO,EAAI,GAE2B,EAAO,GAI1D,OAHA,EAAS,gBAAkB,EAE3B,EAAoB,QACb,EAAuB,OAClC,IACM,EAAc,EAAI,IAAI,KAAK,GAC3B,EAAgB,EAAI,MAAM,KAAK,GACrC,EAAI,IAAM,CAAC,EAAK,KACZ,MAAM,EAAS,EAAY,EAAK,GAKhC,OAJA,EAAW,CAAC,EAAK,IAAQ,OAAO,IAE5B,MAAM,iDAAiD,MAAQ,EAAI,UAAU,IAE1E,CAAM,EAGjB,EAAI,MAAQ,KACR,IAAK,MAAO,EAAK,KAAc,EAAS,UACpC,MAAM,kCAAkC,KACpC,EAAU,iBACV,aAAa,EAAU,iBAEvB,EAAU,oBACV,EAAU,mBAAmB,UAGrC,EAAS,QACT,GAAe,EAEnB,IAAK,MAAO,EAAK,KAAU,EACvB,MAAM,kCAAkC,KACxC,EAAW,CAAC,EAAK,IAAQ,OAAO,IAE5B,MAAM,iDAAiD,MAAQ,EAAI,UAAU,IAGrF,OAAO,CACX,CACA,QAAQ,QAAU,cAElB,OAAO,QAAU,cAEjB,OAAO,QAAQ,QAAU","file":"/npm/@omni-tools/map-age-cleaner@1.0.0-alpha.1/dist/index.js","sourceRoot":"","sourcesContent":["/**\n * Minified by jsDelivr using Terser v5.39.0.\n * Original file: /npm/@omni-tools/map-age-cleaner@1.0.0-alpha.1/dist/index.js\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","\"use strict\";\nvar __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {\n    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }\n    return new (P || (P = Promise))(function (resolve, reject) {\n        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }\n        function rejected(value) { try { step(generator[\"throw\"](value)); } catch (e) { reject(e); } }\n        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }\n        step((generator = generator.apply(thisArg, _arguments || [])).next());\n    });\n};\nvar __importDefault = (this && this.__importDefault) || function (mod) {\n    return (mod && mod.__esModule) ? mod : { \"default\": mod };\n};\nObject.defineProperty(exports, \"__esModule\", { value: true });\nconst p_defer_1 = __importDefault(require(\"p-defer\"));\nconst debug_1 = __importDefault(require(\"debug\"));\nconst debug = debug_1.default('omni-tools:map-age-cleaner');\n/**\n * Automatically cleanup the items in the provided `map`.\n *\n * @param map - Map instance which should be cleaned up.\n * @param options - Options for the mapAgeCleaner\n * @param options.property - Name of the property which olds the expiry timestamp. §!! FIXME EXPIRE\n * @param options.onExpire - Callback to handle/be notified of the expiration of item\n */\nfunction mapAgeCleaner(map, options) {\n    const timerMap = new Map();\n    const safeOptions = options || {};\n    const { property = 'maxAge', onExpire } = safeOptions;\n    const setupTimer = ([key, value]) => __awaiter(this, void 0, void 0, function* () {\n        const maxAge = value[property];\n        debug(`setting up timer for item ${key} scheduled at ${maxAge}`);\n        if (!timerMap.has(key)) {\n            timerMap.set(key, { processingTimer: undefined, processingDeferred: undefined });\n        }\n        const timeItem = timerMap.get(key) || {};\n        if (timeItem.processingTimer) {\n            debug(`existing timer on ${key} expiring it first`);\n            clearTimeout(timeItem.processingTimer);\n        }\n        const itemProcessingDeferred = p_defer_1.default();\n        timeItem.processingDeferred = itemProcessingDeferred;\n        const delay = maxAge - Date.now();\n        if (delay <= 0) {\n            // Remove the item immediately if the delay is equal to or below 0\n            map.delete(key);\n            itemProcessingDeferred.resolve();\n            return;\n        }\n        // tslint:disable-next-line: no-shadowed-variable\n        const expireItem = (key) => {\n            debug(`expire the following key ${key}`);\n            if (onExpire) {\n                try {\n                    onExpire(key, map.get(key));\n                }\n                catch (err) {\n                    // tslint:disable-next-line: no-unsafe-any\n                    debug(`issue occured while expiring item associated with key ${key}: ${err.message}`);\n                }\n            }\n            // Remove the item when the timeout fires\n            map.delete(key);\n            itemProcessingDeferred.resolve();\n            timerMap.delete(key);\n        };\n        const itemProcessingTimer = setTimeout(expireItem, delay, key);\n        timeItem.processingTimer = itemProcessingTimer;\n        // Don't hold up the process from exiting\n        itemProcessingTimer.unref();\n        return itemProcessingDeferred.promise;\n    });\n    const originalSet = map.set.bind(map);\n    const originalClear = map.clear.bind(map);\n    map.set = (key, value) => {\n        const result = originalSet(key, value);\n        setupTimer([key, value]).catch((err) => {\n            /* istanbul ignore next: should not be hapenning ever */\n            debug(`some error occured while setting timer on key ${key}: ${err.message}`);\n        });\n        return result;\n    };\n    // tslint:disable-next-line: typedef\n    map.clear = () => {\n        for (const [key, timerItem] of timerMap.entries()) {\n            debug(`clearing timer attached to key ${key}`);\n            if (timerItem.processingTimer) {\n                clearTimeout(timerItem.processingTimer);\n            }\n            if (timerItem.processingDeferred) {\n                timerItem.processingDeferred.resolve();\n            }\n        }\n        timerMap.clear();\n        originalClear();\n    };\n    for (const [key, value] of map) {\n        debug(`initial set up of timer on key ${key}`);\n        setupTimer([key, value]).catch((err) => {\n            /* istanbul ignore next: should not be hapenning ever */\n            debug(`some error occured while setting timer on key ${key}: ${err.message}`);\n        });\n    }\n    return map;\n}\nexports.default = mapAgeCleaner;\n// Add support for CJS\nmodule.exports = mapAgeCleaner;\n// tslint:disable-next-line: no-unsafe-any\nmodule.exports.default = mapAgeCleaner;\n"]}