Skip to content

Commit e406cc4

Browse files
committed
Use setImmediate if available
1 parent 5e715f5 commit e406cc4

File tree

7 files changed

+22
-14
lines changed

7 files changed

+22
-14
lines changed

Makefile

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
test:
2-
@expresso -I lib --growl test/*.test.js
2+
@node_modules/expresso/bin/expresso -I lib --growl test/*.test.js
33

44
test-cov:
5-
@expresso -I lib --cov test/*.test.js
5+
@node_modules/expresso/bin/expresso -I lib --cov test/*.test.js
66

7-
.PHONY: test test-cov
7+
.PHONY: test test-cov

lib/node.io/io.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ Job.prototype.takeInputStreamLines = function (start, num, callback, read_id) {
255255
var return_lines = function () {
256256
if (read_id !== last_read_id && !self.input_stream.end) {
257257
//Wait for a previous request to return lines
258-
process.nextTick(return_lines);
258+
utils.tick(return_lines);
259259
} else {
260260
last_read_id++;
261261
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) {
264264
return_lines();
265265

266266
} else {
267-
process.nextTick(function () {
267+
utils.tick(function () {
268268
self.takeInputStreamLines(start, num, callback, read_id);
269269
});
270270
}
@@ -395,7 +395,7 @@ Job.prototype.write = function (file, data, callback) {
395395
if (write_id !== last_write_id) {
396396

397397
//Wait for a previous requests
398-
process.nextTick(write_lines);
398+
utils.tick(write_lines);
399399

400400
} else {
401401
last_write_id++;

lib/node.io/process_worker.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ Processor.prototype.setupWorkerEvents = function (job, master) {
8383
});
8484

8585
worker.on('process', function () {
86-
process.nextTick(function () {
86+
utils.tick(function () {
8787
if (job.is_complete) return;
8888

8989
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) {
100100

101101
//We might be done, although hanging instances might add some more input
102102
//using add(), so add another check on the next tick
103-
process.nextTick(function () {
103+
utils.tick(function () {
104104
if (job.input.length === 0 && !job.is_complete) {
105105
worker.emit('complete');
106106
}

lib/node.io/utils.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ exports.removeOnExit = function (file) {
6565
process.exit();
6666
} catch (e) {}
6767
};
68-
['SIGINT', 'SIGTERM', 'SIGKILL', 'SIGQUIT', 'SIGHUP', 'exit'].forEach(function (signal) {
68+
['SIGINT', 'SIGTERM', 'SIGQUIT', 'SIGHUP', 'exit'].forEach(function (signal) {
6969
process.on(signal, removeFile);
7070
});
7171
};
@@ -369,3 +369,6 @@ exports.getFiles = function (root_path, callback) {
369369
});
370370
});
371371
};
372+
373+
exports.tick = typeof setImmediate !== 'undefined' ? setImmediate : process.nextTick;
374+

test/io.test.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var fs = require('fs'),
22
nodeio = require('../lib/node.io'),
3+
utils = require('../lib/node.io/utils'),
34
processor = new nodeio.Processor(),
45
JobClass = nodeio.JobClass,
56
assert = require('assert');
@@ -131,9 +132,11 @@ module.exports = {
131132
});
132133

133134
job.write(output, [1,2,[3,4,5]], function() {
134-
assert.equal(2, i++);
135-
assert.equal('test\n{"foo":"bar","bar":"foo"}\n1\n2\n[3,4,5]\n', fs.readFileSync(output, 'utf8'));
136-
assert.equal(43, job.getBytesWritten());
135+
utils.tick(function () {
136+
assert.equal(2, i++);
137+
assert.equal('test\n{"foo":"bar","bar":"foo"}\n1\n2\n[3,4,5]\n', fs.readFileSync(output, 'utf8'));
138+
assert.equal(43, job.getBytesWritten());
139+
});
137140
});
138141
},
139142

test/job.test.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var nodeio = require('../lib/node.io'),
2+
utils = require('../lib/node.io/utils'),
23
processor = new nodeio.Processor(),
34
assert = require('assert');
45

@@ -78,7 +79,7 @@ module.exports = {
7879
var job = createJob();
7980

8081
job.run = function() {
81-
process.nextTick(function() {
82+
utils.tick(function() {
8283
job.emit(1);
8384
});
8485
}

test/processor.test.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var nodeio = require('../lib/node.io'),
2+
utils = require('../lib/node.io/utils'),
23
processor = new nodeio.Processor(),
34
assert = require('assert');
45

@@ -59,7 +60,7 @@ module.exports = {
5960
input: [0,1,2],
6061
run: function() {
6162
var self = this;
62-
process.nextTick(function() {
63+
utils.tick(function() {
6364
self.emit(1);
6465
});
6566
},

0 commit comments

Comments
 (0)