Skip to content

Commit

Permalink
stream: use arrow fns for 'this' in readable
Browse files Browse the repository at this point in the history
PR-URL: #16927
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
Reviewed-By: Anatoli Papirovski <[email protected]>
Reviewed-By: Gireesh Punathil <[email protected]>
Reviewed-By: Franziska Hinkelmann <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
  • Loading branch information
vipinmenon authored and MylesBorins committed Dec 11, 2017
1 parent 24bb99a commit 78b82b0
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions lib/_stream_readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -856,19 +856,18 @@ Readable.prototype.wrap = function(stream) {
var state = this._readableState;
var paused = false;

var self = this;
stream.on('end', function() {
stream.on('end', () => {
debug('wrapped end');
if (state.decoder && !state.ended) {
var chunk = state.decoder.end();
if (chunk && chunk.length)
self.push(chunk);
this.push(chunk);
}

self.push(null);
this.push(null);
});

stream.on('data', function(chunk) {
stream.on('data', (chunk) => {
debug('wrapped data');
if (state.decoder)
chunk = state.decoder.write(chunk);
Expand All @@ -879,7 +878,7 @@ Readable.prototype.wrap = function(stream) {
else if (!state.objectMode && (!chunk || !chunk.length))
return;

var ret = self.push(chunk);
var ret = this.push(chunk);
if (!ret) {
paused = true;
stream.pause();
Expand All @@ -900,20 +899,20 @@ Readable.prototype.wrap = function(stream) {

// proxy certain important events.
for (var n = 0; n < kProxyEvents.length; n++) {
stream.on(kProxyEvents[n], self.emit.bind(self, kProxyEvents[n]));
stream.on(kProxyEvents[n], this.emit.bind(this, kProxyEvents[n]));
}

// when we try to consume some more bytes, simply unpause the
// underlying stream.
self._read = function(n) {
this._read = (n) => {
debug('wrapped _read', n);
if (paused) {
paused = false;
stream.resume();
}
};

return self;
return this;
};

Object.defineProperty(Readable.prototype, 'readableHighWaterMark', {
Expand Down

0 comments on commit 78b82b0

Please sign in to comment.