it-pushable
Advanced tools
Comparing version 1.2.1 to 1.3.0
21
index.js
const FIFO = require('fast-fifo') | ||
module.exports = onEnd => { | ||
module.exports = (options) => { | ||
options = options || {} | ||
let onEnd | ||
if (typeof options === 'function') { | ||
onEnd = options | ||
options = {} | ||
} | ||
let buffer = new FIFO() | ||
@@ -9,2 +17,13 @@ let pushable, onNext, ended | ||
if (!buffer.isEmpty()) { | ||
if (options.writev) { | ||
let next | ||
const values = [] | ||
while (!buffer.isEmpty()) { | ||
next = buffer.shift() | ||
if (next.error) throw next.error | ||
values.push(next.value) | ||
} | ||
return { done: next.done, value: values } | ||
} | ||
const next = buffer.shift() | ||
@@ -11,0 +30,0 @@ if (next.error) throw next.error |
{ | ||
"name": "it-pushable", | ||
"version": "1.2.1", | ||
"version": "1.3.0", | ||
"description": "Pushable iterable", | ||
@@ -19,6 +19,6 @@ "main": "index.js", | ||
"devDependencies": { | ||
"ava": "^1.4.1", | ||
"ava": "^2.2.0", | ||
"it-pipe": "^1.0.1", | ||
"nyc": "^14.0.0", | ||
"standard": "^12.0.1" | ||
"standard": "^13.1.0" | ||
}, | ||
@@ -25,0 +25,0 @@ "dependencies": { |
@@ -42,3 +42,3 @@ # it-pushable | ||
### `pushable([onEnd])` | ||
### `pushable([options])` | ||
@@ -50,4 +50,9 @@ Create a new async iterable. The values yielded from calls to `.next()` or when used in a `for await of` loop are "pushed" into the iterable. Returns an async iterable object with the following additional methods: | ||
The _optional_ parameter `onEnd` is a function called after all values have been yielded from the iterator (including buffered values). In the case when the iterator is ended with an error it will be passed the error as a parameter. | ||
`options` is an _optional_ parameter, an object with the following properties: | ||
* `onEnd` - a function called after _all_ values have been yielded from the iterator (including buffered values). In the case when the iterator is ended with an error it will be passed the error as a parameter. | ||
* `writev` - a boolean used to signal that the consumer of this iterable supports processing multiple buffered chunks at a time. When this option is set to `true` values yielded from the iterable will be arrays. | ||
Note: the `onEnd` function may be passed instead of `options`. | ||
## Contribute | ||
@@ -54,0 +59,0 @@ |
18
test.js
@@ -219,1 +219,19 @@ import test from 'ava' | ||
}) | ||
test('should support writev', async t => { | ||
const source = pushable({ writev: true }) | ||
const input = [1, 2, 3] | ||
input.forEach(v => source.push(v)) | ||
setTimeout(() => source.end()) | ||
const output = await pipe(source, collect) | ||
t.deepEqual(output[0], input) | ||
}) | ||
test('should support writev and end with error', async t => { | ||
const source = pushable({ writev: true }) | ||
const input = [1, 2, 3] | ||
input.forEach(v => source.push(v)) | ||
source.end(new Error('boom')) | ||
const err = await t.throwsAsync(pipe(source, collect)) | ||
t.deepEqual(err.message, 'boom') | ||
}) |
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
12421
313
63