@@ -214,17 +214,7 @@ class AsyncResource {
214
214
if ( async_hook_fields [ kInit ] === 0 )
215
215
return ;
216
216
217
- processing_hook = true ;
218
- for ( var i = 0 ; i < active_hooks_array . length ; i ++ ) {
219
- if ( typeof active_hooks_array [ i ] [ init_symbol ] === 'function' ) {
220
- runInitCallback ( active_hooks_array [ i ] [ init_symbol ] ,
221
- this [ async_id_symbol ] ,
222
- type ,
223
- triggerId ,
224
- this ) ;
225
- }
226
- }
227
- processing_hook = false ;
217
+ init ( this [ async_id_symbol ] , type , triggerId , this ) ;
228
218
}
229
219
230
220
emitBefore ( ) {
@@ -321,14 +311,7 @@ function emitInitS(asyncId, type, triggerId, resource) {
321
311
if ( ! Number . isSafeInteger ( triggerId ) || triggerId < 0 )
322
312
throw new RangeError ( 'triggerId must be an unsigned integer' ) ;
323
313
324
- processing_hook = true ;
325
- for ( var i = 0 ; i < active_hooks_array . length ; i ++ ) {
326
- if ( typeof active_hooks_array [ i ] [ init_symbol ] === 'function' ) {
327
- runInitCallback (
328
- active_hooks_array [ i ] [ init_symbol ] , asyncId , type , triggerId , resource ) ;
329
- }
330
- }
331
- processing_hook = false ;
314
+ init ( asyncId , type , triggerId , resource ) ;
332
315
333
316
// Isn't null if hooks were added/removed while the hooks were running.
334
317
if ( tmp_active_hooks_array !== null ) {
@@ -339,10 +322,15 @@ function emitInitS(asyncId, type, triggerId, resource) {
339
322
340
323
function emitBeforeN ( asyncId ) {
341
324
processing_hook = true ;
342
- for ( var i = 0 ; i < active_hooks_array . length ; i ++ ) {
343
- if ( typeof active_hooks_array [ i ] [ before_symbol ] === 'function' ) {
344
- runCallback ( active_hooks_array [ i ] [ before_symbol ] , asyncId ) ;
325
+ // Use a single try/catch for all hook to avoid setting up one per iteration.
326
+ try {
327
+ for ( var i = 0 ; i < active_hooks_array . length ; i ++ ) {
328
+ if ( typeof active_hooks_array [ i ] [ before_symbol ] === 'function' ) {
329
+ active_hooks_array [ i ] [ before_symbol ] ( asyncId ) ;
330
+ }
345
331
}
332
+ } catch ( e ) {
333
+ fatalError ( e ) ;
346
334
}
347
335
processing_hook = false ;
348
336
@@ -366,26 +354,27 @@ function emitBeforeS(asyncId, triggerId = asyncId) {
366
354
367
355
pushAsyncIds ( asyncId , triggerId ) ;
368
356
369
- if ( async_hook_fields [ kBefore ] === 0 ) {
357
+ if ( async_hook_fields [ kBefore ] === 0 )
370
358
return ;
371
- }
372
-
373
359
emitBeforeN ( asyncId ) ;
374
360
}
375
361
376
362
377
363
// Called from native. The asyncId stack handling is taken care of there before
378
364
// this is called.
379
365
function emitAfterN ( asyncId ) {
380
- if ( async_hook_fields [ kAfter ] > 0 ) {
381
- processing_hook = true ;
366
+ processing_hook = true ;
367
+ // Use a single try/catch for all hook to avoid setting up one per iteration.
368
+ try {
382
369
for ( var i = 0 ; i < active_hooks_array . length ; i ++ ) {
383
370
if ( typeof active_hooks_array [ i ] [ after_symbol ] === 'function' ) {
384
- runCallback ( active_hooks_array [ i ] [ after_symbol ] , asyncId ) ;
371
+ active_hooks_array [ i ] [ after_symbol ] ( asyncId ) ;
385
372
}
386
373
}
387
- processing_hook = false ;
374
+ } catch ( e ) {
375
+ fatalError ( e ) ;
388
376
}
377
+ processing_hook = false ;
389
378
390
379
if ( tmp_active_hooks_array !== null ) {
391
380
restoreTmpHooks ( ) ;
@@ -397,7 +386,9 @@ function emitAfterN(asyncId) {
397
386
// kIdStackIndex. But what happens if the user doesn't have both before and
398
387
// after callbacks.
399
388
function emitAfterS ( asyncId ) {
400
- emitAfterN ( asyncId ) ;
389
+ if ( async_hook_fields [ kAfter ] > 0 )
390
+ emitAfterN ( asyncId ) ;
391
+
401
392
popAsyncIds ( asyncId ) ;
402
393
}
403
394
@@ -413,10 +404,15 @@ function emitDestroyS(asyncId) {
413
404
414
405
function emitDestroyN ( asyncId ) {
415
406
processing_hook = true ;
416
- for ( var i = 0 ; i < active_hooks_array . length ; i ++ ) {
417
- if ( typeof active_hooks_array [ i ] [ destroy_symbol ] === 'function' ) {
418
- runCallback ( active_hooks_array [ i ] [ destroy_symbol ] , asyncId ) ;
407
+ // Use a single try/catch for all hook to avoid setting up one per iteration.
408
+ try {
409
+ for ( var i = 0 ; i < active_hooks_array . length ; i ++ ) {
410
+ if ( typeof active_hooks_array [ i ] [ destroy_symbol ] === 'function' ) {
411
+ active_hooks_array [ i ] [ destroy_symbol ] ( asyncId ) ;
412
+ }
419
413
}
414
+ } catch ( e ) {
415
+ fatalError ( e ) ;
420
416
}
421
417
processing_hook = false ;
422
418
@@ -434,41 +430,24 @@ function emitDestroyN(asyncId) {
434
430
// init().
435
431
// TODO(trevnorris): Perhaps have MakeCallback call a single JS function that
436
432
// does the before/callback/after calls to remove two additional calls to JS.
437
- function init ( asyncId , type , resource , triggerId ) {
438
- processing_hook = true ;
439
- for ( var i = 0 ; i < active_hooks_array . length ; i ++ ) {
440
- if ( typeof active_hooks_array [ i ] [ init_symbol ] === 'function' ) {
441
- runInitCallback (
442
- active_hooks_array [ i ] [ init_symbol ] , asyncId , type , triggerId , resource ) ;
443
- }
444
- }
445
- processing_hook = false ;
446
- }
447
-
448
-
449
- // Generalized callers for all callbacks that handles error handling.
450
433
451
- // If either runInitCallback() or runCallback() throw then force the
452
- // application to shutdown if one of the callbacks throws. This may change in
453
- // the future depending on whether it can be determined if there's a slim
454
- // chance of the application remaining stable after handling one of these
434
+ // Force the application to shutdown if one of the callbacks throws. This may
435
+ // change in the future depending on whether it can be determined if there's a
436
+ // slim chance of the application remaining stable after handling one of these
455
437
// exceptions.
456
-
457
- function runInitCallback ( cb , asyncId , type , triggerId , resource ) {
458
- try {
459
- cb ( asyncId , type , triggerId , resource ) ;
460
- } catch ( e ) {
461
- fatalError ( e ) ;
462
- }
463
- }
464
-
465
-
466
- function runCallback ( cb , asyncId ) {
438
+ function init ( asyncId , type , triggerId , resource ) {
439
+ processing_hook = true ;
440
+ // Use a single try/catch for all hook to avoid setting up one per iteration.
467
441
try {
468
- cb ( asyncId ) ;
442
+ for ( var i = 0 ; i < active_hooks_array . length ; i ++ ) {
443
+ if ( typeof active_hooks_array [ i ] [ init_symbol ] === 'function' ) {
444
+ active_hooks_array [ i ] [ init_symbol ] ( asyncId , type , triggerId , resource ) ;
445
+ }
446
+ }
469
447
} catch ( e ) {
470
448
fatalError ( e ) ;
471
449
}
450
+ processing_hook = false ;
472
451
}
473
452
474
453
0 commit comments