簡體   English   中英

暫停和緩沖流

[英]gulp pause and buffer stream

我有文件流,在某個時候我需要暫停我的流,等待它完成並緩沖,然后繼續。

例:

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');
        });
})

打印:
d1 1
d2 1
d1 2
d2 2
d1 3
d2 3
結束

當我希望它像:
d1 1
d1 2
d1 3
d2 1
d2 2
d2 3

原因是我想從一個對象的所有文件中收集一些數據,然后將其提供給其他對象,因此我需要在管道鏈中間進行某種同步

您可以在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');
        });
});

真實示例: https//github.com/sirlantis/gulp-order

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM