Skip to content

Commit

Permalink
let process accept arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
calvinmetcalf committed Apr 23, 2015
1 parent 295f3e1 commit 51549be
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 25 deletions.
18 changes: 16 additions & 2 deletions browser.js
Expand Up @@ -30,7 +30,7 @@ function drainQueue() {
currentQueue = queue;
queue = [];
while (++queueIndex < len) {
currentQueue[queueIndex]();
currentQueue[queueIndex].run();
}
queueIndex = -1;
len = queue.length;
Expand All @@ -41,12 +41,26 @@ function drainQueue() {
}

process.nextTick = function (fun) {
queue.push(fun);
var args = new Array(arguments.length - 1);
if (arguments.length > 1) {
for (var i = 1; i < arguments.length; i++) {
args[i - 1] = arguments[i];
}
}
queue.push(new Item(fun, args));
if (!draining) {
setTimeout(drainQueue, 0);
}
};

// v8 likes predictible objects
function Item(fun, array) {
this.fun = fun;
this.array = array;
}
Item.prototype.run = function () {
this.fun.apply(null, this.array);
};
process.title = 'browser';
process.browser = true;
process.env = {};
Expand Down
83 changes: 60 additions & 23 deletions test.js
@@ -1,29 +1,66 @@
var ourProcess = require('./browser');
var assert = require('assert');
var ourProcess = require('./browser');
describe('test against process', function () {
test(process);
});
if (!process.browser) {
describe('test against our shim', function () {
test(ourProcess);
});
}
function test (ourProcess) {
describe('test arguments', function (t) {
it ('works', function (done) {
var order = 0;


describe('test errors', function (t) {
it ('works', function (done) {
var order = 0;
process.removeAllListeners('uncaughtException');
process.once('uncaughtException', function(err) {
assert.equal(2, order++, 'error is third');
process.nextTick(function () {
assert.equal(5, order++, 'schedualed in error is last');
done();
ourProcess.nextTick(function (num) {
assert.equal(num, order++, 'first one works');
ourProcess.nextTick(function (num) {
assert.equal(num, order++, 'recursive one is 4th');
}, 3);
}, 0);
ourProcess.nextTick(function (num) {
assert.equal(num, order++, 'second one starts');
ourProcess.nextTick(function (num) {
assert.equal(num, order++, 'this is third');
ourProcess.nextTick(function (num) {
assert.equal(num, order++, 'this is last');
done();
}, 5);
}, 4);
}, 1);
ourProcess.nextTick(function (num) {

assert.equal(num, order++, '3rd schedualed happens after the error');
}, 2);
});
});
process.nextTick(function () {
assert.equal(0, order++, 'first one works');
process.nextTick(function () {
assert.equal(4, order++, 'recursive one is 4th');

describe('test errors', function (t) {
it ('works', function (done) {
var order = 0;
process.removeAllListeners('uncaughtException');
process.once('uncaughtException', function(err) {
assert.equal(2, order++, 'error is third');
ourProcess.nextTick(function () {
assert.equal(5, order++, 'schedualed in error is last');
done();
});
});
ourProcess.nextTick(function () {
assert.equal(0, order++, 'first one works');
ourProcess.nextTick(function () {
assert.equal(4, order++, 'recursive one is 4th');
});
});
ourProcess.nextTick(function () {
assert.equal(1, order++, 'second one starts');
throw(new Error('an error is thrown'));
});
ourProcess.nextTick(function () {
assert.equal(3, order++, '3rd schedualed happens after the error');
});
});
});
process.nextTick(function () {
assert.equal(1, order++, 'second one starts');
throw(new Error('an error is thrown'));
});
process.nextTick(function () {
assert.equal(3, order++, '3rd schedualed happens after the error');
});
});
});
}

0 comments on commit 51549be

Please sign in to comment.