From 2ec4cd552501b895c83c6871951c1a6256288c48 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Tue, 18 Jan 2011 14:26:32 -0800 Subject: [PATCH] factor linklist code into own file --- lib/_linklist.js | 55 +++++++++++++++++++++ lib/timers.js | 88 +++++----------------------------- test/simple/test-timers-linked-list.js | 2 +- 3 files changed, 69 insertions(+), 76 deletions(-) create mode 100644 lib/_linklist.js diff --git a/lib/_linklist.js b/lib/_linklist.js new file mode 100644 index 0000000..c5c209b --- /dev/null +++ b/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; diff --git a/lib/timers.js b/lib/timers.js index a1a994c..da46c5f 100644 --- a/lib/timers.js +++ b/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); } } }; diff --git a/test/simple/test-timers-linked-list.js b/test/simple/test-timers-linked-list.js index 0d9a2b9..26bf052 100644 --- a/test/simple/test-timers-linked-list.js +++ b/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" }; -- 2.7.4