UNPKG

nstdlib-nightly

Version:

Node.js standard library converted to runtime-agnostic ES modules.

50 lines (38 loc) 1.22 kB
// Source: https://github.com/nodejs/node/blob/65eff1eb/lib/internal/process/signal.js import { ErrnoException } from "nstdlib/lib/internal/errors"; const { signals } = require("binding/constants").os; let Signal; const signalWraps = new Map(); function isSignal(event) { return typeof event === "string" && signals[event] !== undefined; } // Detect presence of a listener for the special signal types function startListeningIfSignal(type) { if (isSignal(type) && !signalWraps.has(type)) { if (Signal === undefined) Signal = require("binding/signal_wrap").Signal; const wrap = new Signal(); wrap.unref(); wrap.onsignal = Function.prototype.bind.call( process.emit, process, type, type, ); const signum = signals[type]; const err = wrap.start(signum); if (err) { wrap.close(); throw new ErrnoException(err, "uv_signal_start"); } signalWraps.set(type, wrap); } } function stopListeningIfSignal(type) { const wrap = signalWraps.get(type); if (wrap !== undefined && process.listenerCount(type) === 0) { wrap.close(); signalWraps.delete(type); } } export { startListeningIfSignal }; export { stopListeningIfSignal };