Browse files

lib: simplify connection hopping requirements a bit

  • Loading branch information...
1 parent fe005ed commit 9fd2ed215bd5be68d6d041be87ab73dd8d18c598 @mscdex committed Mar 4, 2014
Showing with 14 additions and 1 deletion.
  1. +13 −1 lib/Channel.js
  2. +1 −0 lib/Connection.js
View
14 lib/Channel.js
@@ -502,7 +502,10 @@ module.exports = Channel;
function ChannelStream(channel) {
- var self = this;
+ var self = this,
+ emittedFinish = false,
+ emittedClose = false,
+ emittedEnd = false;
this.readable = true;
this.writable = true;
this.paused = false;
@@ -513,11 +516,20 @@ function ChannelStream(channel) {
this._inbuffer = [];
this._decoder = undefined;
this._sockOnEnd = function() {
+ !emittedFinish && self.emit('finish');
+ !emittedEnd && self.emit('end');
+ !emittedClose && self.emit('close');
self.writable = false;
self.readable = false;
channel._conn._sock.removeListener('end', self._sockOnEnd);
channel._conn._sock.removeListener('close', self._sockOnEnd);
};
+ function onFinish() { emittedFinish = true; }
+ this.once('finish', onFinish);
+ function onEnd() { emittedEnd = true; }
+ this.once('end', onEnd);
+ function onClose() { emittedClose = true; }
+ this.once('close', onClose);
channel._conn._sock.once('end', this._sockOnEnd);
channel._conn._sock.once('close', this._sockOnEnd);
}
View
1 lib/Connection.js
@@ -203,6 +203,7 @@ Connection.prototype.connect = function(opts) {
|| (typeof this._pingInterval === 'number' && this._pingInterval < 0))
this._pingInterval = 60000;
+ opts.sock && process.nextTick(function() { opts.sock.emit('connect'); });
this._sock = (opts.sock ? opts.sock : new Socket());
this._channels = [];
this._buffer = [];

0 comments on commit 9fd2ed2

Please sign in to comment.