Skip to content

Commit

Permalink
factor out protocol listening into _start() so it's easier to hack ar…
Browse files Browse the repository at this point in the history
…ound it for .listen()
  • Loading branch information
James Halliday committed Jun 19, 2012
1 parent 1783f00 commit b59d0d7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
1 change: 1 addition & 0 deletions index.js
Expand Up @@ -56,6 +56,7 @@ dnode.prototype.listen = function () {

// just copy over the opts and cons, the rest will need to be re-created
var cons = self.cons, opts = self.opts;
self.cons = function () {};
self.end();

var params = parseArgs(arguments);
Expand Down
30 changes: 20 additions & 10 deletions lib/dnode.js
Expand Up @@ -22,36 +22,46 @@ function dnode (cons, opts) {
: function () { return cons || {} }
;

self.readable = true;
self.writable = true;
self._line = '';

process.nextTick(function () {
if (self._ended) return;
self._start();
});
}

dnode.prototype._start = function () {
var self = this;

var proto = self.proto = protocol(function (remote) {
if (self._ended) return;

var ref = self.cons.call(this, remote, self);
if (typeof ref !== 'object') ref = this;

self.emit('local', ref, self);

return ref;
}).create();

self.readable = true;
self.writable = true;

self._line = '';

proto.on('remote', function (remote) {
self.emit('remote', remote, self);
self.emit('ready'); // backwards compatability, deprecated
});

proto.on('request', function (req) {
if (!self.readable) return;

if (opts.emit === 'object') {
if (self.opts.emit === 'object') {
self.emit('data', req);
}
else self.emit('data', json.stringify(req) + '\n');
});

process.nextTick(function () {
proto.start();
});
}
proto.start();
};

dnode.prototype.write = function (buf) {
var row;
Expand Down

0 comments on commit b59d0d7

Please sign in to comment.