@alwatr/exit-hook
Version:
A utility for registering exit handlers in Node.js.
8 lines (7 loc) • 3.38 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../src/main.ts"],
"sourcesContent": ["import {packageTracer} from '@alwatr/package-tracer';\n\n__dev_mode__: packageTracer.add(__package_name__, __package_version__);\n\n/**\n * Array of callback functions to be called when the process is exiting.\n */\nlet callbacks: (() => void)[] | null = null;\n\n/**\n * True whether the process is exiting to prevent calling the callbacks more than once.\n */\nlet exiting = false;\n\n/**\n * Add a callback function to be called when the process is exiting.\n *\n * @param callback The callback function to be called when the process is exiting.\n *\n * @example\n * ```typescript\n * const saveAllData = () => {\n * // save all data\n * };\n *\n * existHook(saveAllData);\n * ```\n */\nexport function exitHook(callback: () => void): void {\n if (callbacks === null) {\n registerExitEvents();\n callbacks = [];\n }\n callbacks.push(callback);\n}\n\n/**\n * A once callback to be called on process exit event.\n */\nfunction onExit_(signal: number | 'SIGINT' | 'SIGTERM') {\n console.log('onExit({signal: %s})', signal);\n if (exiting === true || callbacks === null) return;\n\n exiting = true;\n\n for (const callback of callbacks) {\n try {\n callback();\n }\n catch (error) {\n console.error('Error in exit hook callback:', error);\n }\n }\n\n if (signal === 'SIGINT' || signal === 'SIGTERM') {\n setTimeout(() => {\n process.exit(0);\n });\n }\n}\n\n/**\n * Register process exit events.\n */\nfunction registerExitEvents(): void {\n /**\n * This event emitted when Node.js empties its event loop and has no additional work to schedule.\n * Normally, the Node.js process will exit when there is no work scheduled,\n * but a listener registered on the 'beforeExit' event can make **asynchronous calls**, and thereby cause the Node.js process to continue.\n *\n * @see https://nodejs.org/api/process.html#event-beforeexit\n */\n // process.once('beforeExit', onExit_);\n\n /**\n * This event is emitted when the Node.js process is about to exit as a result of either:\n * 1- The `process.exit()` method being called explicitly.\n * 2- The Node.js event loop no longer having any additional work to perform.\n *\n * @see https://nodejs.org/api/process.html#event-exit\n */\n process.once('exit', onExit_);\n\n /**\n * This event is emitted in terminal mode before exiting with code 128 + signal number.\n *\n * @see https://nodejs.org/api/process.html#signal-events\n */\n process.once('SIGTERM', onExit_);\n\n /**\n * This event is emitted when `Ctrl+C` is pressed.\n *\n * @see https://nodejs.org/api/process.html#signal-events\n */\n process.once('SIGINT', onExit_);\n}\n"],
"mappings": ";;;AAAA,SAAQ,qBAAoB;AAE5B,aAAc,eAAc,IAAI,qBAAkB,OAAmB;AAKrE,IAAI,YAAmC;AAKvC,IAAI,UAAU;AAgBP,SAAS,SAAS,UAA4B;AACnD,MAAI,cAAc,MAAM;AACtB,uBAAmB;AACnB,gBAAY,CAAC;AAAA,EACf;AACA,YAAU,KAAK,QAAQ;AACzB;AAKA,SAAS,QAAQ,QAAuC;AACtD,UAAQ,IAAI,wBAAwB,MAAM;AAC1C,MAAI,YAAY,QAAQ,cAAc,KAAM;AAE5C,YAAU;AAEV,aAAW,YAAY,WAAW;AAChC,QAAI;AACF,eAAS;AAAA,IACX,SACO,OAAO;AACZ,cAAQ,MAAM,gCAAgC,KAAK;AAAA,IACrD;AAAA,EACF;AAEA,MAAI,WAAW,YAAY,WAAW,WAAW;AAC/C,eAAW,MAAM;AACf,cAAQ,KAAK,CAAC;AAAA,IAChB,CAAC;AAAA,EACH;AACF;AAKA,SAAS,qBAA2B;AAiBlC,UAAQ,KAAK,QAAQ,OAAO;AAO5B,UAAQ,KAAK,WAAW,OAAO;AAO/B,UAAQ,KAAK,UAAU,OAAO;AAChC;",
"names": []
}