Skip to content

Commit

Permalink
"already ended" state was incorrectly marked
Browse files Browse the repository at this point in the history
  • Loading branch information
dex4er committed Mar 10, 2017
1 parent 3a0dcde commit 3ea3ca3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
40 changes: 20 additions & 20 deletions lib/promise-readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,26 +93,26 @@ class PromiseReadable {
return resolve(null)
}

stream.on('data', onData)
stream.once('end', onceEnd)
stream.once('error', onceError)

function onData (data) {
bufferArray.push(data)
const onData = chunk => {
bufferArray.push(chunk)
}

function onceEnd (data) {
const onceEnd = () => {
stream.removeListener('data', onData)
stream.removeListener('error', onceError)
this._ended = true
resolve(Buffer.concat(bufferArray))
}

function onceError (e) {
const onceError = e => {
stream.removeListener('data', onData)
stream.removeListener('end', onceEnd)
reject(e)
}

stream.on('data', onData)
stream.once('end', onceEnd)
stream.once('error', onceError)
})
}

Expand All @@ -131,28 +131,28 @@ class PromiseReadable {
return resolve(null)
}

stream.once(event, onceEvent)
stream.once('end', onceEnd)
stream.once('error', onceError)

function onceEvent (argument) {
const onceEvent = argument => {
stream.removeListener('end', onceEnd)
stream.removeListener('error', onceError)
resolve(argument)
}

function onceEnd (data) {
const onceEnd = () => {
stream.removeListener(event, onceEvent)
stream.removeListener('error', onceError)
this._ended = true
resolve(null)
}

function onceError (e) {
const onceError = e => {
stream.removeListener(event, onceEvent)
stream.removeListener('end', onceEnd)
reject(e)
}

stream.once(event, onceEvent)
stream.once('end', onceEnd)
stream.once('error', onceError)
})
}

Expand All @@ -163,19 +163,19 @@ class PromiseReadable {
return resolve(null)
}

stream.once('end', onceEnd)
stream.once('error', onceError)

function onceEnd (data) {
const onceEnd = () => {
stream.removeListener('error', onceError)
this._ended = true
resolve(null)
}

function onceError (e) {
const onceError = e => {
stream.removeListener('end', onceEnd)
reject(e)
}

stream.once('end', onceEnd)
stream.once('error', onceError)
})
}
}
Expand Down
8 changes: 6 additions & 2 deletions test/promise-readable-stream1.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,14 +305,18 @@ Feature('Test promise-readable module with stream1 API', () => {
this.promiseReadable = new PromiseReadable(this.stream)
})

When('I call close method', () => {
this.promise = this.promiseReadable.close()
When('I call end method', () => {
this.promiseReadable.end()
})

When('end event is emitted', () => {
this.stream.emit('end')
})

When('I call close method', () => {
this.promise = this.promiseReadable.close()
})

Then('promise returns null value', () => {
return this.promise.should.eventually.to.be.null
})
Expand Down

0 comments on commit 3ea3ca3

Please sign in to comment.