interface ThreadDismissal { readonly finished: Promise; readonly restore: () => void; } const rows = new Map ThreadDismissal>>(); export function registerThreadDismissal(key: string, dismiss: () => ThreadDismissal) { const registrations = rows.get(key) ?? new Set(); registrations.add(dismiss); rows.set(key, registrations); return () => { registrations.delete(dismiss); if (registrations.size === 0) rows.delete(key); }; } export async function withThreadDismissal( key: string, action: () => Promise, succeeded: (result: T) => boolean, ): Promise { const dismissals = Array.from(rows.get(key) ?? [], (dismiss) => dismiss()); let committed = false; try { await Promise.all(dismissals.map(({ finished }) => finished)); const result = await action(); committed = succeeded(result); return result; } finally { if (!committed) dismissals.forEach(({ restore }) => restore()); } }