This commit is contained in:
Jerry Kurian
2019-08-22 14:52:39 -04:00
parent d097fa6aa5
commit 1d0e15890c
5 changed files with 59 additions and 57 deletions

View File

@@ -19,4 +19,4 @@ export { replace } from "./replace";
export { split } from "./split";
export { stringify } from "./stringify";
export { unbatch } from "./unbatch";
export { compose, composeDuplex } from "./compose";
export { compose } from "./compose";

View File

@@ -2,7 +2,6 @@ import {
pipeline,
Transform,
Writable,
Pipe,
WritableOptions,
Readable,
Duplex,
@@ -12,7 +11,6 @@ import {
* Return a Readable stream of readable streams concatenated together
* @param streams Readable streams to concatenate
*/
// First Readable --> Readable
// First Transform | Duplex, Last Writable --> Writable
//
@@ -22,26 +20,23 @@ export function compose(
): Duplex {
// Maybe just return a new stream here
if (streams.length < 2) {
throw new Error("Not enough");
throw new Error("At least two streams are required to compose");
}
const first = streams[0] as Writable;
const last = streams[streams.length - 1] as Readable;
const duplex = new Duplex({
objectMode: true,
...options,
write(chunk, enc, cb) {
const first = streams[0] as Writable;
if (!first.write(chunk)) {
first.on("drain", cb);
first.once("drain", cb);
} else {
cb();
}
},
read(size) {
let chunk;
while (
null !==
(chunk = (streams[streams.length - 1] as Readable).read())
) {
this.push(chunk);
if (last.readable) {
this.push(last.read(size));
}
},
});

View File

@@ -297,9 +297,3 @@ export function compose(
options,
);
}
export function composeDuplex(
streams: Array<Writable | Transform>,
options?: WritableOptions,
) {
return baseFunctions.composeDuplex(streams, options);
}

View File

@@ -22,5 +22,4 @@ export {
accumulator,
accumulatorBy,
compose,
composeDuplex,
} from "./functions";