decord/source/1163_AsyncExpiringMap.js

205 lines
4.9 KiB
JavaScript
Raw Normal View History

2026-07-29 08:29:29 +00:00
// Module ID: 1163
// Function ID: 1164
2026-07-20 16:05:17 +00:00
// Name: AsyncExpiringMap
2026-07-29 08:29:29 +00:00
// Dependencies: [32, 41, 42]
2026-07-20 16:05:17 +00:00
2026-07-29 08:29:29 +00:00
// Module 1163 (AsyncExpiringMap)
2026-08-24 06:08:38 +00:00
import _createClassDefault from "_createClass" /* 42 */;
import AsyncExpiringMap from "_slicedToArray" /* 32 */;
import closure_1 from "_classCallCheck" /* 41 */;
2026-07-20 16:05:17 +00:00
2026-07-29 08:29:29 +00:00
class AsyncExpiringMap {
constructor() {
obj = global;
if (global === undefined) {
obj = {};
}
num = obj.cleanupInterval;
if (num === undefined) {
num = 5000;
2026-07-20 16:05:17 +00:00
}
2026-07-29 08:29:29 +00:00
num2 = obj.ttl;
if (num2 === undefined) {
num2 = 2000;
}
2026-08-24 06:08:38 +00:00
tmp = closure_1(this, AsyncExpiringMap);
2026-07-29 08:29:29 +00:00
this._ttl = num2;
map = new Map();
this._map = map;
this._cleanupIntervalMs = num;
startCleanupResult = this.startCleanup();
return;
2026-07-20 16:05:17 +00:00
}
2026-07-29 08:29:29 +00:00
}
const items = [
{
2026-07-20 16:05:17 +00:00
key: "set",
2026-08-04 21:03:28 +00:00
value: function set(arg0, value) {
2026-07-23 10:11:24 +00:00
let self = this;
self = this;
2026-07-20 16:05:17 +00:00
if (!this._cleanupInterval) {
self.startCleanup();
}
2026-08-04 21:03:28 +00:00
if (typeof value === "object") {
if (value) {
if ("then" in value) {
2026-08-08 06:10:11 +00:00
let obj = { value: "r", expiresAt: "PX_16", promise: "TRANSPARENT" };
2026-08-04 21:03:28 +00:00
obj[2] = value;
2026-07-20 16:05:17 +00:00
const _map2 = self._map;
const result = _map2.set(arg0, obj);
2026-08-04 21:03:28 +00:00
value.then((value) => {
2026-07-20 16:05:17 +00:00
obj.value = value;
obj.expiresAt = Date.now() + self._ttl;
obj.promise = null;
}, () => {
obj.expiresAt = Date.now() + self._ttl;
obj.promise = null;
});
}
}
}
const _map = self._map;
2026-08-04 21:03:28 +00:00
obj = { value, expiresAt: Date.now() + self._ttl, promise: null };
2026-07-20 16:05:17 +00:00
const result1 = _map.set(arg0, obj);
}
2026-07-29 08:29:29 +00:00
},
{
2026-07-20 16:05:17 +00:00
key: "pop",
value: function pop(arg0) {
const _map = this._map;
const value = this.get(arg0);
_map.delete(arg0);
return value;
}
2026-07-29 08:29:29 +00:00
},
{
2026-07-20 16:05:17 +00:00
key: "get",
value: function get(arg0) {
const _map = this._map;
const iter = _map.get(arg0);
if (iter) {
if (iter.promise) {
let value = iter.promise;
} else {
let expiresAt = iter.expiresAt;
if (expiresAt) {
const _Date = Date;
expiresAt = iter.expiresAt <= Date.now();
}
if (expiresAt) {
const _map2 = this._map;
_map2.delete(arg0);
}
value = iter.value;
}
return value;
}
}
2026-07-29 08:29:29 +00:00
},
{
2026-07-20 16:05:17 +00:00
key: "has",
value: function has(arg0) {
const _map = this._map;
const value = _map.get(arg0);
2026-07-29 08:29:29 +00:00
let tmp2 = value;
if (tmp2) {
let promise = value.promise;
if (!promise) {
2026-07-20 16:05:17 +00:00
let expiresAt = value.expiresAt;
if (expiresAt) {
const _Date = Date;
expiresAt = value.expiresAt <= Date.now();
}
let flag = !expiresAt;
2026-07-29 08:29:29 +00:00
if (expiresAt) {
2026-07-20 16:05:17 +00:00
const _map2 = this._map;
_map2.delete(arg0);
flag = false;
}
2026-07-29 08:29:29 +00:00
promise = flag;
2026-07-20 16:05:17 +00:00
}
2026-07-29 08:29:29 +00:00
tmp2 = promise;
2026-07-20 16:05:17 +00:00
}
2026-07-29 08:29:29 +00:00
return tmp2;
2026-07-20 16:05:17 +00:00
}
2026-07-29 08:29:29 +00:00
},
{
2026-07-20 16:05:17 +00:00
key: "ttl",
value: function ttl(arg0) {
const _map = this._map;
const value = _map.get(arg0);
let expiresAt;
if (null != value) {
expiresAt = value.expiresAt;
}
if (expiresAt) {
const _Date = Date;
const diff = value.expiresAt - Date.now();
let num = 0;
if (diff > 0) {
num = diff;
}
return num;
}
}
2026-07-29 08:29:29 +00:00
},
{
2026-07-20 16:05:17 +00:00
key: "cleanup",
value: function cleanup() {
const self = this;
const _map = this._map;
2026-07-29 08:29:29 +00:00
const timestamp = Date.now();
2026-07-20 16:05:17 +00:00
const entries = _map.entries();
while (tmp3 !== undefined) {
let tmp5 = AsyncExpiringMap;
let tmp6 = AsyncExpiringMap(tmp4, 2);
[tmp7, tmp8] = tmp6;
let expiresAt = tmp8.expiresAt;
if (expiresAt) {
2026-07-29 08:29:29 +00:00
let tmp10 = tmp8;
expiresAt = tmp9.expiresAt <= timestamp;
2026-07-20 16:05:17 +00:00
}
2026-07-29 08:29:29 +00:00
if (expiresAt) {
2026-07-20 16:05:17 +00:00
let _map2 = self._map;
2026-07-29 08:29:29 +00:00
let tmp11 = tmp7;
2026-07-20 16:05:17 +00:00
let deleteResult = _map2.delete(tmp7);
}
2026-07-23 10:11:24 +00:00
continue;
2026-07-20 16:05:17 +00:00
}
if (!self._map.size) {
self.stopCleanup();
}
}
2026-07-29 08:29:29 +00:00
},
{
2026-07-20 16:05:17 +00:00
key: "clear",
value: function clear() {
const self = this;
if (this._cleanupInterval) {
const _clearInterval = clearInterval;
clearInterval(self._cleanupInterval);
}
const _map = self._map;
_map.clear();
}
2026-07-29 08:29:29 +00:00
},
{
2026-07-20 16:05:17 +00:00
key: "stopCleanup",
value: function stopCleanup() {
if (this._cleanupInterval) {
const _clearInterval = clearInterval;
clearInterval(tmp._cleanupInterval);
}
}
2026-07-29 08:29:29 +00:00
},
{
2026-07-20 16:05:17 +00:00
key: "startCleanup",
value: function startCleanup() {
2026-07-23 10:11:24 +00:00
const self = this;
2026-07-20 16:05:17 +00:00
this._cleanupInterval = setInterval(() => self.cleanup(), this._cleanupIntervalMs);
}
2026-07-29 08:29:29 +00:00
}
];
2026-08-24 06:08:38 +00:00
export const AsyncExpiringMap = _createClassDefault(AsyncExpiringMap, items);