Skip to content
Closed
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
Prev Previous commit
timers: refactor to use ES6 classes
  • Loading branch information
jasnell committed Apr 29, 2017
commit 468d522c401bac20dde3266346229afd7c5d3dd0
213 changes: 108 additions & 105 deletions lib/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,15 @@ function createTimersList(msecs, unrefed) {
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._unrefed = unrefed;
this.msecs = msecs;
this.nextTick = false;
class TimersList {
constructor(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._unrefed = unrefed;
this.msecs = msecs;
this.nextTick = false;
}
}

function listOnTimeout() {
Expand Down Expand Up @@ -494,18 +496,63 @@ function clearInterval(timer) {
}
}

class Timeout {
constructor(after, callback, args) {
this._called = false;
this._idleTimeout = after;
this._idlePrev = this;
this._idleNext = this;
this._idleStart = null;
this._onTimeout = callback;
this._timerArgs = args;
this._repeat = null;
}

unref() {
if (this._handle) {
this._handle.unref();
} else if (typeof this._onTimeout === 'function') {
var now = TimerWrap.now();
if (!this._idleStart) this._idleStart = now;
var delay = this._idleStart + this._idleTimeout - now;
if (delay < 0) delay = 0;

// Prevent running cb again when unref() is called during the same cb
if (this._called && !this._repeat) {
unenroll(this);
return;
}

function Timeout(after, callback, args) {
this._called = false;
this._idleTimeout = after;
this._idlePrev = this;
this._idleNext = this;
this._idleStart = null;
this._onTimeout = callback;
this._timerArgs = args;
this._repeat = null;
}
var handle = reuse(this);

this._handle = handle || new TimerWrap();
this._handle.owner = this;
this._handle[kOnTimeout] = unrefdHandle;
this._handle.start(delay);
this._handle.domain = this.domain;
this._handle.unref();
}
return this;
}

ref() {
if (this._handle)
this._handle.ref();
return this;
}

close() {
this._onTimeout = null;
if (this._handle) {
this._idleTimeout = -1;
this._handle[kOnTimeout] = null;
this._handle.close();
} else {
unenroll(this);
}
return this;
}
}

function unrefdHandle() {
// Don't attempt to call the callback if it is not a function.
Expand All @@ -521,93 +568,48 @@ function unrefdHandle() {
}
}


Timeout.prototype.unref = function() {
if (this._handle) {
this._handle.unref();
} else if (typeof this._onTimeout === 'function') {
var now = TimerWrap.now();
if (!this._idleStart) this._idleStart = now;
var delay = this._idleStart + this._idleTimeout - now;
if (delay < 0) delay = 0;

// Prevent running cb again when unref() is called during the same cb
if (this._called && !this._repeat) {
unenroll(this);
return;
// A linked list for storing `setImmediate()` requests
class ImmediateList {
constructor() {
this.head = null;
this.tail = null;
}

// Appends an item to the end of the linked list, adjusting the current tail's
// previous and next pointers where applicable
append(item) {
if (this.tail) {
this.tail._idleNext = item;
item._idlePrev = this.tail;
} else {
this.head = item;
}

var handle = reuse(this);

this._handle = handle || new TimerWrap();
this._handle.owner = this;
this._handle[kOnTimeout] = unrefdHandle;
this._handle.start(delay);
this._handle.domain = this.domain;
this._handle.unref();
this.tail = item;
}
return this;
};

Timeout.prototype.ref = function() {
if (this._handle)
this._handle.ref();
return this;
};

Timeout.prototype.close = function() {
this._onTimeout = null;
if (this._handle) {
this._idleTimeout = -1;
this._handle[kOnTimeout] = null;
this._handle.close();
} else {
unenroll(this);
}
return this;
};


// A linked list for storing `setImmediate()` requests
function ImmediateList() {
this.head = null;
this.tail = null;
}
// Removes an item from the linked list, adjusting the pointers of adjacent
// items and the linked list's head or tail pointers as necessary
remove(item) {
if (item._idleNext) {
item._idleNext._idlePrev = item._idlePrev;
}

// 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) {
this.tail._idleNext = item;
item._idlePrev = this.tail;
} else {
this.head = item;
}
this.tail = item;
};
if (item._idlePrev) {
item._idlePrev._idleNext = item._idleNext;
}

// 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) {
item._idleNext._idlePrev = item._idlePrev;
}
if (item === this.head)
this.head = item._idleNext;
if (item === this.tail)
this.tail = item._idlePrev;

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

if (item === this.head)
this.head = item._idleNext;
if (item === this.tail)
this.tail = item._idlePrev;

item._idleNext = null;
item._idlePrev = null;
};
}

// Create a single linked list instance only once at startup
var immediateQueue = new ImmediateList();
const immediateQueue = new ImmediateList();


function processImmediate() {
Expand Down Expand Up @@ -703,16 +705,17 @@ function runCallback(timer) {
}
}


function Immediate() {
// assigning the callback here can cause optimize/deoptimize thrashing
// so have caller annotate the object (node v6.0.0, v8 5.0.71.35)
this._idleNext = null;
this._idlePrev = null;
this._callback = null;
this._argv = null;
this._onImmediate = null;
this.domain = process.domain;
class Immediate {
constructor() {
// assigning the callback here can cause optimize/deoptimize thrashing
// so have caller annotate the object (node v6.0.0, v8 5.0.71.35)
this._idleNext = null;
this._idlePrev = null;
this._callback = null;
this._argv = null;
this._onImmediate = null;
this.domain = process.domain;
}
}

function setImmediate(callback, arg1, arg2, arg3) {
Expand Down