11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst { 41cb0ef41Sopenharmony_ci FunctionPrototypeBind, 51cb0ef41Sopenharmony_ci SafeMap, 61cb0ef41Sopenharmony_ci} = primordials; 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_ciconst { 91cb0ef41Sopenharmony_ci errnoException, 101cb0ef41Sopenharmony_ci} = require('internal/errors'); 111cb0ef41Sopenharmony_ci 121cb0ef41Sopenharmony_ciconst { signals } = internalBinding('constants').os; 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_cilet Signal; 151cb0ef41Sopenharmony_ciconst signalWraps = new SafeMap(); 161cb0ef41Sopenharmony_ci 171cb0ef41Sopenharmony_cifunction isSignal(event) { 181cb0ef41Sopenharmony_ci return typeof event === 'string' && signals[event] !== undefined; 191cb0ef41Sopenharmony_ci} 201cb0ef41Sopenharmony_ci 211cb0ef41Sopenharmony_ci// Detect presence of a listener for the special signal types 221cb0ef41Sopenharmony_cifunction startListeningIfSignal(type) { 231cb0ef41Sopenharmony_ci if (isSignal(type) && !signalWraps.has(type)) { 241cb0ef41Sopenharmony_ci if (Signal === undefined) 251cb0ef41Sopenharmony_ci Signal = internalBinding('signal_wrap').Signal; 261cb0ef41Sopenharmony_ci const wrap = new Signal(); 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_ci wrap.unref(); 291cb0ef41Sopenharmony_ci 301cb0ef41Sopenharmony_ci wrap.onsignal = FunctionPrototypeBind(process.emit, process, type, type); 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_ci const signum = signals[type]; 331cb0ef41Sopenharmony_ci const err = wrap.start(signum); 341cb0ef41Sopenharmony_ci if (err) { 351cb0ef41Sopenharmony_ci wrap.close(); 361cb0ef41Sopenharmony_ci throw errnoException(err, 'uv_signal_start'); 371cb0ef41Sopenharmony_ci } 381cb0ef41Sopenharmony_ci 391cb0ef41Sopenharmony_ci signalWraps.set(type, wrap); 401cb0ef41Sopenharmony_ci } 411cb0ef41Sopenharmony_ci} 421cb0ef41Sopenharmony_ci 431cb0ef41Sopenharmony_cifunction stopListeningIfSignal(type) { 441cb0ef41Sopenharmony_ci const wrap = signalWraps.get(type); 451cb0ef41Sopenharmony_ci if (wrap !== undefined && process.listenerCount(type) === 0) { 461cb0ef41Sopenharmony_ci wrap.close(); 471cb0ef41Sopenharmony_ci signalWraps.delete(type); 481cb0ef41Sopenharmony_ci } 491cb0ef41Sopenharmony_ci} 501cb0ef41Sopenharmony_ci 511cb0ef41Sopenharmony_cimodule.exports = { 521cb0ef41Sopenharmony_ci startListeningIfSignal, 531cb0ef41Sopenharmony_ci stopListeningIfSignal, 541cb0ef41Sopenharmony_ci}; 55