add ref/unref to setTimeout timers

This commit is contained in:
Timothy J Fontaine 2012-07-12 22:19:01 -04:00 committed by Bert Belder
parent 2637b5c261
commit cd6122edeb

View File

@ -47,7 +47,6 @@ if (process.env.NODE_DEBUG && /timer/.test(process.env.NODE_DEBUG)) {
// value = list // value = list
var lists = {}; var lists = {};
// the main function - creates lists on demand and the watchers associated // the main function - creates lists on demand and the watchers associated
// with them. // with them.
function insert(item, msecs) { function insert(item, msecs) {
@ -151,6 +150,7 @@ exports.enroll = function(item, msecs) {
exports.active = function(item) { exports.active = function(item) {
var msecs = item._idleTimeout; var msecs = item._idleTimeout;
if (msecs >= 0) { if (msecs >= 0) {
var list = lists[msecs]; var list = lists[msecs];
if (!list || L.isEmpty(list)) { if (!list || L.isEmpty(list)) {
insert(item, msecs); insert(item, msecs);
@ -176,9 +176,7 @@ exports.setTimeout = function(callback, after) {
after = 1; // schedule on next tick, follows browser behaviour after = 1; // schedule on next tick, follows browser behaviour
} }
timer = { _idleTimeout: after }; timer = new Timeout(after);
timer._idlePrev = timer;
timer._idleNext = timer;
if (arguments.length <= 2) { if (arguments.length <= 2) {
timer._onTimeout = callback; timer._onTimeout = callback;
@ -209,7 +207,7 @@ exports.setTimeout = function(callback, after) {
exports.clearTimeout = function(timer) { exports.clearTimeout = function(timer) {
if (timer && (timer.ontimeout || timer._onTimeout)) { if (timer && (timer.ontimeout || timer._onTimeout)) {
timer.ontimeout = timer._onTimeout = null; timer.ontimeout = timer._onTimeout = null;
if (timer instanceof Timer) { if (timer instanceof Timer || timer instanceof Timeout) {
timer.close(); // for after === 0 timer.close(); // for after === 0
} else { } else {
exports.unenroll(timer); exports.unenroll(timer);
@ -245,3 +243,37 @@ exports.clearInterval = function(timer) {
timer.close(); timer.close();
} }
}; };
var Timeout = function(after) {
this._idleTimeout = after;
this._idlePrev = this;
this._idleNext = this;
this._when = Date.now() + after;
};
Timeout.prototype.unref = function() {
if (!this._handle) {
exports.unenroll(this);
this._handle = new Timer();
this._handle.ontimeout = this._onTimeout;
this._handle.start(this._when - Date.now(), 0);
this._handle.unref();
} else {
this._handle.unref();
}
};
Timeout.prototype.ref = function() {
if (this._handle)
this._handle.ref();
};
Timeout.prototype.close = function() {
this._onTimeout = null;
if (this._handle) {
this._handle.ontimeout = null;
this._handle.close();
} else {
exports.unenroll(this);
}
};