Browse Source

factor linklist code into own file

v0.7.4-release
Ryan Dahl 14 years ago
parent
commit
2ec4cd5525
  1. 55
      lib/_linklist.js
  2. 88
      lib/timers.js
  3. 2
      test/simple/test-timers-linked-list.js

55
lib/_linklist.js

@ -0,0 +1,55 @@
function init(list) {
list._idleNext = list;
list._idlePrev = list;
}
exports.init = init;
// show the most idle item
function peek(list) {
if (list._idlePrev == list) return null;
return list._idlePrev;
}
exports.peek = peek;
// remove the most idle item from the list
function shift(list) {
var first = list._idlePrev;
remove(first);
return first;
}
exports.shift = shift;
// remove a item from its list
function remove(item) {
if (item._idleNext) {
item._idleNext._idlePrev = item._idlePrev;
}
if (item._idlePrev) {
item._idlePrev._idleNext = item._idleNext;
}
item._idleNext = null;
item._idlePrev = null;
}
exports.remove = remove;
// remove a item from its list and place at the end.
function append(list, item) {
remove(item);
item._idleNext = list._idleNext;
list._idleNext._idlePrev = item;
item._idlePrev = list;
list._idleNext = item;
}
exports.append = append;
function isEmpty(list) {
return list._idleNext === list;
}
exports.isEmpty = isEmpty;

88
lib/timers.js

@ -1,4 +1,5 @@
var Timer = process.binding('timer').Timer; var Timer = process.binding('timer').Timer;
var L = require('_linklist');
var assert = process.assert; var assert = process.assert;
var debug; var debug;
@ -9,69 +10,6 @@ if (process.env.NODE_debug && /timer/.test(process.env.NODE_debug)) {
} }
// Export the linklist code for testing.
exports.linkedList = {};
function init(list) {
list._idleNext = list;
list._idlePrev = list;
}
exports.linkedList.init = init;
// show the most idle item
function peek(list) {
if (list._idlePrev == list) return null;
return list._idlePrev;
}
exports.linkedList.peek = peek;
// remove the most idle item from the list
function shift(list) {
var first = list._idlePrev;
remove(first);
return first;
}
exports.linkedList.shift = shift;
// remove a item from its list
function remove(item) {
if (item._idleNext) {
item._idleNext._idlePrev = item._idlePrev;
}
if (item._idlePrev) {
item._idlePrev._idleNext = item._idleNext;
}
item._idleNext = null;
item._idlePrev = null;
}
exports.linkedList.remove = remove;
// remove a item from its list and place at the end.
function append(list, item) {
remove(item);
item._idleNext = list._idleNext;
list._idleNext._idlePrev = item;
item._idlePrev = list;
list._idleNext = item;
}
exports.linkedList.append = append;
function isEmpty(list) {
return list._idleNext === list;
}
exports.linkedList.isEmpty = isEmpty;
// IDLE TIMEOUTS // IDLE TIMEOUTS
// //
// Because often many sockets will have the same idle timeout we will not // Because often many sockets will have the same idle timeout we will not
@ -100,7 +38,7 @@ function insert(item, msecs) {
list = lists[msecs]; list = lists[msecs];
} else { } else {
list = new Timer(); list = new Timer();
init(list); L.init(list);
lists[msecs] = list; lists[msecs] = list;
@ -112,42 +50,42 @@ function insert(item, msecs) {
debug('now: ' + now); debug('now: ' + now);
var first; var first;
while (first = peek(list)) { while (first = L.peek(list)) {
var diff = now - first._idleStart; var diff = now - first._idleStart;
if (diff + 1 < msecs) { if (diff + 1 < msecs) {
list.again(msecs - diff); list.again(msecs - diff);
debug(msecs + ' list wait because diff is ' + diff); debug(msecs + ' list wait because diff is ' + diff);
return; return;
} else { } else {
remove(first); L.remove(first);
assert(first !== peek(list)); assert(first !== L.peek(list));
if (first._onTimeout) first._onTimeout(); if (first._onTimeout) first._onTimeout();
} }
} }
debug(msecs + ' list empty'); debug(msecs + ' list empty');
assert(isEmpty(list)); assert(L.isEmpty(list));
list.stop(); list.stop();
}; };
} }
if (isEmpty(list)) { if (L.isEmpty(list)) {
// if empty (re)start the timer // if empty (re)start the timer
list.again(msecs); list.again(msecs);
} }
append(list, item); L.append(list, item);
assert(!isEmpty(list)); // list is not empty assert(!L.isEmpty(list)); // list is not empty
} }
var unenroll = exports.unenroll = function(item) { var unenroll = exports.unenroll = function(item) {
remove(item); L.remove(item);
var list = lists[item._idleTimeout]; var list = lists[item._idleTimeout];
// if empty then stop the watcher // if empty then stop the watcher
debug('unenroll'); debug('unenroll');
if (list && isEmpty(list)) { if (list && L.isEmpty(list)) {
debug('unenroll: list empty'); debug('unenroll: list empty');
list.stop(); list.stop();
} }
@ -161,7 +99,7 @@ exports.enroll = function(item, msecs) {
if (item._idleNext) unenroll(item); if (item._idleNext) unenroll(item);
item._idleTimeout = msecs; item._idleTimeout = msecs;
init(item); L.init(item);
}; };
@ -175,7 +113,7 @@ exports.active = function(item) {
insert(item, msecs); insert(item, msecs);
} else { } else {
item._idleStart = new Date(); item._idleStart = new Date();
append(list, item); L.append(list, item);
} }
} }
}; };

2
test/simple/test-timers-linked-list.js

@ -1,6 +1,6 @@
var common = require('../common'); var common = require('../common');
var assert = require('assert'); var assert = require('assert');
var L = require('timers').linkedList; var L = require('_linklist');
var list = { name: "list" }; var list = { name: "list" };

Loading…
Cancel
Save