Skip to content
Browse files

improved message handling.

  • Loading branch information...
1 parent 4140fa0 commit 3870adb50b593b5b72b4b2c75a4c61d33ea85b20 Joachim Kainz committed Sep 24, 2011
Showing with 14 additions and 5 deletions.
  1. +14 −5 lib/backgrounder.js
View
19 lib/backgrounder.js
@@ -1,13 +1,18 @@
+var _ = require("underscore");
var cp = require("child_process");
var events = require("events");
var util = require("util");
function processMessage(emitter, message) {
if (message.type === 'console') {
console.log(message.content);
- return;
}
- console.error("unexpected message %s", util.inspect(message));
+ else if (message.type === 'message') {
+ emitter.emit('message', message.content);
+ }
+ else {
+ console.error("unexpected message %s", util.inspect(message));
+ }
}
function Child(mdl, module) {
@@ -18,10 +23,14 @@ function Child(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);
+ var messages = self.buffer.split('\n');
self.buffer = "";
- processMessage(this.emitter, parsed);
+ _.each(messages, function(message){
+ if (message.length > 0) {
+ var parsed = JSON.parse(message);
+ processMessage(self.emitter, parsed);
+ }
+ });
}
});
this.process.stderr.on('data', function(data) {

0 comments on commit 3870adb

Please sign in to comment.
Something went wrong with that request. Please try again.