Permalink
Browse files

Use setImmediate if available

  • Loading branch information...
1 parent 5e715f5 commit e406cc44ab882e258c37e826f198d677465fcfb4 @chriso chriso committed Mar 12, 2013
Showing with 22 additions and 14 deletions.
  1. +3 −3 Makefile
  2. +3 −3 lib/node.io/io.js
  3. +2 −2 lib/node.io/process_worker.js
  4. +4 −1 lib/node.io/utils.js
  5. +6 −3 test/io.test.js
  6. +2 −1 test/job.test.js
  7. +2 −1 test/processor.test.js
View
@@ -1,7 +1,7 @@
test:
- @expresso -I lib --growl test/*.test.js
+ @node_modules/expresso/bin/expresso -I lib --growl test/*.test.js
test-cov:
- @expresso -I lib --cov test/*.test.js
+ @node_modules/expresso/bin/expresso -I lib --cov test/*.test.js
-.PHONY: test test-cov
+.PHONY: test test-cov
View
@@ -255,7 +255,7 @@ Job.prototype.takeInputStreamLines = function (start, num, callback, read_id) {
var return_lines = function () {
if (read_id !== last_read_id && !self.input_stream.end) {
//Wait for a previous request to return lines
- process.nextTick(return_lines);
+ utils.tick(return_lines);
} else {
last_read_id++;
callback(self.input_stream.lines.length ? self.input_stream.lines.splice(0, num) : false);
@@ -264,7 +264,7 @@ Job.prototype.takeInputStreamLines = function (start, num, callback, read_id) {
return_lines();
} else {
- process.nextTick(function () {
+ utils.tick(function () {
self.takeInputStreamLines(start, num, callback, read_id);
});
}
@@ -395,7 +395,7 @@ Job.prototype.write = function (file, data, callback) {
if (write_id !== last_write_id) {
//Wait for a previous requests
- process.nextTick(write_lines);
+ utils.tick(write_lines);
} else {
last_write_id++;
@@ -83,7 +83,7 @@ Processor.prototype.setupWorkerEvents = function (job, master) {
});
worker.on('process', function () {
- process.nextTick(function () {
+ utils.tick(function () {
if (job.is_complete) return;
if (job.ready_to_request_input && job.input.length < (job.options.max * job.options.take * job.options.worker_input_mult)) {
@@ -100,7 +100,7 @@ Processor.prototype.setupWorkerEvents = function (job, master) {
//We might be done, although hanging instances might add some more input
//using add(), so add another check on the next tick
- process.nextTick(function () {
+ utils.tick(function () {
if (job.input.length === 0 && !job.is_complete) {
worker.emit('complete');
}
View
@@ -65,7 +65,7 @@ exports.removeOnExit = function (file) {
process.exit();
} catch (e) {}
};
- ['SIGINT', 'SIGTERM', 'SIGKILL', 'SIGQUIT', 'SIGHUP', 'exit'].forEach(function (signal) {
+ ['SIGINT', 'SIGTERM', 'SIGQUIT', 'SIGHUP', 'exit'].forEach(function (signal) {
process.on(signal, removeFile);
});
};
@@ -369,3 +369,6 @@ exports.getFiles = function (root_path, callback) {
});
});
};
+
+exports.tick = typeof setImmediate !== 'undefined' ? setImmediate : process.nextTick;
+
View
@@ -1,5 +1,6 @@
var fs = require('fs'),
nodeio = require('../lib/node.io'),
+ utils = require('../lib/node.io/utils'),
processor = new nodeio.Processor(),
JobClass = nodeio.JobClass,
assert = require('assert');
@@ -131,9 +132,11 @@ module.exports = {
});
job.write(output, [1,2,[3,4,5]], function() {
- assert.equal(2, i++);
- assert.equal('test\n{"foo":"bar","bar":"foo"}\n1\n2\n[3,4,5]\n', fs.readFileSync(output, 'utf8'));
- assert.equal(43, job.getBytesWritten());
+ utils.tick(function () {
+ assert.equal(2, i++);
+ assert.equal('test\n{"foo":"bar","bar":"foo"}\n1\n2\n[3,4,5]\n', fs.readFileSync(output, 'utf8'));
+ assert.equal(43, job.getBytesWritten());
+ });
});
},
View
@@ -1,4 +1,5 @@
var nodeio = require('../lib/node.io'),
+ utils = require('../lib/node.io/utils'),
processor = new nodeio.Processor(),
assert = require('assert');
@@ -78,7 +79,7 @@ module.exports = {
var job = createJob();
job.run = function() {
- process.nextTick(function() {
+ utils.tick(function() {
job.emit(1);
});
}
View
@@ -1,4 +1,5 @@
var nodeio = require('../lib/node.io'),
+ utils = require('../lib/node.io/utils'),
processor = new nodeio.Processor(),
assert = require('assert');
@@ -59,7 +60,7 @@ module.exports = {
input: [0,1,2],
run: function() {
var self = this;
- process.nextTick(function() {
+ utils.tick(function() {
self.emit(1);
});
},

0 comments on commit e406cc4

Please sign in to comment.