Permalink
Browse files

reformatting code

  • Loading branch information...
1 parent 1810991 commit 0d8b427adcae1c2e94d51c415d63f547e02f8f7f Joachim Kainz committed Sep 25, 2011
Showing with 83 additions and 105 deletions.
  1. +58 −68 lib/backgrounder-launcher.js
  2. +24 −34 lib/backgrounder.js
  3. +1 −3 tests/master.js
@@ -2,95 +2,85 @@ var util = require('util');
var events = require("events");
function format(f) {
- if (typeof f !== 'string') {
- var objects = [];
- for (var i = 0; i < arguments.length; i++) {
- objects.push(util.inspect(arguments[i]));
+ var i;
+ if (typeof f !== 'string') {
+ var objects = [];
+ for (i = 0; i < arguments.length; i++) {
+ objects.push(util.inspect(arguments[i]));
+ }
+ return objects.join(' ');
}
- return objects.join(' ');
- }
-
-
- var i = 1;
- var args = arguments;
- var str = String(f).replace(/%[sdj]/g, function(x) {
- switch (x) {
- case '%s': return String(args[i++]);
- case '%d': return Number(args[i++]);
- case '%j': return JSON.stringify(args[i++]);
- default:
- return x;
+ i = 1;
+ var args = arguments;
+ var str = String(f).replace(/%[sdj]/g, function(x) {
+ switch (x) {
+ case '%s':
+ return String(args[i++]);
+ case '%d':
+ return Number(args[i++]);
+ case '%j':
+ return JSON.stringify(args[i++]);
+ default:
+ return x;
+ }
+ });
+ for (var len = args.length, x = args[i]; i < len; x = args[++i]) {
+ if (x === null || typeof x !== 'object') {
+ str += ' ' + x;
+ }
+ else {
+ str += ' ' + util.inspect(x);
+ }
}
- });
- for (var len = args.length, x = args[i]; i < len; x = args[++i]) {
- if (x === null || typeof x !== 'object') {
- str += ' ' + x;
- } else {
- str += ' ' + util.inspect(x);
- }
- }
- return str;
+ return str;
}
function log(message) {
- var _message = {"type": "console", "content" : message};
- var json = JSON.stringify(_message);
-
- process.stdout.write(json + '\n');
+ var _message = {
+ "type": "console",
+ "content": message
+ };
+ var json = JSON.stringify(_message);
+ process.stdout.write(json + '\n');
}
-
console.log = function() {
- var message = format.apply(this, arguments);
-
- log(message);
+ var message = format.apply(this, arguments);
+ log(message);
};
-
console.dir = function(object) {
- var message = util.inspect(object);
-
- log(message);
+ var message = util.inspect(object);
+ log(message);
};
-
var times = {};
console.time = function(label) {
- times[label] = Date.now();
+ times[label] = Date.now();
};
-
-
console.timeEnd = function(label) {
- var duration = Date.now() - times[label]
-
- console.log('%s: %dms', label, duration);
+ var duration = Date.now() - times[label];
+ console.log('%s: %dms', label, duration);
};
-
var buffer = "";
var emitter = new events.EventEmitter();
-
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function(data) {
- buffer += data.toString();
-
- if (buffer.substr(-1) === '\n') {
- var parsed = JSON.parse(buffer);
- buffer = "";
-
- emitter.emit("message", parsed);
- }
-})
-
+ buffer += data.toString();
+ if (buffer.substr(-1) === '\n') {
+ var parsed = JSON.parse(buffer);
+ buffer = "";
+ emitter.emit("message", parsed);
+ }
+});
process.on = function(message, listener) {
- emitter.on(message, listener);
-}
-
+ emitter.on(message, listener);
+};
process.send = function(message) {
- var json = JSON.stringify({"type":"message", "content" : message});
-
- process.stdout.write(json + '\n');
-}
-
+ var json = JSON.stringify({
+ "type": "message",
+ "content": message
+ });
+ process.stdout.write(json + '\n');
+};
console.info = console.log;
-
var module = process.argv[2];
-
-require(module);
+require(module);
View
@@ -7,49 +7,39 @@ function processMessage(emitter, message) {
console.log(message.content);
return;
}
-
console.error("unexpected message %s", util.inspect(message));
}
function Child(mdl, module) {
- var self = this;
-
- this.buffer = "";
- this.emitter = new events.EventEmitter();
- this.process = cp.spawn('node', [mdl, module]);
- this.process.stdout.on('data', function(data) {
- self.buffer += data.toString();
-
- if (self.buffer.substr(-1) === '\n') {
- console.error("self.buffer: %s", self.buffer);
- var parsed = JSON.parse(self.buffer);
- self.buffer = "";
-
- processMessage(this.emitter, parsed);
- }
- })
- this.process.stderr.on('data', function(data) {
- var _data = data.toString();
-
- if (_data.substr(-1) === '\n') {
- _data = _data.substr(0, _data.length - 1);
- }
-
- console.error(_data);
- })
+ var self = this;
+ this.buffer = "";
+ this.emitter = new events.EventEmitter();
+ this.process = cp.spawn('node', [mdl, module]);
+ this.process.stdout.on('data', function(data) {
+ self.buffer += data.toString();
+ if (self.buffer.substr(-1) === '\n') {
+ console.error("self.buffer: %s", self.buffer);
+ var parsed = JSON.parse(self.buffer);
+ self.buffer = "";
+ processMessage(this.emitter, parsed);
+ }
+ });
+ this.process.stderr.on('data', function(data) {
+ var _data = data.toString();
+ if (_data.substr(-1) === '\n') {
+ _data = _data.substr(0, _data.length - 1);
+ }
+ console.error(_data);
+ });
}
-
Child.prototype.on = function(event, listener) {
- this.emitter.on(event, listener);
+ this.emitter.on(event, listener);
};
Child.prototype.send = function(message) {
- var json = JSON.stringify(message);
-
- this.process.stdin.write(json + '\n');
+ var json = JSON.stringify(message);
+ this.process.stdin.write(json + '\n');
};
-
module.exports.spawn = function(module) {
var mdl = __dirname + '/backgrounder-launcher.js';
-
return new Child(mdl, module);
-};
+};
View
@@ -1,12 +1,10 @@
var util = require("util");
var backgrounder = require("../lib/backgrounder");
var worker = backgrounder.spawn(__dirname + "/worker.js");
-
worker.on("message", function(message) {
console.log(util.inspect(message, false, 100));
});
-
worker.send({
"title": "hello world!",
"flag": true
-});
+});

0 comments on commit 0d8b427

Please sign in to comment.