// value = list
var lists = {};
-
// the main function - creates lists on demand and the watchers associated
// with them.
function insert(item, msecs) {
exports.active = function(item) {
var msecs = item._idleTimeout;
if (msecs >= 0) {
+
var list = lists[msecs];
if (!list || L.isEmpty(list)) {
insert(item, msecs);
after = 1; // schedule on next tick, follows browser behaviour
}
- timer = { _idleTimeout: after };
- timer._idlePrev = timer;
- timer._idleNext = timer;
+ timer = new Timeout(after);
if (arguments.length <= 2) {
timer._onTimeout = callback;
exports.clearTimeout = function(timer) {
if (timer && (timer.ontimeout || timer._onTimeout)) {
timer.ontimeout = timer._onTimeout = null;
- if (timer instanceof Timer) {
+ if (timer instanceof Timer || timer instanceof Timeout) {
timer.close(); // for after === 0
} else {
exports.unenroll(timer);
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);
+ }
+};