5 var pna = require('process-nextick-args');
8 // undocumented cb() API, needed for core, not for public API
9 function destroy(err, cb) {
12 var readableDestroyed = this._readableState && this._readableState.destroyed;
13 var writableDestroyed = this._writableState && this._writableState.destroyed;
15 if (readableDestroyed || writableDestroyed) {
18 } else if (err && (!this._writableState || !this._writableState.errorEmitted)) {
19 pna.nextTick(emitErrorNT, this, err);
24 // we set destroyed to true before firing error callbacks in order
25 // to make it re-entrance safe in case destroy() is called within callbacks
27 if (this._readableState) {
28 this._readableState.destroyed = true;
31 // if this is a duplex stream mark the writable part as destroyed as well
32 if (this._writableState) {
33 this._writableState.destroyed = true;
36 this._destroy(err || null, function (err) {
38 pna.nextTick(emitErrorNT, _this, err);
39 if (_this._writableState) {
40 _this._writableState.errorEmitted = true;
50 function undestroy() {
51 if (this._readableState) {
52 this._readableState.destroyed = false;
53 this._readableState.reading = false;
54 this._readableState.ended = false;
55 this._readableState.endEmitted = false;
58 if (this._writableState) {
59 this._writableState.destroyed = false;
60 this._writableState.ended = false;
61 this._writableState.ending = false;
62 this._writableState.finished = false;
63 this._writableState.errorEmitted = false;
67 function emitErrorNT(self, err) {
68 self.emit('error', err);