Skip to content

Commit

Permalink
async_hooks: use hasHooks function internally
Browse files Browse the repository at this point in the history
PR-URL: #32656
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
rickyes authored and addaleax committed Apr 7, 2020
1 parent 6ed912d commit 1c816f0
Showing 1 changed file with 13 additions and 9 deletions.
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;
}

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

0 comments on commit 1c816f0

Please sign in to comment.