decord/source/1163_AsyncExpiringMap.js
2026-08-05 07:35:12 +00:00

205 lines
4.9 KiB
JavaScript

// Module ID: 1163
// Function ID: 1164
// Name: AsyncExpiringMap
// Dependencies: [32, 41, 42]
// Module 1163 (AsyncExpiringMap)
import AsyncExpiringMap from "_slicedToArray";
import _classCallCheck from "_classCallCheck";
class AsyncExpiringMap {
constructor() {
obj = global;
if (global === undefined) {
obj = {};
}
num = obj.cleanupInterval;
if (num === undefined) {
num = 5000;
}
num2 = obj.ttl;
if (num2 === undefined) {
num2 = 2000;
}
tmp = outer1_1(this, AsyncExpiringMap);
this._ttl = num2;
map = new Map();
this._map = map;
this._cleanupIntervalMs = num;
startCleanupResult = this.startCleanup();
return;
}
}
const items = [
{
key: "set",
value: function set(arg0, value) {
let self = this;
self = this;
if (!this._cleanupInterval) {
self.startCleanup();
}
if (typeof value === "object") {
if (value) {
if ("then" in value) {
let obj = { value: "r", expiresAt: "Path", promise: "TRANSPARENT" };
obj[2] = value;
const _map2 = self._map;
const result = _map2.set(arg0, obj);
value.then((value) => {
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;
obj = { value, expiresAt: Date.now() + self._ttl, promise: null };
const result1 = _map.set(arg0, obj);
}
},
{
key: "pop",
value: function pop(arg0) {
const _map = this._map;
const value = this.get(arg0);
_map.delete(arg0);
return value;
}
},
{
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;
}
}
},
{
key: "has",
value: function has(arg0) {
const _map = this._map;
const value = _map.get(arg0);
let tmp2 = value;
if (tmp2) {
let promise = value.promise;
if (!promise) {
let expiresAt = value.expiresAt;
if (expiresAt) {
const _Date = Date;
expiresAt = value.expiresAt <= Date.now();
}
let flag = !expiresAt;
if (expiresAt) {
const _map2 = this._map;
_map2.delete(arg0);
flag = false;
}
promise = flag;
}
tmp2 = promise;
}
return tmp2;
}
},
{
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;
}
}
},
{
key: "cleanup",
value: function cleanup() {
let tmp7;
let tmp8;
const self = this;
const _map = this._map;
const timestamp = Date.now();
const entries = _map.entries();
while (tmp3 !== undefined) {
let tmp5 = AsyncExpiringMap;
let tmp6 = AsyncExpiringMap(tmp4, 2);
[tmp7, tmp8] = tmp6;
let expiresAt = tmp8.expiresAt;
if (expiresAt) {
let tmp10 = tmp8;
expiresAt = tmp9.expiresAt <= timestamp;
}
if (expiresAt) {
let _map2 = self._map;
let tmp11 = tmp7;
let deleteResult = _map2.delete(tmp7);
}
continue;
}
if (!self._map.size) {
self.stopCleanup();
}
}
},
{
key: "clear",
value: function clear() {
const self = this;
if (this._cleanupInterval) {
const _clearInterval = clearInterval;
clearInterval(self._cleanupInterval);
}
const _map = self._map;
_map.clear();
}
},
{
key: "stopCleanup",
value: function stopCleanup() {
if (this._cleanupInterval) {
const _clearInterval = clearInterval;
clearInterval(tmp._cleanupInterval);
}
}
},
{
key: "startCleanup",
value: function startCleanup() {
const self = this;
this._cleanupInterval = setInterval(() => self.cleanup(), this._cleanupIntervalMs);
}
}
];
export const AsyncExpiringMap = require("_createClass")(AsyncExpiringMap, items);