mirror of
https://github.com/nodejs/node.git
synced 2025-08-15 21:58:48 +02:00
async_hooks: refactor to use more primordials
PR-URL: https://github.com/nodejs/node/pull/36168 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
This commit is contained in:
parent
514f464a60
commit
f47d65538a
3 changed files with 20 additions and 12 deletions
|
@ -1,6 +1,11 @@
|
|||
'use strict';
|
||||
|
||||
const {
|
||||
ArrayPrototypeIncludes,
|
||||
ArrayPrototypeIndexOf,
|
||||
ArrayPrototypePush,
|
||||
ArrayPrototypeSplice,
|
||||
FunctionPrototypeBind,
|
||||
NumberIsSafeInteger,
|
||||
ObjectDefineProperties,
|
||||
ObjectIs,
|
||||
|
@ -85,7 +90,7 @@ class AsyncHook {
|
|||
const [hooks_array, hook_fields] = getHookArrays();
|
||||
|
||||
// Each hook is only allowed to be added once.
|
||||
if (hooks_array.includes(this))
|
||||
if (ArrayPrototypeIncludes(hooks_array, this))
|
||||
return this;
|
||||
|
||||
const prev_kTotals = hook_fields[kTotals];
|
||||
|
@ -99,7 +104,7 @@ class AsyncHook {
|
|||
hook_fields[kTotals] += hook_fields[kDestroy] += +!!this[destroy_symbol];
|
||||
hook_fields[kTotals] +=
|
||||
hook_fields[kPromiseResolve] += +!!this[promise_resolve_symbol];
|
||||
hooks_array.push(this);
|
||||
ArrayPrototypePush(hooks_array, this);
|
||||
|
||||
if (prev_kTotals === 0 && hook_fields[kTotals] > 0) {
|
||||
enableHooks();
|
||||
|
@ -113,7 +118,7 @@ class AsyncHook {
|
|||
disable() {
|
||||
const [hooks_array, hook_fields] = getHookArrays();
|
||||
|
||||
const index = hooks_array.indexOf(this);
|
||||
const index = ArrayPrototypeIndexOf(hooks_array, this);
|
||||
if (index === -1)
|
||||
return this;
|
||||
|
||||
|
@ -125,7 +130,7 @@ class AsyncHook {
|
|||
hook_fields[kTotals] += hook_fields[kDestroy] -= +!!this[destroy_symbol];
|
||||
hook_fields[kTotals] +=
|
||||
hook_fields[kPromiseResolve] -= +!!this[promise_resolve_symbol];
|
||||
hooks_array.splice(index, 1);
|
||||
ArrayPrototypeSplice(hooks_array, index, 1);
|
||||
|
||||
if (prev_kTotals > 0 && hook_fields[kTotals] === 0) {
|
||||
disableHooks();
|
||||
|
@ -218,7 +223,7 @@ class AsyncResource {
|
|||
bind(fn) {
|
||||
if (typeof fn !== 'function')
|
||||
throw new ERR_INVALID_ARG_TYPE('fn', 'Function', fn);
|
||||
const ret = this.runInAsyncScope.bind(this, fn);
|
||||
const ret = FunctionPrototypeBind(this.runInAsyncScope, this, fn);
|
||||
ObjectDefineProperties(ret, {
|
||||
'length': {
|
||||
configurable: true,
|
||||
|
@ -264,7 +269,8 @@ class AsyncLocalStorage {
|
|||
if (this.enabled) {
|
||||
this.enabled = false;
|
||||
// If this.enabled, the instance must be in storageList
|
||||
storageList.splice(storageList.indexOf(this), 1);
|
||||
ArrayPrototypeSplice(storageList,
|
||||
ArrayPrototypeIndexOf(storageList, this), 1);
|
||||
if (storageList.length === 0) {
|
||||
storageHook.disable();
|
||||
}
|
||||
|
@ -274,7 +280,7 @@ class AsyncLocalStorage {
|
|||
_enable() {
|
||||
if (!this.enabled) {
|
||||
this.enabled = true;
|
||||
storageList.push(this);
|
||||
ArrayPrototypePush(storageList, this);
|
||||
storageHook.enable();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue