Skip to content

Commit d3bb40d

Browse files
committed
Merge pull request #43 from calvinmetcalf/arguments
let process.nextTick accept arguments
2 parents 295f3e1 + 51549be commit d3bb40d

File tree

2 files changed

+76
-25
lines changed

2 files changed

+76
-25
lines changed

browser.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function drainQueue() {
3030
currentQueue = queue;
3131
queue = [];
3232
while (++queueIndex < len) {
33-
currentQueue[queueIndex]();
33+
currentQueue[queueIndex].run();
3434
}
3535
queueIndex = -1;
3636
len = queue.length;
@@ -41,12 +41,26 @@ function drainQueue() {
4141
}
4242

4343
process.nextTick = function (fun) {
44-
queue.push(fun);
44+
var args = new Array(arguments.length - 1);
45+
if (arguments.length > 1) {
46+
for (var i = 1; i < arguments.length; i++) {
47+
args[i - 1] = arguments[i];
48+
}
49+
}
50+
queue.push(new Item(fun, args));
4551
if (!draining) {
4652
setTimeout(drainQueue, 0);
4753
}
4854
};
4955

56+
// v8 likes predictible objects
57+
function Item(fun, array) {
58+
this.fun = fun;
59+
this.array = array;
60+
}
61+
Item.prototype.run = function () {
62+
this.fun.apply(null, this.array);
63+
};
5064
process.title = 'browser';
5165
process.browser = true;
5266
process.env = {};

test.js

Lines changed: 60 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,66 @@
1-
var ourProcess = require('./browser');
21
var assert = require('assert');
2+
var ourProcess = require('./browser');
3+
describe('test against process', function () {
4+
test(process);
5+
});
6+
if (!process.browser) {
7+
describe('test against our shim', function () {
8+
test(ourProcess);
9+
});
10+
}
11+
function test (ourProcess) {
12+
describe('test arguments', function (t) {
13+
it ('works', function (done) {
14+
var order = 0;
15+
316

4-
describe('test errors', function (t) {
5-
it ('works', function (done) {
6-
var order = 0;
7-
process.removeAllListeners('uncaughtException');
8-
process.once('uncaughtException', function(err) {
9-
assert.equal(2, order++, 'error is third');
10-
process.nextTick(function () {
11-
assert.equal(5, order++, 'schedualed in error is last');
12-
done();
17+
ourProcess.nextTick(function (num) {
18+
assert.equal(num, order++, 'first one works');
19+
ourProcess.nextTick(function (num) {
20+
assert.equal(num, order++, 'recursive one is 4th');
21+
}, 3);
22+
}, 0);
23+
ourProcess.nextTick(function (num) {
24+
assert.equal(num, order++, 'second one starts');
25+
ourProcess.nextTick(function (num) {
26+
assert.equal(num, order++, 'this is third');
27+
ourProcess.nextTick(function (num) {
28+
assert.equal(num, order++, 'this is last');
29+
done();
30+
}, 5);
31+
}, 4);
32+
}, 1);
33+
ourProcess.nextTick(function (num) {
34+
35+
assert.equal(num, order++, '3rd schedualed happens after the error');
36+
}, 2);
1337
});
1438
});
15-
process.nextTick(function () {
16-
assert.equal(0, order++, 'first one works');
17-
process.nextTick(function () {
18-
assert.equal(4, order++, 'recursive one is 4th');
39+
40+
describe('test errors', function (t) {
41+
it ('works', function (done) {
42+
var order = 0;
43+
process.removeAllListeners('uncaughtException');
44+
process.once('uncaughtException', function(err) {
45+
assert.equal(2, order++, 'error is third');
46+
ourProcess.nextTick(function () {
47+
assert.equal(5, order++, 'schedualed in error is last');
48+
done();
49+
});
50+
});
51+
ourProcess.nextTick(function () {
52+
assert.equal(0, order++, 'first one works');
53+
ourProcess.nextTick(function () {
54+
assert.equal(4, order++, 'recursive one is 4th');
55+
});
56+
});
57+
ourProcess.nextTick(function () {
58+
assert.equal(1, order++, 'second one starts');
59+
throw(new Error('an error is thrown'));
60+
});
61+
ourProcess.nextTick(function () {
62+
assert.equal(3, order++, '3rd schedualed happens after the error');
63+
});
1964
});
2065
});
21-
process.nextTick(function () {
22-
assert.equal(1, order++, 'second one starts');
23-
throw(new Error('an error is thrown'));
24-
});
25-
process.nextTick(function () {
26-
assert.equal(3, order++, '3rd schedualed happens after the error');
27-
});
28-
});
29-
});
66+
}

0 commit comments

Comments
 (0)