Skip to content

Commit

Permalink
timers: improvements to TimersList management
Browse files Browse the repository at this point in the history
Move all the TimersList instantiation code into the constructor.

Compare values to undefined & null as appropriate instead of
truthy or falsy.

PR-URL: #17429
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
  • Loading branch information
apapirovski committed Dec 7, 2017
1 parent a413df4 commit e55b7d6
Showing 1 changed file with 24 additions and 31 deletions.
55 changes: 24 additions & 31 deletions lib/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const activateImmediateCheck = process._activateImmediateCheck;
delete process._activateImmediateCheck;

// Timeout values > TIMEOUT_MAX are set to 1.
const TIMEOUT_MAX = 2147483647; // 2^31-1
const TIMEOUT_MAX = 2 ** 31 - 1;


// HOW and WHY the timers implementation works the way it does.
Expand Down Expand Up @@ -173,9 +173,9 @@ function insert(item, unrefed) {

// Use an existing list if there is one, otherwise we need to make a new one.
var list = lists[msecs];
if (!list) {
if (list === undefined) {
debug('no %d list was found in insert, creating a new one', msecs);
lists[msecs] = list = createTimersList(msecs, unrefed);
lists[msecs] = list = new TimersList(msecs, unrefed);
}

if (!item[async_id_symbol] || item._destroyed) {
Expand All @@ -194,28 +194,21 @@ function insert(item, unrefed) {
assert(!L.isEmpty(list)); // list is not empty
}

function createTimersList(msecs, unrefed) {
// Make a new linked list of timers, and create a TimerWrap to schedule
// processing for the list.
const list = new TimersList(msecs, unrefed);
L.init(list);
list._timer._list = list;

if (unrefed === true) list._timer.unref();
list._timer.start(msecs);

list._timer[kOnTimeout] = listOnTimeout;

return list;
}

function TimersList(msecs, unrefed) {
this._idleNext = null; // Create the list with the linkedlist properties to
this._idlePrev = null; // prevent any unnecessary hidden class changes.
this._timer = new TimerWrap();
this._idleNext = this; // Create the list with the linkedlist properties to
this._idlePrev = this; // prevent any unnecessary hidden class changes.
this._unrefed = unrefed;
this.msecs = msecs;
this.nextTick = false;

const timer = this._timer = new TimerWrap();
timer._list = this;

if (unrefed === true)
timer.unref();
timer.start(msecs);

timer[kOnTimeout] = listOnTimeout;
}

function listOnTimeout() {
Expand Down Expand Up @@ -359,7 +352,7 @@ function reuse(item) {

var list = refedLists[item._idleTimeout];
// if empty - reuse the watcher
if (list && L.isEmpty(list)) {
if (list !== undefined && L.isEmpty(list)) {
debug('reuse hit');
list._timer.stop();
delete refedLists[item._idleTimeout];
Expand All @@ -382,7 +375,7 @@ const unenroll = exports.unenroll = function(item) {
}

var handle = reuse(item);
if (handle) {
if (handle !== null) {
debug('unenroll: list empty');
handle.close();
}
Expand Down Expand Up @@ -610,7 +603,7 @@ Timeout.prototype.unref = function() {
}

var handle = reuse(this);
if (handle) {
if (handle !== null) {
handle._list = undefined;
}

Expand Down Expand Up @@ -659,7 +652,7 @@ function ImmediateList() {
// Appends an item to the end of the linked list, adjusting the current tail's
// previous and next pointers where applicable
ImmediateList.prototype.append = function(item) {
if (this.tail) {
if (this.tail !== null) {
this.tail._idleNext = item;
item._idlePrev = this.tail;
} else {
Expand All @@ -671,11 +664,11 @@ ImmediateList.prototype.append = function(item) {
// Removes an item from the linked list, adjusting the pointers of adjacent
// items and the linked list's head or tail pointers as necessary
ImmediateList.prototype.remove = function(item) {
if (item._idleNext) {
if (item._idleNext !== null) {
item._idleNext._idlePrev = item._idlePrev;
}

if (item._idlePrev) {
if (item._idlePrev !== null) {
item._idlePrev._idleNext = item._idleNext;
}

Expand All @@ -701,7 +694,7 @@ function processImmediate() {
// immediate callbacks are executed
immediateQueue.head = immediateQueue.tail = null;

while (immediate) {
while (immediate !== null) {
domain = immediate.domain;

if (!immediate._onImmediate) {
Expand All @@ -722,7 +715,7 @@ function processImmediate() {

// If `clearImmediate(immediate)` wasn't called from the callback, use the
// `immediate`'s next item
if (immediate._idleNext)
if (immediate._idleNext !== null)
immediate = immediate._idleNext;
else
immediate = next;
Expand Down Expand Up @@ -754,11 +747,11 @@ function tryOnImmediate(immediate, oldTail) {
}
}

if (threw && immediate._idleNext) {
if (threw && immediate._idleNext !== null) {
// Handle any remaining on next tick, assuming we're still alive to do so.
const curHead = immediateQueue.head;
const next = immediate._idleNext;
if (curHead) {
if (curHead !== null) {
curHead._idlePrev = oldTail;
oldTail._idleNext = curHead;
next._idlePrev = null;
Expand Down

0 comments on commit e55b7d6

Please sign in to comment.