diff options
author | isaacs <i@izs.me> | 2012-11-17 15:24:14 +1100 |
---|---|---|
committer | isaacs <i@izs.me> | 2012-12-13 17:00:32 -0800 |
commit | b15e19a2324c3dc11192ad3d4b66e88d37b136f2 (patch) | |
tree | aa44bc127e1ff46c246791d015cbdd7c4f6d22db /lib/_stream_transform.js | |
parent | 2ff499c022942e22fffc74709a2a99990bbec8ba (diff) | |
download | node-new-b15e19a2324c3dc11192ad3d4b66e88d37b136f2.tar.gz |
streams2: Remove function.bind() usage
It's too slow, unfortunately.
Diffstat (limited to 'lib/_stream_transform.js')
-rw-r--r-- | lib/_stream_transform.js | 48 |
1 files changed, 26 insertions, 22 deletions
diff --git a/lib/_stream_transform.js b/lib/_stream_transform.js index cf1c2e3b0e..b0819de29a 100644 --- a/lib/_stream_transform.js +++ b/lib/_stream_transform.js @@ -70,10 +70,13 @@ var Duplex = require('_stream_duplex'); var util = require('util'); util.inherits(Transform, Duplex); -function TransformState() { +function TransformState(stream) { this.buffer = []; this.transforming = false; this.pendingReadCb = null; + this.output = function(chunk) { + stream._output(chunk); + }; } function Transform(options) { @@ -83,17 +86,19 @@ function Transform(options) { Duplex.call(this, options); // bind output so that it can be passed around as a regular function. - this._output = this._output.bind(this); + var stream = this; // the queue of _write chunks that are pending being transformed - this._transformState = new TransformState(); + var ts = this._transformState = new TransformState(stream); // when the writable side finishes, then flush out anything remaining. this.once('finish', function() { if ('function' === typeof this._flush) - this._flush(this._output, done.bind(this)); + this._flush(ts.output, function(er) { + done(stream, er); + }); else - done.call(this); + done(stream); }); } @@ -159,14 +164,13 @@ Transform.prototype._read = function(n, readcb) { var req = ts.buffer.shift(); var chunk = req[0]; var writecb = req[1]; - var output = this._output; ts.transforming = true; - this._transform(chunk, output, function(er, data) { + this._transform(chunk, ts.output, function(er, data) { ts.transforming = false; if (data) - output(data); + ts.output(data); writecb(er); - }.bind(this)); + }); }; Transform.prototype._output = function(chunk) { @@ -185,25 +189,25 @@ Transform.prototype._output = function(chunk) { } // otherwise, it's up to us to fill the rs buffer. - var state = this._readableState; - var len = state.length; - state.buffer.push(chunk); - state.length += chunk.length; - if (state.needReadable) { - state.needReadable = false; + var rs = this._readableState; + var len = rs.length; + rs.buffer.push(chunk); + rs.length += chunk.length; + if (rs.needReadable) { + rs.needReadable = false; this.emit('readable'); } }; -function done(er) { +function done(stream, er) { if (er) - return this.emit('error', er); + return stream.emit('error', er); // if there's nothing in the write buffer, then that means // that nothing more will ever be provided - var ws = this._writableState; - var rs = this._readableState; - var ts = this._transformState; + var ws = stream._writableState; + var rs = stream._readableState; + var ts = stream._transformState; if (ws.length) throw new Error('calling transform done when ws.length != 0'); @@ -221,7 +225,7 @@ function done(er) { // no more data coming from the writable side, we need to emit // now so that the consumer knows to pick up the tail bits. if (rs.length && rs.needReadable) - this.emit('readable'); + stream.emit('readable'); else if (rs.length === 0) - this.emit('end'); + stream.emit('end'); } |