Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add .destroy to Readable and Writable prototypes #125

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions lib/_stream_readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ function ReadableState(options, stream) {
this.decoder = new StringDecoder(options.encoding);
this.encoding = options.encoding;
}

this.destroyed = false;
}

function Readable(options) {
Expand Down Expand Up @@ -152,6 +154,28 @@ Readable.prototype.isPaused = function() {
return this._readableState.flowing === false;
};

Readable.prototype.destroy = function() {
var self = this;
if (this._readableState.destroyed) return;
this._readableState.destroyed = true;

// if this is a duplex stream mark the writable part as destroyed as well
if (this._writableState) {
this._writableState.destroyed = true;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sort of tempted to say "may as well specially implement destroy on Duplex" just to avoid adding another case of the parent class having intimate knowledge of its subclass.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea i think thats good idea. will update the pr

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's almost a shame we couldn't add this to the base stream class somehow since they are both doing pretty much the same thing.

Better still if there was a base StreamState that all states inherited from, although that would restrict people from calling _destroy() directly, however that might not be such a bad thing...


this._destroy(function(err) {
if (err) {
self.emit('error', err);
}
self.emit('close');
});
};

Readable.prototype._destroy = function (callback) {
process.nextTick(callback)
};

function readableAddChunk(stream, state, chunk, encoding, addToFront) {
var er = chunkInvalid(state, chunk);
if (er) {
Expand Down
19 changes: 19 additions & 0 deletions lib/_stream_writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ function WritableState(options, stream) {

// True if the error was already emitted and should not be thrown again
this.errorEmitted = false;

this.destroyed = false;
}

WritableState.prototype.getBuffer = function writableStateGetBuffer() {
Expand Down Expand Up @@ -246,6 +248,23 @@ Writable.prototype.uncork = function() {
}
};

// More or less the same destroy method as in Readable
Writable.prototype.destroy = function() {
var self = this;
if (this._writableState.destroyed) return;
this._writableState.destroyed = true;
this._destroy(function(err) {
if (err) {
self.emit('error', err);
}
self.emit('close');
});
};

Writable.prototype._destroy = function (callback) {
process.nextTick(callback);
};

Writable.prototype.setDefaultEncoding = function setDefaultEncoding(encoding) {
// node::ParseEncoding() requires lower case.
if (typeof encoding === 'string')
Expand Down