sigIntWatcher.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. "use strict";
  2. var __defProp = Object.defineProperty;
  3. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  4. var __getOwnPropNames = Object.getOwnPropertyNames;
  5. var __hasOwnProp = Object.prototype.hasOwnProperty;
  6. var __export = (target, all) => {
  7. for (var name in all)
  8. __defProp(target, name, { get: all[name], enumerable: true });
  9. };
  10. var __copyProps = (to, from, except, desc) => {
  11. if (from && typeof from === "object" || typeof from === "function") {
  12. for (let key of __getOwnPropNames(from))
  13. if (!__hasOwnProp.call(to, key) && key !== except)
  14. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  15. }
  16. return to;
  17. };
  18. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  19. var sigIntWatcher_exports = {};
  20. __export(sigIntWatcher_exports, {
  21. SigIntWatcher: () => SigIntWatcher
  22. });
  23. module.exports = __toCommonJS(sigIntWatcher_exports);
  24. class SigIntWatcher {
  25. constructor() {
  26. this._hadSignal = false;
  27. let sigintCallback;
  28. this._sigintPromise = new Promise((f) => sigintCallback = f);
  29. this._sigintHandler = () => {
  30. FixedNodeSIGINTHandler.off(this._sigintHandler);
  31. this._hadSignal = true;
  32. sigintCallback();
  33. };
  34. FixedNodeSIGINTHandler.on(this._sigintHandler);
  35. }
  36. promise() {
  37. return this._sigintPromise;
  38. }
  39. hadSignal() {
  40. return this._hadSignal;
  41. }
  42. disarm() {
  43. FixedNodeSIGINTHandler.off(this._sigintHandler);
  44. }
  45. }
  46. class FixedNodeSIGINTHandler {
  47. static {
  48. this._handlers = [];
  49. }
  50. static {
  51. this._ignoreNextSIGINTs = false;
  52. }
  53. static {
  54. this._handlerInstalled = false;
  55. }
  56. static {
  57. this._dispatch = () => {
  58. if (this._ignoreNextSIGINTs)
  59. return;
  60. this._ignoreNextSIGINTs = true;
  61. setTimeout(() => {
  62. this._ignoreNextSIGINTs = false;
  63. if (!this._handlers.length)
  64. this._uninstall();
  65. }, 1e3);
  66. for (const handler of this._handlers)
  67. handler();
  68. };
  69. }
  70. static _install() {
  71. if (!this._handlerInstalled) {
  72. this._handlerInstalled = true;
  73. process.on("SIGINT", this._dispatch);
  74. }
  75. }
  76. static _uninstall() {
  77. if (this._handlerInstalled) {
  78. this._handlerInstalled = false;
  79. process.off("SIGINT", this._dispatch);
  80. }
  81. }
  82. static on(handler) {
  83. this._handlers.push(handler);
  84. if (this._handlers.length === 1)
  85. this._install();
  86. }
  87. static off(handler) {
  88. this._handlers = this._handlers.filter((h) => h !== handler);
  89. if (!this._ignoreNextSIGINTs && !this._handlers.length)
  90. this._uninstall();
  91. }
  92. }
  93. // Annotate the CommonJS export names for ESM import in node:
  94. 0 && (module.exports = {
  95. SigIntWatcher
  96. });