The following code simply increments number asynchronously 1000 times. I assumed the parallel transform emits a finish event after all data consumption but is didn't.
const expect = 1000;
let actual = 0;
const r = createReadable(expect);
class MyTransform extends ParallelStream {
constructor() {
super({objectMode: true, maxParallel: 32, highWaterMark: 32});
}
_parallelTransform(data, enc, callback) {
setTimeout(() => {
console.log(++actual);
callback();
}, Math.random() * 100);
}
}
const t = new MyTransform();
t.on('finish', () => {
assert.strictEqual(actual, expect); // AssertionError [ERR_ASSERTION]: 985 === 1000
});
r.pipe(t);
function createReadable(size) {
let i = 0;
return new stream.Readable({
read: function () {
this.push(i < size ? i++ : null);
},
objectMode: true,
});
}
This is not pleasant especially when we use libs such as pump that calls a callback function depending on finish event.
It seems that it emits finish after all writable data is buffered instead of consumed.
This problem occurs also in parallel-stream module.
Also, when we do readable.pipe(parallelTransform).pipe(writable), writable emits finish earlier than expected.
It is likely that we have to dig more in private methods of Node Stream.
The following code simply increments number asynchronously 1000 times. I assumed the parallel transform emits a
finishevent after all data consumption but is didn't.This is not pleasant especially when we use libs such as
pumpthat calls a callback function depending onfinishevent.It seems that it emits
finishafter all writable data is buffered instead of consumed.This problem occurs also in
parallel-streammodule.Also, when we do
readable.pipe(parallelTransform).pipe(writable), writable emitsfinishearlier than expected.It is likely that we have to dig more in private methods of Node Stream.