33 lines
996 B
JavaScript
33 lines
996 B
JavaScript
/**
|
|
* In-memory store for the short-lived context of a modal flow, e.g. the branches
|
|
* of `/pr create` which are typed as options but sent with the modal (M5-T04).
|
|
* Entries expire on their own and disappear on restart.
|
|
*/
|
|
const DEFAULT_TTL_MS = 5 * 60_000;
|
|
|
|
const entries = new Map();
|
|
|
|
export function remember(key, value, { ttlMs = DEFAULT_TTL_MS, now = Date.now() } = {}) {
|
|
entries.set(key, { value, expiresAt: now + ttlMs });
|
|
return value;
|
|
}
|
|
|
|
/** Returns the stored value and forgets it, or null when missing or expired. */
|
|
export function consume(key, { now = Date.now() } = {}) {
|
|
const entry = entries.get(key);
|
|
if (!entry) return null;
|
|
entries.delete(key);
|
|
return entry.expiresAt > now ? entry.value : null;
|
|
}
|
|
|
|
export function purgeExpired(now = Date.now()) {
|
|
for (const [key, entry] of entries) {
|
|
if (entry.expiresAt <= now) entries.delete(key);
|
|
}
|
|
return entries.size;
|
|
}
|
|
|
|
export function pendingCount() {
|
|
return entries.size;
|
|
}
|