| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 3f1787b commit b491eab
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -134,15 +134,16 @@ class AsyncResource { | |||
| 134 | 134 | constructor(type, opts = {}) { | |
| 135 | 135 | validateString(type, 'type'); | |
| 136 | 136 | ||
| 137 | - if (typeof opts === 'number') { | ||
| 138 | - opts = { triggerAsyncId: opts, requireManualDestroy: false }; | ||
| 139 | - } else if (opts.triggerAsyncId === undefined) { | ||
| 140 | - opts.triggerAsyncId = getDefaultTriggerAsyncId(); | ||
| 137 | + let triggerAsyncId = opts; | ||
| 138 | + let requireManualDestroy = false; | ||
| 139 | + if (typeof opts !== 'number') { | ||
| 140 | + triggerAsyncId = opts.triggerAsyncId === undefined ? | ||
| 141 | + getDefaultTriggerAsyncId() : opts.triggerAsyncId; | ||
| 142 | + requireManualDestroy = !!opts.requireManualDestroy; | ||
| 141 | 143 | } | |
| 142 | 144 | ||
| 143 | 145 | // Unlike emitInitScript, AsyncResource doesn't supports null as the | |
| 144 | 146 | // triggerAsyncId. | |
| 145 | - const triggerAsyncId = opts.triggerAsyncId; | ||
| 146 | 147 | if (!Number.isSafeInteger(triggerAsyncId) || triggerAsyncId < -1) { | |
| 147 | 148 | throw new ERR_INVALID_ASYNC_ID('triggerAsyncId', triggerAsyncId); | |
| 148 | 149 | } | |
@@ -151,15 +152,14 @@ class AsyncResource { | |||
| 151 | 152 | this[async_id_symbol] = asyncId; | |
| 152 | 153 | this[trigger_async_id_symbol] = triggerAsyncId; | |
| 153 | 154 | ||
| 154 | - // This prop name (destroyed) has to be synchronized with C++ | ||
| 155 | - const destroyed = { destroyed: false }; | ||
| 156 | - this[destroyedSymbol] = destroyed; | ||
| 157 | - | ||
| 158 | 155 | if (initHooksExist()) { | |
| 159 | 156 | emitInit(asyncId, type, triggerAsyncId, this); | |
| 160 | 157 | } | |
| 161 | 158 | ||
| 162 | - if (!opts.requireManualDestroy) { | ||
| 159 | + if (!requireManualDestroy) { | ||
| 160 | + // This prop name (destroyed) has to be synchronized with C++ | ||
| 161 | + const destroyed = { destroyed: false }; | ||
| 162 | + this[destroyedSymbol] = destroyed; | ||
| 163 | 163 | registerDestroyHook(this, asyncId, destroyed); | |
| 164 | 164 | } | |
| 165 | 165 | } | |
@@ -168,14 +168,18 @@ class AsyncResource { | |||
| 168 | 168 | const asyncId = this[async_id_symbol]; | |
| 169 | 169 | emitBefore(asyncId, this[trigger_async_id_symbol]); | |
| 170 | 170 | try { | |
| 171 | + if (thisArg === undefined) | ||
| 172 | + return fn(...args); | ||
| 171 | 173 | return Reflect.apply(fn, thisArg, args); | |
| 172 | 174 | } finally { | |
| 173 | 175 | emitAfter(asyncId); | |
| 174 | 176 | } | |
| 175 | 177 | } | |
| 176 | 178 | ||
| 177 | 179 | emitDestroy() { | |
| 178 | - this[destroyedSymbol].destroyed = true; | ||
| 180 | + if (this[destroyedSymbol] !== undefined) { | ||
| 181 | + this[destroyedSymbol].destroyed = true; | ||
| 182 | + } | ||
| 179 | 183 | emitDestroy(this[async_id_symbol]); | |
| 180 | 184 | return this; | |
| 181 | 185 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -128,15 +128,13 @@ function nextTick(callback) { | |||
| 128 | 128 | } | |
| 129 | 129 | ||
| 130 | 130 | let AsyncResource; | |
| 131 | + const defaultMicrotaskResourceOpts = { requireManualDestroy: true }; | ||
| 131 | 132 | function createMicrotaskResource() { | |
| 132 | 133 | // Lazy load the async_hooks module | |
| 133 | - if (!AsyncResource) { | ||
| 134 | + if (AsyncResource === undefined) { | ||
| 134 | 135 | AsyncResource = require('async_hooks').AsyncResource; | |
| 135 | 136 | } | |
| 136 | - return new AsyncResource('Microtask', { | ||
| 137 | - triggerAsyncId: getDefaultTriggerAsyncId(), | ||
| 138 | - requireManualDestroy: true, | ||
| 139 | - }); | ||
| 137 | + return new AsyncResource('Microtask', defaultMicrotaskResourceOpts); | ||
| 140 | 138 | } | |
| 141 | 139 | ||
| 142 | 140 | function runMicrotask() { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments