|
|
@ -19,7 +19,7 @@ |
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
|
|
|
var Timer = process.binding('timer').Timer; |
|
|
|
var Timer = process.binding('timer_wrap').Timer; |
|
|
|
var L = require('_linklist'); |
|
|
|
var assert = require('assert').ok; |
|
|
|
|
|
|
@ -59,11 +59,13 @@ function insert(item, msecs) { |
|
|
|
list = lists[msecs]; |
|
|
|
} else { |
|
|
|
list = new Timer(); |
|
|
|
list.start(msecs, 0); |
|
|
|
|
|
|
|
L.init(list); |
|
|
|
|
|
|
|
lists[msecs] = list; |
|
|
|
|
|
|
|
list.callback = function() { |
|
|
|
list.ontimeout = function() { |
|
|
|
debug('timeout callback ' + msecs); |
|
|
|
// TODO - don't stop and start the watcher all the time.
|
|
|
|
// just set its repeat
|
|
|
@ -74,7 +76,7 @@ function insert(item, msecs) { |
|
|
|
while (first = L.peek(list)) { |
|
|
|
var diff = now - first._idleStart; |
|
|
|
if (diff + 1 < msecs) { |
|
|
|
list.again(msecs - diff); |
|
|
|
list.start(diff, 0); |
|
|
|
debug(msecs + ' list wait because diff is ' + diff); |
|
|
|
return; |
|
|
|
} else { |
|
|
@ -86,15 +88,11 @@ function insert(item, msecs) { |
|
|
|
|
|
|
|
debug(msecs + ' list empty'); |
|
|
|
assert(L.isEmpty(list)); |
|
|
|
list.stop(); |
|
|
|
list.close(); |
|
|
|
delete lists[msecs]; |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
if (L.isEmpty(list)) { |
|
|
|
// if empty (re)start the timer
|
|
|
|
list.again(msecs); |
|
|
|
} |
|
|
|
|
|
|
|
L.append(list, item); |
|
|
|
assert(!L.isEmpty(list)); // list is not empty
|
|
|
|
} |
|
|
@ -108,7 +106,8 @@ var unenroll = exports.unenroll = function(item) { |
|
|
|
debug('unenroll'); |
|
|
|
if (list && L.isEmpty(list)) { |
|
|
|
debug('unenroll: list empty'); |
|
|
|
list.stop(); |
|
|
|
list.close(); |
|
|
|
delete lists[item._idleTimeout]; |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
@ -146,43 +145,43 @@ exports.active = function(item) { |
|
|
|
|
|
|
|
|
|
|
|
exports.setTimeout = function(callback, after) { |
|
|
|
var timer; |
|
|
|
var timer, c, args; |
|
|
|
|
|
|
|
if (after <= 0) { |
|
|
|
// Use the slow case for after == 0
|
|
|
|
timer = new Timer(); |
|
|
|
timer.callback = callback; |
|
|
|
timer.ontimeout = callback; |
|
|
|
|
|
|
|
args = Array.prototype.slice.call(arguments, 2); |
|
|
|
timer._onTimeout = function() { |
|
|
|
callback.apply(timer, args); |
|
|
|
timer.close(); |
|
|
|
} |
|
|
|
|
|
|
|
timer.start(0, 0); |
|
|
|
} else { |
|
|
|
timer = { _idleTimeout: after, _onTimeout: callback }; |
|
|
|
timer = { _idleTimeout: after }; |
|
|
|
timer._idlePrev = timer; |
|
|
|
timer._idleNext = timer; |
|
|
|
} |
|
|
|
|
|
|
|
/* |
|
|
|
* Sometimes setTimeout is called with arguments, EG |
|
|
|
* |
|
|
|
* setTimeout(callback, 2000, "hello", "world") |
|
|
|
* |
|
|
|
* If that's the case we need to call the callback with |
|
|
|
* those args. The overhead of an extra closure is not |
|
|
|
* desired in the normal case. |
|
|
|
*/ |
|
|
|
if (arguments.length > 2) { |
|
|
|
var args = Array.prototype.slice.call(arguments, 2); |
|
|
|
var c = function() { |
|
|
|
callback.apply(timer, args); |
|
|
|
}; |
|
|
|
|
|
|
|
if (timer instanceof Timer) { |
|
|
|
timer.callback = c; |
|
|
|
if (arguments.length <= 2) { |
|
|
|
timer._onTimeout = callback; |
|
|
|
} else { |
|
|
|
timer._onTimeout = c; |
|
|
|
/* |
|
|
|
* Sometimes setTimeout is called with arguments, EG |
|
|
|
* |
|
|
|
* setTimeout(callback, 2000, "hello", "world") |
|
|
|
* |
|
|
|
* If that's the case we need to call the callback with |
|
|
|
* those args. The overhead of an extra closure is not |
|
|
|
* desired in the normal case. |
|
|
|
*/ |
|
|
|
args = Array.prototype.slice.call(arguments, 2); |
|
|
|
timer._onTimeout = function() { |
|
|
|
callback.apply(timer, args); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (timer instanceof Timer) { |
|
|
|
timer.start(0, 0); |
|
|
|
} else { |
|
|
|
exports.active(timer); |
|
|
|
} |
|
|
|
|
|
|
@ -191,10 +190,13 @@ exports.setTimeout = function(callback, after) { |
|
|
|
|
|
|
|
|
|
|
|
exports.clearTimeout = function(timer) { |
|
|
|
if (timer && (timer.callback || timer._onTimeout)) { |
|
|
|
timer.callback = timer._onTimeout = null; |
|
|
|
exports.unenroll(timer); |
|
|
|
if (timer instanceof Timer) timer.stop(); // for after === 0
|
|
|
|
if (timer && (timer.ontimeout || timer._onTimeout)) { |
|
|
|
timer.ontimeout = timer._onTimeout = null; |
|
|
|
if (timer instanceof Timer) { |
|
|
|
timer.close(); // for after === 0
|
|
|
|
} else { |
|
|
|
exports.unenroll(timer); |
|
|
|
} |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
@ -202,13 +204,9 @@ exports.clearTimeout = function(timer) { |
|
|
|
exports.setInterval = function(callback, repeat) { |
|
|
|
var timer = new Timer(); |
|
|
|
|
|
|
|
if (arguments.length > 2) { |
|
|
|
var args = Array.prototype.slice.call(arguments, 2); |
|
|
|
timer.callback = function() { |
|
|
|
callback.apply(timer, args); |
|
|
|
}; |
|
|
|
} else { |
|
|
|
timer.callback = callback; |
|
|
|
var args = Array.prototype.slice.call(arguments, 2); |
|
|
|
timer.ontimeout = function() { |
|
|
|
callback.apply(timer, args); |
|
|
|
} |
|
|
|
|
|
|
|
timer.start(repeat, repeat ? repeat : 1); |
|
|
@ -218,7 +216,7 @@ exports.setInterval = function(callback, repeat) { |
|
|
|
|
|
|
|
exports.clearInterval = function(timer) { |
|
|
|
if (timer instanceof Timer) { |
|
|
|
timer.callback = null; |
|
|
|
timer.stop(); |
|
|
|
timer.ontimeout = null; |
|
|
|
timer.close(); |
|
|
|
} |
|
|
|
}; |
|
|
|