timers: add helper fn for async init

There are currently 3 places in Timers where the exact same code
appears. Instead create a helper function that does the same job
of setting asyncId & triggerAsyncId, as well as calling emitInit.

PR-URL: https://github.com/nodejs/node/pull/18825
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
Anatoli Papirovski 2018-02-11 19:20:42 -05:00
parent 30f89dfbf6
commit 28f3ffba0f
No known key found for this signature in database
GPG Key ID: 614E2E1ABEB4B2C0
2 changed files with 13 additions and 28 deletions

View File

@ -24,6 +24,7 @@ module.exports = {
async_id_symbol, async_id_symbol,
trigger_async_id_symbol, trigger_async_id_symbol,
Timeout, Timeout,
initAsyncResource,
refreshFnSymbol, refreshFnSymbol,
setUnrefTimeout, setUnrefTimeout,
validateTimerDuration validateTimerDuration
@ -37,6 +38,14 @@ function getTimers() {
return timers; return timers;
} }
function initAsyncResource(resource, type) {
const asyncId = resource[async_id_symbol] = newAsyncId();
const triggerAsyncId =
resource[trigger_async_id_symbol] = getDefaultTriggerAsyncId();
if (initHooksExist())
emitInit(asyncId, type, triggerAsyncId, resource);
}
// Timer constructor function. // Timer constructor function.
// The entire prototype is defined in lib/timers.js // The entire prototype is defined in lib/timers.js
function Timeout(callback, after, args, isRepeat, isUnrefed) { function Timeout(callback, after, args, isRepeat, isUnrefed) {
@ -66,14 +75,7 @@ function Timeout(callback, after, args, isRepeat, isUnrefed) {
this[unrefedSymbol] = isUnrefed; this[unrefedSymbol] = isUnrefed;
this[async_id_symbol] = newAsyncId(); initAsyncResource(this, 'Timeout');
this[trigger_async_id_symbol] = getDefaultTriggerAsyncId();
if (initHooksExist()) {
emitInit(this[async_id_symbol],
'Timeout',
this[trigger_async_id_symbol],
this);
}
} }
Timeout.prototype[refreshFnSymbol] = function refresh() { Timeout.prototype[refreshFnSymbol] = function refresh() {

View File

@ -30,6 +30,7 @@ const {
async_id_symbol, async_id_symbol,
trigger_async_id_symbol, trigger_async_id_symbol,
Timeout, Timeout,
initAsyncResource,
validateTimerDuration validateTimerDuration
} = require('internal/timers'); } = require('internal/timers');
const internalUtil = require('internal/util'); const internalUtil = require('internal/util');
@ -39,12 +40,8 @@ const util = require('util');
const errors = require('internal/errors'); const errors = require('internal/errors');
const debug = util.debuglog('timer'); const debug = util.debuglog('timer');
const { const {
getDefaultTriggerAsyncId,
newAsyncId,
initHooksExist,
destroyHooksExist, destroyHooksExist,
// The needed emit*() functions. // The needed emit*() functions.
emitInit,
emitBefore, emitBefore,
emitAfter, emitAfter,
emitDestroy emitDestroy
@ -188,14 +185,7 @@ function insert(item, unrefed, start) {
if (!item[async_id_symbol] || item._destroyed) { if (!item[async_id_symbol] || item._destroyed) {
item._destroyed = false; item._destroyed = false;
item[async_id_symbol] = newAsyncId(); initAsyncResource(item, 'Timeout');
item[trigger_async_id_symbol] = getDefaultTriggerAsyncId();
if (initHooksExist()) {
emitInit(item[async_id_symbol],
'Timeout',
item[trigger_async_id_symbol],
item);
}
} }
L.append(list, item); L.append(list, item);
@ -720,14 +710,7 @@ const Immediate = class Immediate {
this._destroyed = false; this._destroyed = false;
this[kRefed] = false; this[kRefed] = false;
this[async_id_symbol] = newAsyncId(); initAsyncResource(this, 'Immediate');
this[trigger_async_id_symbol] = getDefaultTriggerAsyncId();
if (initHooksExist()) {
emitInit(this[async_id_symbol],
'Immediate',
this[trigger_async_id_symbol],
this);
}
this.ref(); this.ref();
immediateInfo[kCount]++; immediateInfo[kCount]++;