Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

async_hook: Add the hasHooks function #32656

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions lib/internal/async_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,27 +316,31 @@ function defaultTriggerAsyncIdScope(triggerAsyncId, block, ...args) {
}
}

function hasHooks(key) {
return async_hook_fields[key] > 0;
}
addaleax marked this conversation as resolved.
Show resolved Hide resolved

function enabledHooksExist() {
return async_hook_fields[kCheck] > 0;
return hasHooks(kCheck);
}

function initHooksExist() {
return async_hook_fields[kInit] > 0;
return hasHooks(kInit);
}

function afterHooksExist() {
return async_hook_fields[kAfter] > 0;
return hasHooks(kAfter);
}

function destroyHooksExist() {
return async_hook_fields[kDestroy] > 0;
return hasHooks(kDestroy);
}


function emitInitScript(asyncId, type, triggerAsyncId, resource) {
// Short circuit all checks for the common case. Which is that no hooks have
// been set. Do this to remove performance impact for embedders (and core).
if (async_hook_fields[kInit] === 0)
if (!hasHooks(kInit))
return;

if (triggerAsyncId === null) {
Expand All @@ -350,13 +354,13 @@ function emitInitScript(asyncId, type, triggerAsyncId, resource) {
function emitBeforeScript(asyncId, triggerAsyncId, resource) {
pushAsyncContext(asyncId, triggerAsyncId, resource);

if (async_hook_fields[kBefore] > 0)
if (hasHooks(kBefore))
emitBeforeNative(asyncId);
}


function emitAfterScript(asyncId) {
if (async_hook_fields[kAfter] > 0)
if (hasHooks(kAfter))
emitAfterNative(asyncId);

popAsyncContext(asyncId);
Expand All @@ -365,7 +369,7 @@ function emitAfterScript(asyncId) {

function emitDestroyScript(asyncId) {
// Return early if there are no destroy callbacks, or invalid asyncId.
if (async_hook_fields[kDestroy] === 0 || asyncId <= 0)
if (!hasHooks(kDestroy) || asyncId <= 0)
return;
async_wrap.queueDestroyAsyncId(asyncId);
}
Expand All @@ -382,7 +386,7 @@ function clearAsyncIdStack() {


function hasAsyncIdStack() {
return async_hook_fields[kStackLength] > 0;
return hasHooks(kStackLength);
}


Expand Down