// src/utils/pendingOffboards.js // // In-memory store for pending offboard confirmation cards. // // We wrap a plain Map so every entry is stamped with a `timestamp` on insert // (callers don't have to remember to set one). A background sweep expires // entries older than TTL_MS to prevent the map from leaking when users // generate offboard cards but never click confirm/cancel. import { logger } from './logger.js'; const TTL_MS = 10 * 60 * 1000; // expire un-acted cards after 10 min const SWEEP_INTERVAL_MS = 60 * 1000; // check once a minute const _store = new Map(); export const pendingOffboards = { set(cardId, data) { _store.set(cardId, { ...data, timestamp: Date.now() }); return this; }, get(cardId) { return _store.get(cardId); }, has(cardId) { return _store.has(cardId); }, delete(cardId) { return _store.delete(cardId); }, get size() { return _store.size; }, entries() { return _store.entries(); }, }; const sweepHandle = setInterval(() => { const now = Date.now(); for (const [cardId, data] of _store.entries()) { const stamped = typeof data?.timestamp === 'number' ? data.timestamp : 0; if (now - stamped > TTL_MS) { logger('offboard:cleanup', `Expired card ${cardId} for ${data?.email || 'unknown'}`); _store.delete(cardId); } } }, SWEEP_INTERVAL_MS); // Don't keep the event loop alive just for this sweep (lets the process exit // cleanly during tests / SIGINT without an explicit clearInterval call). sweepHandle.unref?.();