Skip to content

Commit

Permalink
Can call destroy twice
Browse files Browse the repository at this point in the history
  • Loading branch information
dex4er committed Feb 5, 2018
1 parent bfeee4d commit 56d3b1c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,9 @@
# Changelog

## v3.1.1 2018-02-05

* Can call `destroy` twice.

## v3.1.0 2018-02-04

* Support `import PromiseWritable from 'promise-writable'` syntax.
Expand Down
14 changes: 9 additions & 5 deletions lib/promise-writable.js
Expand Up @@ -257,12 +257,16 @@ class PromiseWritable {
}

destroy () {
this.stream.removeListener('error', this._errorHandler)
delete this._errorHandler
if (typeof this.stream.destroy === 'function') {
this.stream.destroy()
if (this.stream) {
if (this._errorHandler) {
this.stream.removeListener('error', this._errorHandler)
delete this._errorHandler
}
if (typeof this.stream.destroy === 'function') {
this.stream.destroy()
}
delete this.stream
}
delete this.stream
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "promise-writable",
"version": "3.1.0",
"version": "3.1.1",
"description": "Return promise for writable stream",
"main": "lib/promise-writable.js",
"typings": "lib/promise-writable.d.ts",
Expand Down
24 changes: 24 additions & 0 deletions test/promise-writable.js
Expand Up @@ -95,6 +95,14 @@ Feature('Test promise-writable module', () => {
And('stream should contain another chunk', () => {
stream._buffer.should.deep.equal(Buffer.from('chunk1chunk2'))
})

And('PromiseWritable object can be destroyed', () => {
promiseWritable.destroy()
})

And('PromiseWritable object can be destroyed', () => {
promiseWritable.destroy()
})
})

Scenario('Write chunks to stream which pauses', () => {
Expand Down Expand Up @@ -205,6 +213,14 @@ Feature('Test promise-writable module', () => {
Then('promise is rejected', () => {
return error.should.be.an('error', 'write after end')
})

And('PromiseWritable object can be destroyed', () => {
promiseWritable.destroy()
})

And('PromiseWritable object can be destroyed', () => {
promiseWritable.destroy()
})
})

Scenario('Write chunk to stream with error', () => {
Expand All @@ -231,6 +247,14 @@ Feature('Test promise-writable module', () => {
Then('promise is rejected', () => {
return promise.should.be.rejectedWith(Error, 'boom')
})

And('PromiseWritable object can be destroyed', () => {
promiseWritable.destroy()
})

And('PromiseWritable object can be destroyed', () => {
promiseWritable.destroy()
})
})

Scenario('Write chunk to stream with emitted error', () => {
Expand Down

0 comments on commit 56d3b1c

Please sign in to comment.