Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
timers: remember extra setTimeout() arguments when timeout==0
Browse files Browse the repository at this point in the history
Fixes #2079.
  • Loading branch information
bnoordhuis committed Nov 12, 2011
1 parent 8082858 commit 098fef6
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 16 deletions.
27 changes: 11 additions & 16 deletions lib/timers.js
Expand Up @@ -144,19 +144,23 @@ exports.active = function(item) {


exports.setTimeout = function(callback, after) {
var timer, c, args;
var timer;

if (after <= 0) {
// Use the slow case for after == 0
timer = new Timer();
timer.ontimeout = callback;

args = Array.prototype.slice.call(arguments, 2);
timer._onTimeout = function() {
callback.apply(timer, args);
timer.close();
if (arguments.length <= 2) {
timer._onTimeout = callback;
} else {
var args = Array.prototype.slice.call(arguments, 2);
timer._onTimeout = function() {
callback.apply(timer, args);
timer.close();
}
}

timer.ontimeout = timer._onTimeout;
timer.start(0, 0);
} else {
timer = { _idleTimeout: after };
Expand All @@ -166,16 +170,7 @@ exports.setTimeout = function(callback, after) {
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);
var args = Array.prototype.slice.call(arguments, 2);
timer._onTimeout = function() {
callback.apply(timer, args);
}
Expand Down
58 changes: 58 additions & 0 deletions test/simple/test-timers-zero-timeout.js
@@ -0,0 +1,58 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

var common = require('../common');
var assert = require('assert');

// https://github.com/joyent/node/issues/2079 - zero timeout drops extra args
(function() {
var ncalled = 0;

setTimeout(f, 0, 'foo', 'bar', 'baz');

function f(a, b, c) {
assert.equal(a, 'foo');
assert.equal(b, 'bar');
assert.equal(c, 'baz');
ncalled++;
}

process.on('exit', function() {
assert.equal(ncalled, 1);
});
})();

(function() {
var ncalled = 0;

var iv = setInterval(f, 0, 'foo', 'bar', 'baz');

function f(a, b, c) {
assert.equal(a, 'foo');
assert.equal(b, 'bar');
assert.equal(c, 'baz');
if (++ncalled == 3) clearTimeout(iv);
}

process.on('exit', function() {
assert.equal(ncalled, 3);
});
})();

0 comments on commit 098fef6

Please sign in to comment.