Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

timers: add helper fn for async init #18825

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions lib/internal/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module.exports = {
async_id_symbol,
trigger_async_id_symbol,
Timeout,
initAsyncResource,
refreshFnSymbol,
setUnrefTimeout,
validateTimerDuration
Expand All @@ -37,6 +38,14 @@ function getTimers() {
return timers;
}

function initAsyncResource(resource, type = 'Timeout') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking nit: I personally would remove the default and always pass in the value explicitly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I was undecided on whether to do this or not so adjusted now.

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) {
Expand Down Expand Up @@ -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() {
Expand Down
23 changes: 3 additions & 20 deletions lib/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const {
async_id_symbol,
trigger_async_id_symbol,
Timeout,
initAsyncResource,
validateTimerDuration
} = require('internal/timers');
const internalUtil = require('internal/util');
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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]++;
Expand Down