Use timer_wrap instead of the old timer binding

This commit is contained in:
Bert Belder 2011-06-08 04:35:50 +02:00
parent 5c9a262eb0
commit d9aa9b54cf
3 changed files with 50 additions and 56 deletions

View File

@ -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];

View File

@ -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;
} else {
timer = { _idleTimeout: after, _onTimeout: callback };
timer._idlePrev = timer;
timer._idleNext = timer;
}
timer.ontimeout = callback;
/*
* 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() {
args = Array.prototype.slice.call(arguments, 2);
timer._onTimeout = function() {
callback.apply(timer, args);
};
if (timer instanceof Timer) {
timer.callback = c;
} else {
timer._onTimeout = c;
timer.close();
}
}
if (timer instanceof Timer) {
timer.start(0, 0);
} else {
timer = { _idleTimeout: after };
timer._idlePrev = timer;
timer._idleNext = timer;
if (arguments.length <= 2) {
timer._onTimeout = callback;
} else {
/*
* 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);
}
}
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();
}
};

View File

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