Skip to content

Commit 751cd24

Browse files
author
Stephen Belanger
committed
async_hooks: eliminate native PromiseHook
1 parent 84d6ce9 commit 751cd24

File tree

6 files changed

+33
-449
lines changed

6 files changed

+33
-449
lines changed

lib/internal/async_hooks.js

+32-14
Original file line numberDiff line numberDiff line change
@@ -307,9 +307,13 @@ function trackPromise(promise, parent) {
307307
return;
308308
}
309309

310-
promise[async_id_symbol] = newAsyncId();
311-
promise[trigger_async_id_symbol] = parent ? getOrSetAsyncId(parent) :
310+
// Get trigger id from parent async id before making the async id of the
311+
// child so if a new one must be made it will be lower than the child.
312+
const triggerAsyncId = parent ? getOrSetAsyncId(parent) :
312313
getDefaultTriggerAsyncId();
314+
315+
promise[async_id_symbol] = newAsyncId();
316+
promise[trigger_async_id_symbol] = triggerAsyncId;
313317
}
314318

315319
function promiseInitHook(promise, parent) {
@@ -319,6 +323,21 @@ function promiseInitHook(promise, parent) {
319323
emitInitScript(asyncId, 'PROMISE', triggerAsyncId, promise);
320324
}
321325

326+
function promiseInitHookWithDestroyTracking(promise, parent) {
327+
promiseInitHook(promise, parent);
328+
destroyTracking(promise, parent);
329+
}
330+
331+
const destroyedSymbol = Symbol('destroyed');
332+
333+
function destroyTracking(promise, parent) {
334+
trackPromise(promise, parent);
335+
const asyncId = promise[async_id_symbol];
336+
const destroyed = { destroyed: false };
337+
promise[destroyedSymbol] = destroyed;
338+
registerDestroyHook(promise, asyncId, destroyed);
339+
}
340+
322341
function promiseBeforeHook(promise) {
323342
trackPromise(promise);
324343
const asyncId = promise[async_id_symbol];
@@ -355,18 +374,18 @@ function enableHooks() {
355374

356375
function updatePromiseHookMode() {
357376
wantPromiseHook = true;
358-
if (destroyHooksExist()) {
359-
enablePromiseHook();
360-
setPromiseHooks(undefined, undefined, undefined, undefined);
361-
} else {
362-
disablePromiseHook();
363-
setPromiseHooks(
364-
initHooksExist() ? promiseInitHook : undefined,
365-
promiseBeforeHook,
366-
promiseAfterHook,
367-
promiseResolveHooksExist() ? promiseResolveHook : undefined,
368-
);
377+
let initHook = undefined;
378+
if (initHooksExist()) {
379+
initHook = destroyHooksExist() ? promiseInitHookWithDestroyTracking : promiseInitHook;
380+
} else if (destroyHooksExist()) {
381+
initHook = destroyTracking;
369382
}
383+
setPromiseHooks(
384+
initHook,
385+
promiseBeforeHook,
386+
promiseAfterHook,
387+
promiseResolveHooksExist() ? promiseResolveHook : undefined,
388+
);
370389
}
371390

372391
function disableHooks() {
@@ -381,7 +400,6 @@ function disableHooks() {
381400

382401
function disablePromiseHookIfNecessary() {
383402
if (!wantPromiseHook) {
384-
disablePromiseHook();
385403
setPromiseHooks(undefined, undefined, undefined, undefined);
386404
}
387405
}

0 commit comments

Comments
 (0)