mirror of
https://github.com/nodejs/node.git
synced 2025-08-15 21:58:48 +02:00
async_wrap: use kTotals to enable PromiseHook
Keep a total of enabled hook callbacks in kTotals. This value is used to track whether node::PromiseHook (src/async-wrap.cc) should be enabled or disabled. Don't enable node::PromiseHook, using enablePromiseHook(), until a hook has been added. Then, using disablePromiseHook(), disable node::PromiseHook when all hooks have been disabled. Need to use a native test in order to check the internal field of the Promise and check for a PromiseWrap. PR-URL: https://github.com/nodejs/node/pull/13509 Reviewed-By: Andreas Madsen <amwebdk@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
This commit is contained in:
parent
cc2dd93ea5
commit
a2fdb76677
7 changed files with 177 additions and 12 deletions
|
@ -38,8 +38,8 @@ var tmp_async_hook_fields = null;
|
|||
// Each constant tracks how many callbacks there are for any given step of
|
||||
// async execution. These are tracked so if the user didn't include callbacks
|
||||
// for a given step, that step can bail out early.
|
||||
const { kInit, kBefore, kAfter, kDestroy, kCurrentAsyncId, kCurrentTriggerId,
|
||||
kAsyncUidCntr, kInitTriggerId } = async_wrap.constants;
|
||||
const { kInit, kBefore, kAfter, kDestroy, kTotals, kCurrentAsyncId,
|
||||
kCurrentTriggerId, kAsyncUidCntr, kInitTriggerId } = async_wrap.constants;
|
||||
|
||||
const { async_id_symbol, trigger_id_symbol } = async_wrap;
|
||||
|
||||
|
@ -50,7 +50,9 @@ const after_symbol = Symbol('after');
|
|||
const destroy_symbol = Symbol('destroy');
|
||||
|
||||
// Setup the callbacks that node::AsyncWrap will call when there are hooks to
|
||||
// process. They use the same functions as the JS embedder API.
|
||||
// process. They use the same functions as the JS embedder API. These callbacks
|
||||
// are setup immediately to prevent async_wrap.setupHooks() from being hijacked
|
||||
// and the cost of doing so is negligible.
|
||||
async_wrap.setupHooks({ init,
|
||||
before: emitBeforeN,
|
||||
after: emitAfterN,
|
||||
|
@ -103,14 +105,21 @@ class AsyncHook {
|
|||
if (hooks_array.includes(this))
|
||||
return this;
|
||||
|
||||
const prev_kTotals = hook_fields[kTotals];
|
||||
hook_fields[kTotals] = 0;
|
||||
|
||||
// createHook() has already enforced that the callbacks are all functions,
|
||||
// so here simply increment the count of whether each callbacks exists or
|
||||
// not.
|
||||
hook_fields[kInit] += +!!this[init_symbol];
|
||||
hook_fields[kBefore] += +!!this[before_symbol];
|
||||
hook_fields[kAfter] += +!!this[after_symbol];
|
||||
hook_fields[kDestroy] += +!!this[destroy_symbol];
|
||||
hook_fields[kTotals] += hook_fields[kInit] += +!!this[init_symbol];
|
||||
hook_fields[kTotals] += hook_fields[kBefore] += +!!this[before_symbol];
|
||||
hook_fields[kTotals] += hook_fields[kAfter] += +!!this[after_symbol];
|
||||
hook_fields[kTotals] += hook_fields[kDestroy] += +!!this[destroy_symbol];
|
||||
hooks_array.push(this);
|
||||
|
||||
if (prev_kTotals === 0 && hook_fields[kTotals] > 0)
|
||||
async_wrap.enablePromiseHook();
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@ -121,11 +130,18 @@ class AsyncHook {
|
|||
if (index === -1)
|
||||
return this;
|
||||
|
||||
hook_fields[kInit] -= +!!this[init_symbol];
|
||||
hook_fields[kBefore] -= +!!this[before_symbol];
|
||||
hook_fields[kAfter] -= +!!this[after_symbol];
|
||||
hook_fields[kDestroy] -= +!!this[destroy_symbol];
|
||||
const prev_kTotals = hook_fields[kTotals];
|
||||
hook_fields[kTotals] = 0;
|
||||
|
||||
hook_fields[kTotals] += hook_fields[kInit] -= +!!this[init_symbol];
|
||||
hook_fields[kTotals] += hook_fields[kBefore] -= +!!this[before_symbol];
|
||||
hook_fields[kTotals] += hook_fields[kAfter] -= +!!this[after_symbol];
|
||||
hook_fields[kTotals] += hook_fields[kDestroy] -= +!!this[destroy_symbol];
|
||||
hooks_array.splice(index, 1);
|
||||
|
||||
if (prev_kTotals > 0 && hook_fields[kTotals] === 0)
|
||||
async_wrap.disablePromiseHook();
|
||||
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue