简体   繁体   English

暂停和缓冲流

[英]gulp pause and buffer stream

I have a stream of files, and at some point i need to pause my stream, wait until it finish and buffers, and then continue. 我有文件流,在某个时候我需要暂停我的流,等待它完成并缓冲,然后继续。

Example: 例:

var eventStream = require('event-stream')
gulp.task('test', () => {
    eventStream.readArray([1, 2, 3, 4, 5])
        .pipe(gulpTap((data) => {
            console.log('d1:', data);
        }))
        .pipe(gulpTap((data) => {
            console.log('d2:', data);
        }))
        .on('end', function () {
            console.log('ended');
        });
})

prints: 打印:
d1 1 d1 1
d2 1 d2 1
d1 2 d1 2
d2 2 d2 2
d1 3 d1 3
d2 3 d2 3
ended 结束

when i want it to be like: 当我希望它像:
d1 1 d1 1
d1 2 d1 2
d1 3 d1 3
d2 1 d2 1
d2 2 d2 2
d2 3 d2 3

The reason here is that i want to collect some data from all files in one object, and then provide it to other objects, so i need a some kind of sync in the middle of pipe chain 原因是我想从一个对象的所有文件中收集一些数据,然后将其提供给其他对象,因此我需要在管道链中间进行某种同步

You can do this with the help of through2 , eg: 您可以在through2的帮助下完成此through2 ,例如:

const eventStream = require('event-stream');
const through = require('through2');

gulp.task('test', () => {
    const input = [];

    eventStream.readArray([1, 2, 3, 4, 5])
        .pipe(through.obj((data, enc, done) => {
            // Save data and remove from stream
            input.push(data);

            console.log('d1:', data);
            return done();
        }, function(done) {
            // Return "buffered" data back to stream
            input.forEach(this.push.bind(this));

            return done();
        }))
        .pipe(through.obj((data, enc, done) => {
            console.log('d2:', data);
            return done(null, data);
        }))
        .on('end', function () {
            console.log('ended');
        });
});

Real-life example: https://github.com/sirlantis/gulp-order 真实示例: https//github.com/sirlantis/gulp-order

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

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