{
  "version": 3,
  "sources": ["../src/main.ts"],
  "sourcesContent": ["/**\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": ";;qqBAAA,6GAGA,IAAI,UAAmC,KAKvC,IAAI,QAAU,MAgBP,SAAS,SAAS,SAA4B,CACnD,GAAI,YAAc,KAAM,CACtB,mBAAmB,EACnB,UAAY,CAAC,CACf,CACA,UAAU,KAAK,QAAQ,CACzB,CAKA,SAAS,QAAQ,OAAuC,CACtD,QAAQ,IAAI,uBAAwB,MAAM,EAC1C,GAAI,UAAY,MAAQ,YAAc,KAAM,OAE5C,QAAU,KAEV,UAAW,YAAY,UAAW,CAChC,GAAI,CACF,SAAS,CACX,OACO,MAAO,CACZ,QAAQ,MAAM,+BAAgC,KAAK,CACrD,CACF,CAEA,GAAI,SAAW,UAAY,SAAW,UAAW,CAC/C,WAAW,IAAM,CACf,QAAQ,KAAK,CAAC,CAChB,CAAC,CACH,CACF,CAKA,SAAS,oBAA2B,CAiBlC,QAAQ,KAAK,OAAQ,OAAO,EAO5B,QAAQ,KAAK,UAAW,OAAO,EAO/B,QAAQ,KAAK,SAAU,OAAO,CAChC",
  "names": []
}
