gulp暂停和缓冲流

时间:2015-09-08 14:51:09

标签: javascript node.js gulp

我有一个文件流,在某些时候我需要暂停我的流,等到它完成并缓冲,然后继续。

示例:

select a.*
from #temp1 a
where exists (select 1 from #temp2 b where b.id2 = a.id1) or
      not exists (select 1 from #temp2 b);

打印:
d1 1
d2 1
d1 2
d2 2
d1 3
d2 3
结束了

当我想要它时: d1 1
d1 2
d1 3
d2 1
d2 2
d2 3

这里的原因是我想从一个对象中的所有文件中收集一些数据,然后将其提供给其他对象,所以我需要在管道链中间进行某种同步

1 个答案:

答案 0 :(得分:1)

您可以在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

相关问题