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 L = require('_linklist');
var assert = process.assert;
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
//
// Because often many sockets will have the same idle timeout we will not
@ -100,7 +38,7 @@ function insert(item, msecs) {
list = lists[msecs];
} else {
list = new Timer();
init(list);
L.init(list);
lists[msecs] = list;
@ -112,42 +50,42 @@ function insert(item, msecs) {
debug('now: ' + now);
var first;
while (first = peek(list)) {
while (first = L.peek(list)) {
var diff = now - first._idleStart;
if (diff + 1 < msecs) {
list.again(msecs - diff);
debug(msecs + ' list wait because diff is ' + diff);
return;
} else {
remove(first);
assert(first !== peek(list));
L.remove(first);
assert(first !== L.peek(list));
if (first._onTimeout) first._onTimeout();
}
}
debug(msecs + ' list empty');
assert(isEmpty(list));
assert(L.isEmpty(list));
list.stop();
};
}
if (isEmpty(list)) {
if (L.isEmpty(list)) {
// if empty (re)start the timer
list.again(msecs);
}
append(list, item);
assert(!isEmpty(list)); // list is not empty
L.append(list, item);
assert(!L.isEmpty(list)); // list is not empty
}
var unenroll = exports.unenroll = function(item) {
remove(item);
L.remove(item);
var list = lists[item._idleTimeout];
// if empty then stop the watcher
debug('unenroll');
if (list && isEmpty(list)) {
if (list && L.isEmpty(list)) {
debug('unenroll: list empty');
list.stop();
}
@ -161,7 +99,7 @@ exports.enroll = function(item, msecs) {
if (item._idleNext) unenroll(item);
item._idleTimeout = msecs;
init(item);
L.init(item);
};
@ -175,7 +113,7 @@ exports.active = function(item) {
insert(item, msecs);
} else {
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 assert = require('assert');
var L = require('timers').linkedList;
var L = require('_linklist');
var list = { name: "list" };

Loading…
Cancel
Save