openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
27 lines (26 loc) • 687 B
JavaScript
//#region src/infra/map-size.ts
/** Prunes a Map in insertion order until it fits the requested maximum size. */
function pruneMapToMaxSize(e, t) {
if (Number.isNaN(t) || t === Number.POSITIVE_INFINITY) return;
const n = Math.max(0, Math.floor(t));
if (n <= 0) {
e.clear();
return;
}
for (const t of e.keys()) {
if (e.size <= n) break;
e.delete(t);
}
}
/** Remembers a key as newest and evicts oldest keys beyond the caller's bound. */
function touchBoundedSet(e, t, n) {
e.delete(t);
e.add(t);
while (e.size > n) {
const t = e.values().next().value;
if (t === void 0) break;
e.delete(t);
}
}
//#endregion
export { touchBoundedSet as n, pruneMapToMaxSize as t };