Skip to content

Commit 28f3ffb

Browse files
committed
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: #18825 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent 30f89df commit 28f3ffb

File tree

2 files changed

+13
-28
lines changed

2 files changed

+13
-28
lines changed

lib/internal/timers.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ module.exports = {
2424
async_id_symbol,
2525
trigger_async_id_symbol,
2626
Timeout,
27+
initAsyncResource,
2728
refreshFnSymbol,
2829
setUnrefTimeout,
2930
validateTimerDuration
@@ -37,6 +38,14 @@ function getTimers() {
3738
return timers;
3839
}
3940

41+
function initAsyncResource(resource, type) {
42+
const asyncId = resource[async_id_symbol] = newAsyncId();
43+
const triggerAsyncId =
44+
resource[trigger_async_id_symbol] = getDefaultTriggerAsyncId();
45+
if (initHooksExist())
46+
emitInit(asyncId, type, triggerAsyncId, resource);
47+
}
48+
4049
// Timer constructor function.
4150
// The entire prototype is defined in lib/timers.js
4251
function Timeout(callback, after, args, isRepeat, isUnrefed) {
@@ -66,14 +75,7 @@ function Timeout(callback, after, args, isRepeat, isUnrefed) {
6675

6776
this[unrefedSymbol] = isUnrefed;
6877

69-
this[async_id_symbol] = newAsyncId();
70-
this[trigger_async_id_symbol] = getDefaultTriggerAsyncId();
71-
if (initHooksExist()) {
72-
emitInit(this[async_id_symbol],
73-
'Timeout',
74-
this[trigger_async_id_symbol],
75-
this);
76-
}
78+
initAsyncResource(this, 'Timeout');
7779
}
7880

7981
Timeout.prototype[refreshFnSymbol] = function refresh() {

lib/timers.js

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const {
3030
async_id_symbol,
3131
trigger_async_id_symbol,
3232
Timeout,
33+
initAsyncResource,
3334
validateTimerDuration
3435
} = require('internal/timers');
3536
const internalUtil = require('internal/util');
@@ -39,12 +40,8 @@ const util = require('util');
3940
const errors = require('internal/errors');
4041
const debug = util.debuglog('timer');
4142
const {
42-
getDefaultTriggerAsyncId,
43-
newAsyncId,
44-
initHooksExist,
4543
destroyHooksExist,
4644
// The needed emit*() functions.
47-
emitInit,
4845
emitBefore,
4946
emitAfter,
5047
emitDestroy
@@ -188,14 +185,7 @@ function insert(item, unrefed, start) {
188185

189186
if (!item[async_id_symbol] || item._destroyed) {
190187
item._destroyed = false;
191-
item[async_id_symbol] = newAsyncId();
192-
item[trigger_async_id_symbol] = getDefaultTriggerAsyncId();
193-
if (initHooksExist()) {
194-
emitInit(item[async_id_symbol],
195-
'Timeout',
196-
item[trigger_async_id_symbol],
197-
item);
198-
}
188+
initAsyncResource(item, 'Timeout');
199189
}
200190

201191
L.append(list, item);
@@ -720,14 +710,7 @@ const Immediate = class Immediate {
720710
this._destroyed = false;
721711
this[kRefed] = false;
722712

723-
this[async_id_symbol] = newAsyncId();
724-
this[trigger_async_id_symbol] = getDefaultTriggerAsyncId();
725-
if (initHooksExist()) {
726-
emitInit(this[async_id_symbol],
727-
'Immediate',
728-
this[trigger_async_id_symbol],
729-
this);
730-
}
713+
initAsyncResource(this, 'Immediate');
731714

732715
this.ref();
733716
immediateInfo[kCount]++;

0 commit comments

Comments
 (0)