Browse Source

Use timer_wrap instead of the old timer binding

v0.7.4-release
Bert Belder 14 years ago
parent
commit
d9aa9b54cf
  1. 4
      lib/dns.js
  2. 96
      lib/timers.js
  3. 4
      src/timer_wrap.cc

4
lib/dns.js

@ -26,11 +26,11 @@ var IOWatcher = process.binding('io_watcher').IOWatcher;
var watchers = {};
var activeWatchers = {};
var Timer = process.binding('timer').Timer;
var Timer = process.binding('timer_wrap').Timer;
var timer = new Timer();
timer.callback = function() {
timer.ontimeout = function() {
var sockets = Object.keys(activeWatchers);
for (var i = 0, l = sockets.length; i < l; i++) {
var socket = sockets[i];

96
lib/timers.js

@ -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();
}
};

4
src/timer_wrap.cc

@ -182,8 +182,6 @@ class TimerWrap {
uv_timer_set_repeat(&wrap->handle_, repeat);
wrap->StateChange();
return scope.Close(Integer::New(0));
}
@ -196,8 +194,6 @@ class TimerWrap {
if (repeat < 0) SetErrno(uv_last_error().code);
wrap->StateChange();
return scope.Close(Integer::New(repeat));
}

Loading…
Cancel
Save