From 3ea3ca3e0f40a173de998c708070182c303f3b91 Mon Sep 17 00:00:00 2001 From: Piotr Roszatycki Date: Fri, 10 Mar 2017 12:17:39 +0100 Subject: [PATCH] "already ended" state was incorrectly marked --- lib/promise-readable.js | 40 ++++++++++++++++---------------- test/promise-readable-stream1.js | 8 +++++-- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/lib/promise-readable.js b/lib/promise-readable.js index ba008f9..c79f2f1 100644 --- a/lib/promise-readable.js +++ b/lib/promise-readable.js @@ -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) }) } @@ -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) }) } @@ -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) }) } } diff --git a/test/promise-readable-stream1.js b/test/promise-readable-stream1.js index 49032b0..26dc4cc 100644 --- a/test/promise-readable-stream1.js +++ b/test/promise-readable-stream1.js @@ -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 })