mirror of
https://github.com/nodejs/node.git
synced 2025-08-15 21:58:48 +02:00
async_hooks: clean up usage in internal code
Instead of exposing internals of async_hooks & async_wrap throughout the code base, create necessary helper methods within the internal async_hooks that allows easy usage by Node.js internals. This stops every single internal user of async_hooks from importing a ton of functions, constants and internal Aliased Buffers from C++ async_wrap. Adds functions initHooksExist, afterHooksExist, and destroyHooksExist to determine whether the related emit methods need to be triggered. Adds clearDefaultTriggerAsyncId and clearAsyncIdStack on the JS side as an alternative to always calling C++. Moves async_id_symbol and trigger_async_id_symbol to internal async_hooks as they are never used in C++. Renames newUid to newAsyncId for added clarity of its purpose. Adjusts usage throughout the codebase, as well as in a couple of tests. PR-URL: https://github.com/nodejs/node/pull/18720 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ali Ijaz Sheikh <ofrobots@google.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
This commit is contained in:
parent
7748865cd3
commit
e9ac80bb39
20 changed files with 141 additions and 122 deletions
|
@ -9,12 +9,14 @@ const internal_async_hooks = require('internal/async_hooks');
|
|||
// resource gets gced.
|
||||
const { registerDestroyHook } = async_wrap;
|
||||
const {
|
||||
executionAsyncId,
|
||||
triggerAsyncId,
|
||||
// Private API
|
||||
getHookArrays,
|
||||
enableHooks,
|
||||
disableHooks,
|
||||
// Internal Embedder API
|
||||
newUid,
|
||||
newAsyncId,
|
||||
getDefaultTriggerAsyncId,
|
||||
emitInit,
|
||||
emitBefore,
|
||||
|
@ -22,21 +24,16 @@ const {
|
|||
emitDestroy,
|
||||
} = internal_async_hooks;
|
||||
|
||||
// Get fields
|
||||
const { async_id_fields } = async_wrap;
|
||||
|
||||
// Get symbols
|
||||
const {
|
||||
async_id_symbol, trigger_async_id_symbol,
|
||||
init_symbol, before_symbol, after_symbol, destroy_symbol,
|
||||
promise_resolve_symbol
|
||||
} = internal_async_hooks.symbols;
|
||||
|
||||
const { async_id_symbol, trigger_async_id_symbol } = async_wrap;
|
||||
|
||||
// Get constants
|
||||
const {
|
||||
kInit, kBefore, kAfter, kDestroy, kTotals, kPromiseResolve,
|
||||
kExecutionAsyncId, kTriggerAsyncId
|
||||
} = async_wrap.constants;
|
||||
|
||||
// Listener API //
|
||||
|
@ -125,16 +122,6 @@ function createHook(fns) {
|
|||
}
|
||||
|
||||
|
||||
function executionAsyncId() {
|
||||
return async_id_fields[kExecutionAsyncId];
|
||||
}
|
||||
|
||||
|
||||
function triggerAsyncId() {
|
||||
return async_id_fields[kTriggerAsyncId];
|
||||
}
|
||||
|
||||
|
||||
// Embedder API //
|
||||
|
||||
const destroyedSymbol = Symbol('destroyed');
|
||||
|
@ -170,7 +157,7 @@ class AsyncResource {
|
|||
triggerAsyncId);
|
||||
}
|
||||
|
||||
this[async_id_symbol] = newUid();
|
||||
this[async_id_symbol] = newAsyncId();
|
||||
this[trigger_async_id_symbol] = triggerAsyncId;
|
||||
// this prop name (destroyed) has to be synchronized with C++
|
||||
this[destroyedSymbol] = { destroyed: false };
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue