promise-readable
Advanced tools
Comparing version 0.3.0 to 0.4.0
# Changelog | ||
## v0.4.0 2017-03-14 | ||
* New method `once` replaces other `once*` methods. | ||
## v0.3.0 2017-03-11 | ||
@@ -4,0 +8,0 @@ |
@@ -119,11 +119,3 @@ 'use strict' | ||
onceOpen () { | ||
return this._onceEvent('open') | ||
} | ||
onceClose () { | ||
return this._onceEvent('close') | ||
} | ||
_onceEvent (event) { | ||
once (event) { | ||
const stream = this.stream | ||
@@ -135,10 +127,12 @@ return new Promise((resolve, reject) => { | ||
const onceEvent = argument => { | ||
const onceEvent = event !== 'end' ? argument => { | ||
stream.removeListener('end', onceEnd) | ||
stream.removeListener('error', onceError) | ||
resolve(argument) | ||
} | ||
} : undefined | ||
const onceEnd = () => { | ||
stream.removeListener(event, onceEvent) | ||
if (event !== 'end') { | ||
stream.removeListener(event, onceEvent) | ||
} | ||
stream.removeListener('error', onceError) | ||
@@ -150,3 +144,5 @@ this._ended = true | ||
const onceError = e => { | ||
stream.removeListener(event, onceEvent) | ||
if (event !== 'end') { | ||
stream.removeListener(event, onceEvent) | ||
} | ||
stream.removeListener('end', onceEnd) | ||
@@ -156,26 +152,5 @@ reject(e) | ||
stream.once(event, onceEvent) | ||
stream.once('end', onceEnd) | ||
stream.once('error', onceError) | ||
}) | ||
} | ||
onceEnd () { | ||
const stream = this.stream | ||
return new Promise((resolve, reject) => { | ||
if (this._ended) { | ||
return resolve(null) | ||
if (event !== 'end') { | ||
stream.once(event, onceEvent) | ||
} | ||
const onceEnd = () => { | ||
stream.removeListener('error', onceError) | ||
this._ended = true | ||
resolve(null) | ||
} | ||
const onceError = e => { | ||
stream.removeListener('end', onceEnd) | ||
reject(e) | ||
} | ||
stream.once('end', onceEnd) | ||
@@ -182,0 +157,0 @@ stream.once('error', onceError) |
{ | ||
"name": "promise-readable", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "Return promise for readable stream", | ||
@@ -5,0 +5,0 @@ "main": "lib/promise-readable.js", |
@@ -28,2 +28,4 @@ ## promise-readable | ||
#### constructor(stream) | ||
`PromiseReadable` object requires `Readable` object to work: | ||
@@ -45,3 +47,3 @@ | ||
#### read | ||
#### read([chunkSize]) | ||
@@ -71,3 +73,3 @@ This method returns `Promise` which is fulfilled when stream can return one | ||
#### readAll | ||
#### readAll() | ||
@@ -82,34 +84,15 @@ This method returns `Promise` which is fulfilled when stream is ended. The | ||
#### onceOpen | ||
#### once(event) | ||
This method returns `Promise` which is fulfilled when stream is opened. File | ||
descriptor is returned. It works only for | ||
[`fd.ReadStream`](https://nodejs.org/api/fs.html#fs_class_fs_readstream) | ||
streams. It returns `null` if stream was already ended. | ||
This method returns `Promise` which is fulfilled when stream emits `event`. The | ||
result of this event is returned or `null` value if stream is already ended. | ||
```js | ||
const fd = await promiseRstream.onceOpen() | ||
const fd = await promiseRstream.once('open') | ||
promiseRstream.stream.pipe(process.stdout) | ||
``` | ||
#### onceClose | ||
await promiseRstream.once('close') | ||
This method returns `Promise` which is fulfilled when stream is closed. | ||
`undefined` value is returned. It works only for | ||
[`fd.ReadStream`](https://nodejs.org/api/fs.html#fs_class_fs_readstream) | ||
streams. It returns `null` if stream was already ended. | ||
```js | ||
await promiseRstream.onceClose() | ||
``` | ||
#### onceEnd | ||
This method returns `Promise` which is fulfilled when stream is ended. No value | ||
is returned. It might be used when stream is handled with `data` event directly. | ||
```js | ||
promiseRstream.stream.on('data', chunk => console.log(chunk.length)) | ||
await promiseRstream.onceEnd() | ||
await promiseRstream.once('end') | ||
``` | ||
@@ -116,0 +99,0 @@ |
@@ -221,3 +221,3 @@ 'use strict' | ||
When('I call open method', () => { | ||
this.promise = this.promiseReadable.onceOpen() | ||
this.promise = this.promiseReadable.once('open') | ||
}) | ||
@@ -244,3 +244,3 @@ | ||
When('I call open method', () => { | ||
this.promise = this.promiseReadable.onceOpen() | ||
this.promise = this.promiseReadable.once('open') | ||
}) | ||
@@ -267,3 +267,3 @@ | ||
When('I call open method', () => { | ||
this.promise = this.promiseReadable.onceOpen() | ||
this.promise = this.promiseReadable.once('open') | ||
}) | ||
@@ -290,3 +290,3 @@ | ||
When('I call close method', () => { | ||
this.promise = this.promiseReadable.onceClose() | ||
this.promise = this.promiseReadable.once('close') | ||
}) | ||
@@ -313,3 +313,3 @@ | ||
When('I call end method', () => { | ||
this.promiseReadable.onceEnd() | ||
this.promiseReadable.once('end') | ||
}) | ||
@@ -322,3 +322,3 @@ | ||
When('I call close method', () => { | ||
this.promise = this.promiseReadable.onceClose() | ||
this.promise = this.promiseReadable.once('close') | ||
}) | ||
@@ -341,3 +341,3 @@ | ||
When('I call end method', () => { | ||
this.promise = this.promiseReadable.onceClose() | ||
this.promise = this.promiseReadable.once('close') | ||
}) | ||
@@ -364,3 +364,3 @@ | ||
When('I call end method', () => { | ||
this.promise = this.promiseReadable.onceEnd() | ||
this.promise = this.promiseReadable.once('end') | ||
}) | ||
@@ -403,3 +403,3 @@ | ||
When('I call end method', () => { | ||
this.promise = this.promiseReadable.onceEnd() | ||
this.promise = this.promiseReadable.once('end') | ||
}) | ||
@@ -422,3 +422,3 @@ | ||
When('I call end method', () => { | ||
this.promise = this.promiseReadable.onceEnd() | ||
this.promise = this.promiseReadable.once('end') | ||
}) | ||
@@ -425,0 +425,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
27064
652
114