From f8f3e94d2d8e0c264876ab035e2fb23b178eb68d Mon Sep 17 00:00:00 2001 From: Anatoli Papirovski Date: Sun, 11 Feb 2018 19:20:42 -0500 Subject: [PATCH 1/2] 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. --- lib/internal/timers.js | 18 ++++++++++-------- lib/timers.js | 23 +++-------------------- 2 files changed, 13 insertions(+), 28 deletions(-) diff --git a/lib/internal/timers.js b/lib/internal/timers.js index 0c140811f8ce88..39ab8d112fb306 100644 --- a/lib/internal/timers.js +++ b/lib/internal/timers.js @@ -24,6 +24,7 @@ module.exports = { async_id_symbol, trigger_async_id_symbol, Timeout, + initAsyncResource, refreshFnSymbol, setUnrefTimeout, validateTimerDuration @@ -37,6 +38,14 @@ function getTimers() { return timers; } +function initAsyncResource(resource, type = 'Timeout') { + 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. // The entire prototype is defined in lib/timers.js function Timeout(callback, after, args, isRepeat, isUnrefed) { @@ -66,14 +75,7 @@ function Timeout(callback, after, args, isRepeat, isUnrefed) { this[unrefedSymbol] = isUnrefed; - this[async_id_symbol] = newAsyncId(); - this[trigger_async_id_symbol] = getDefaultTriggerAsyncId(); - if (initHooksExist()) { - emitInit(this[async_id_symbol], - 'Timeout', - this[trigger_async_id_symbol], - this); - } + initAsyncResource(this); } Timeout.prototype[refreshFnSymbol] = function refresh() { diff --git a/lib/timers.js b/lib/timers.js index 145550b7b5666b..b4d86277cecbfd 100644 --- a/lib/timers.js +++ b/lib/timers.js @@ -30,6 +30,7 @@ const { async_id_symbol, trigger_async_id_symbol, Timeout, + initAsyncResource, validateTimerDuration } = require('internal/timers'); const internalUtil = require('internal/util'); @@ -39,12 +40,8 @@ const util = require('util'); const errors = require('internal/errors'); const debug = util.debuglog('timer'); const { - getDefaultTriggerAsyncId, - newAsyncId, - initHooksExist, destroyHooksExist, // The needed emit*() functions. - emitInit, emitBefore, emitAfter, emitDestroy @@ -188,14 +185,7 @@ function insert(item, unrefed, start) { if (!item[async_id_symbol] || item._destroyed) { item._destroyed = false; - item[async_id_symbol] = newAsyncId(); - item[trigger_async_id_symbol] = getDefaultTriggerAsyncId(); - if (initHooksExist()) { - emitInit(item[async_id_symbol], - 'Timeout', - item[trigger_async_id_symbol], - item); - } + initAsyncResource(item); } L.append(list, item); @@ -720,14 +710,7 @@ const Immediate = class Immediate { this._destroyed = false; this[kRefed] = false; - this[async_id_symbol] = newAsyncId(); - this[trigger_async_id_symbol] = getDefaultTriggerAsyncId(); - if (initHooksExist()) { - emitInit(this[async_id_symbol], - 'Immediate', - this[trigger_async_id_symbol], - this); - } + initAsyncResource(this, 'Immediate'); this.ref(); immediateInfo[kCount]++; From 23671ae412b578c0847f890281f6739e601e5353 Mon Sep 17 00:00:00 2001 From: Anatoli Papirovski Date: Fri, 16 Feb 2018 16:22:38 -0500 Subject: [PATCH 2/2] fixup: no default type --- lib/internal/timers.js | 4 ++-- lib/timers.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/internal/timers.js b/lib/internal/timers.js index 39ab8d112fb306..8c1a87a65f4725 100644 --- a/lib/internal/timers.js +++ b/lib/internal/timers.js @@ -38,7 +38,7 @@ function getTimers() { return timers; } -function initAsyncResource(resource, type = 'Timeout') { +function initAsyncResource(resource, type) { const asyncId = resource[async_id_symbol] = newAsyncId(); const triggerAsyncId = resource[trigger_async_id_symbol] = getDefaultTriggerAsyncId(); @@ -75,7 +75,7 @@ function Timeout(callback, after, args, isRepeat, isUnrefed) { this[unrefedSymbol] = isUnrefed; - initAsyncResource(this); + initAsyncResource(this, 'Timeout'); } Timeout.prototype[refreshFnSymbol] = function refresh() { diff --git a/lib/timers.js b/lib/timers.js index b4d86277cecbfd..bc98718fdfac02 100644 --- a/lib/timers.js +++ b/lib/timers.js @@ -185,7 +185,7 @@ function insert(item, unrefed, start) { if (!item[async_id_symbol] || item._destroyed) { item._destroyed = false; - initAsyncResource(item); + initAsyncResource(item, 'Timeout'); } L.append(list, item);