简体   繁体   English

压缩两个可读流似乎不会产生任何结果

[英]Zipping two readable streams does not seem to produce anything

I have the following function: 我有以下功能:

const _ = require('highland');

module.exports =
    (numbers /* Readable */, words /* Readable */, repeated_words /* Writable */) => {
        const numberStream = _(numbers);
        const wordStream = _(words);
        numberStream
            .zip(wordStream)
            .flatMap((numberWordPair) => {
                const result = [];
                for (let i = 0; i < numberWordPair[0]; i++) {
                    result.push(numberWordPair[1]);
                }
                return _(result);
            })
            .pipe(repeated_words);
    };

The stream arguments are automatically injected and I am 100% sure the stream injection works (other streaming functions work). 流参数是自动注入的,我100%确定流注入有效(其他流函数有效)。

When I replace this somewhat complex transformation by something as straightforward as _(numbers).each(xs => {console.log(xs)}) , I can see the data being logged. 当我用_(numbers).each(xs => {console.log(xs)})简单明了的东西替换这个有点复杂的转换时,我可以看到正在记录的数据。

However, here, I must be clearly missing something with Highland.js as there is nothing produced at all. 但是,在这里, Highland.js必须明显缺少一些东西,因为根本没有产生任何东西。

I'm using version 2.13.5 of Highland.js. 我正在使用Highland.js 2.13.5版本。

What am I missing? 我想念什么?

It appears it all works properly, therefore the mistake must lie somewhere else. 看来一切正常,因此错误必须出在其他地方。 As a proof, I was able to run this little program: 作为证明,我能够运行以下小程序:

const {TextDecoder} = require('util');
const repeater = require('./lib/repeater');
const {PassThrough, Readable, Transform} = require('stream');

class ConstantSource extends Readable {

    constructor(options, constant) {
        super(options);
        this.constant = constant;
    }

    _read(size) {
        for (let i = 0; i < size; i++) {
            this.push(this.constant);
        }
    }
}

class StdinSource extends Readable {

    constructor(options) {
        super(options);
        this.decoder = new TextDecoder('utf8');
        process.stdin.on('data', (chunk) => {
            this.push(this.decoder.decode(chunk).trim());
        });
    }

    _read(size) {

    }
}

class StdoutSink extends Transform {

    constructor(options) {
        super(options);
        this.pipe(process.stdout);
    }

    _transform(chunk, _, callback) {
        callback(null, '\x1b[33m' + chunk + '\x1b[0m\n');
    }
}

const main = () => {
    if (process.argv.length < 3) {
        console.error('Please specify a number.');
        process.exit(1);
    }

    const options = {objectMode: true};
    const inputStream1 = new ConstantSource(options, process.argv[2]);
    const inputStream2 = new StdinSource(options);
    const outputStream = new StdoutSink(options);

    repeater(inputStream1, inputStream2, outputStream);
}

main();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM