on_shutdown.ts (575B)
1 const shutdownTasks: (() => void)[] = []; 2 3 /** 4 * Register a callback to be run during program shutdown (triggered, e.g., by the 'beforeunload' 5 * event when the webpage is being closed). 6 * 7 * Note such tasks should be synchronous functions; otherwise, they probably won't complete. 8 */ 9 export function registerShutdownTask(task: () => void) { 10 shutdownTasks.push(task); 11 } 12 13 /** Run all shutdown tasks. Should only be called during program shutdown. */ 14 export function runShutdownTasks() { 15 for (const task of shutdownTasks) { 16 task(); 17 } 18 shutdownTasks.length = 0; 19 }