@@ -57,7 +57,7 @@ const {
57
57
clearAsyncIdStack,
58
58
} = async_wrap ;
59
59
// For performance reasons, only track Promises when a hook is enabled.
60
- const { enablePromiseHook, disablePromiseHook } = async_wrap ;
60
+ const { enablePromiseHook, disablePromiseHook, setPromiseHooks } = async_wrap ;
61
61
// Properties in active_hooks are used to keep track of the set of hooks being
62
62
// executed in case another hook is enabled/disabled. The new set of hooks is
63
63
// then restored once the active set of hooks is finished executing.
@@ -306,71 +306,68 @@ function restoreActiveHooks() {
306
306
active_hooks . tmp_fields = null ;
307
307
}
308
308
309
- function trackPromise ( promise , parent , silent ) {
310
- const asyncId = getOrSetAsyncId ( promise ) ;
309
+ function trackPromise ( promise , parent ) {
310
+ if ( promise [ async_id_symbol ] ) {
311
+ return ;
312
+ }
311
313
314
+ promise [ async_id_symbol ] = newAsyncId ( ) ;
312
315
promise [ trigger_async_id_symbol ] = parent ? getOrSetAsyncId ( parent ) :
313
316
getDefaultTriggerAsyncId ( ) ;
317
+ }
314
318
315
- if ( ! silent && initHooksExist ( ) ) {
316
- const triggerId = promise [ trigger_async_id_symbol ] ;
317
- emitInitScript ( asyncId , 'PROMISE' , triggerId , promise ) ;
318
- }
319
+ function promiseInitHook ( promise , parent ) {
320
+ trackPromise ( promise , parent ) ;
321
+ const asyncId = promise [ async_id_symbol ] ;
322
+ const triggerAsyncId = promise [ trigger_async_id_symbol ] ;
323
+ emitInitScript ( asyncId , 'PROMISE' , triggerAsyncId , promise ) ;
319
324
}
320
325
321
- function fastPromiseHook ( type , promise , parent ) {
322
- if ( type === kInit || ! promise [ async_id_symbol ] ) {
323
- const silent = type !== kInit ;
324
- if ( parent instanceof Promise ) {
325
- trackPromise ( promise , parent , silent ) ;
326
- } else {
327
- trackPromise ( promise , null , silent ) ;
328
- }
326
+ function promiseBeforeHook ( promise ) {
327
+ trackPromise ( promise ) ;
328
+ const asyncId = promise [ async_id_symbol ] ;
329
+ const triggerId = promise [ trigger_async_id_symbol ] ;
330
+ emitBeforeScript ( asyncId , triggerId , promise ) ;
331
+ }
329
332
330
- if ( ! silent ) return ;
333
+ function promiseAfterHook ( promise ) {
334
+ trackPromise ( promise ) ;
335
+ const asyncId = promise [ async_id_symbol ] ;
336
+ if ( hasHooks ( kAfter ) ) {
337
+ emitAfterNative ( asyncId ) ;
331
338
}
339
+ if ( asyncId === executionAsyncId ( ) ) {
340
+ // This condition might not be true if async_hooks was enabled during
341
+ // the promise callback execution.
342
+ // Popping it off the stack can be skipped in that case, because it is
343
+ // known that it would correspond to exactly one call with
344
+ // PromiseHookType::kBefore that was not witnessed by the PromiseHook.
345
+ popAsyncContext ( asyncId ) ;
346
+ }
347
+ }
332
348
349
+ function promiseResolveHook ( promise ) {
350
+ trackPromise ( promise ) ;
333
351
const asyncId = promise [ async_id_symbol ] ;
334
- switch ( type ) {
335
- case kBefore :
336
- const triggerId = promise [ trigger_async_id_symbol ] ;
337
- emitBeforeScript ( asyncId , triggerId , promise ) ;
338
- break ;
339
- case kAfter :
340
- if ( hasHooks ( kAfter ) ) {
341
- emitAfterNative ( asyncId ) ;
342
- }
343
- if ( asyncId === executionAsyncId ( ) ) {
344
- // This condition might not be true if async_hooks was enabled during
345
- // the promise callback execution.
346
- // Popping it off the stack can be skipped in that case, because it is
347
- // known that it would correspond to exactly one call with
348
- // PromiseHookType::kBefore that was not witnessed by the PromiseHook.
349
- popAsyncContext ( asyncId ) ;
350
- }
351
- break ;
352
- case kPromiseResolve :
353
- emitPromiseResolveNative ( asyncId ) ;
354
- break ;
355
- }
352
+ emitPromiseResolveNative ( asyncId ) ;
356
353
}
357
354
358
355
let wantPromiseHook = false ;
359
356
function enableHooks ( ) {
360
357
async_hook_fields [ kCheck ] += 1 ;
361
358
}
362
359
363
- let promiseHookMode = - 1 ;
364
360
function updatePromiseHookMode ( ) {
365
361
wantPromiseHook = true ;
366
362
if ( destroyHooksExist ( ) ) {
367
- if ( promiseHookMode !== 1 ) {
368
- promiseHookMode = 1 ;
369
- enablePromiseHook ( ) ;
370
- }
371
- } else if ( promiseHookMode !== 0 ) {
372
- promiseHookMode = 0 ;
373
- enablePromiseHook ( fastPromiseHook ) ;
363
+ enablePromiseHook ( ) ;
364
+ } else {
365
+ setPromiseHooks (
366
+ initHooksExist ( ) ? promiseInitHook : undefined ,
367
+ promiseBeforeHook ,
368
+ promiseAfterHook ,
369
+ promiseResolveHooksExist ( ) ? promiseResolveHook : undefined ,
370
+ ) ;
374
371
}
375
372
}
376
373
@@ -386,8 +383,8 @@ function disableHooks() {
386
383
387
384
function disablePromiseHookIfNecessary ( ) {
388
385
if ( ! wantPromiseHook ) {
389
- promiseHookMode = - 1 ;
390
386
disablePromiseHook ( ) ;
387
+ setPromiseHooks ( undefined , undefined , undefined , undefined ) ;
391
388
}
392
389
}
393
390
@@ -461,6 +458,10 @@ function destroyHooksExist() {
461
458
return hasHooks ( kDestroy ) ;
462
459
}
463
460
461
+ function promiseResolveHooksExist ( ) {
462
+ return hasHooks ( kPromiseResolve ) ;
463
+ }
464
+
464
465
465
466
function emitInitScript ( asyncId , type , triggerAsyncId , resource ) {
466
467
// Short circuit all checks for the common case. Which is that no hooks have
0 commit comments