diff --git a/lib/Parser.js b/lib/Parser.js index bed817d..4e8c807 100644 --- a/lib/Parser.js +++ b/lib/Parser.js @@ -40,8 +40,6 @@ function Parser(stream, debug) { this._ignoreReadable = false; this.debug = debug; - this.setStream(stream); - var self = this; this._cbReadable = function() { if (self._ignoreReadable) @@ -51,12 +49,18 @@ function Parser(stream, debug) { else self._tryread(); }; - this._stream.on('readable', this._cbReadable); + + this.setStream(stream); + process.nextTick(this._cbReadable); } inherits(Parser, EventEmitter); Parser.prototype.setStream = function(stream) { + if (this._stream) { + this._stream.removeListener('readable', this._cbReadable); + } + if (/^v0\.8\./.test(process.version)) { this._stream = (new ReadableStream()).wrap(stream); @@ -66,6 +70,8 @@ Parser.prototype.setStream = function(stream) { stream._events.error.pop(); } else this._stream = stream; + + this._stream.on('readable', this._cbReadable); }; Parser.prototype._tryread = function(n) {