@@ -20,7 +20,7 @@ const processOk = (process) => !!process &&
2020const kExitEmitter = Symbol . for ( 'signal-exit emitter' ) ;
2121const global = globalThis ;
2222const ObjectDefineProperty = Object . defineProperty . bind ( Object ) ;
23- // teeny tiny ee
23+ // teeny special purpose ee
2424class Emitter {
2525 emitted = {
2626 afterExit : false ,
@@ -63,12 +63,17 @@ class Emitter {
6363 }
6464 emit ( ev , code , signal ) {
6565 if ( this . emitted [ ev ] ) {
66- return ;
66+ return false ;
6767 }
6868 this . emitted [ ev ] = true ;
69+ let ret = false ;
6970 for ( const fn of this . listeners [ ev ] ) {
70- fn ( code , signal ) ;
71+ ret = fn ( code , signal ) === true || ret ;
72+ }
73+ if ( ev === 'exit' ) {
74+ ret = this . emit ( 'afterExit' , code , signal ) || ret ;
7175 }
76+ return ret ;
7277 }
7378}
7479class SignalExitBase {
@@ -122,18 +127,22 @@ class SignalExit extends SignalExitBase {
122127 // exit v4 are not aware of each other, and each will attempt to let
123128 // the other handle it, so neither of them do. To correct this, we
124129 // detect if we're the only handler *except* for previous versions
125- // of signal-exit.
130+ // of signal-exit, and increment by the count of listeners it has
131+ // created.
126132 /* c8 ignore start */
127- //@ts -ignore
128- if ( typeof process . __signal_exit_emitter__ === 'object' )
129- count ++ ;
133+ const p = process ;
134+ if ( typeof p . __signal_exit_emitter__ === 'object' &&
135+ typeof p . __signal_exit_emitter__ . count === 'number' ) {
136+ count += p . __signal_exit_emitter__ . count ;
137+ }
130138 /* c8 ignore stop */
131139 if ( listeners . length === count ) {
132140 this . unload ( ) ;
133- this . #emitter. emit ( 'exit' , null , sig ) ;
134- this . #emitter. emit ( 'afterExit' , null , sig ) ;
141+ const ret = this . #emitter. emit ( 'exit' , null , sig ) ;
135142 /* c8 ignore start */
136- process . kill ( process . pid , sig === 'SIGHUP' ? this . #hupSig : sig ) ;
143+ const s = sig === 'SIGHUP' ? this . #hupSig : sig ;
144+ if ( ! ret )
145+ process . kill ( process . pid , s ) ;
137146 /* c8 ignore stop */
138147 }
139148 } ;
@@ -216,7 +225,6 @@ class SignalExit extends SignalExitBase {
216225 this . #process. exitCode = code || 0 ;
217226 /* c8 ignore stop */
218227 this . #emitter. emit ( 'exit' , this . #process. exitCode , null ) ;
219- this . #emitter. emit ( 'afterExit' , this . #process. exitCode , null ) ;
220228 return this . #originalProcessReallyExit. call ( this . #process, this . #process. exitCode ) ;
221229 }
222230 #processEmit( ev , ...args ) {
@@ -230,7 +238,6 @@ class SignalExit extends SignalExitBase {
230238 const ret = og . call ( this . #process, ev , ...args ) ;
231239 /* c8 ignore start */
232240 this . #emitter. emit ( 'exit' , this . #process. exitCode , null ) ;
233- this . #emitter. emit ( 'afterExit' , this . #process. exitCode , null ) ;
234241 /* c8 ignore stop */
235242 return ret ;
236243 }
0 commit comments