javascript - gulp 暂停和缓冲流

标签 javascript node.js gulp

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

示例:

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 的帮助下完成此操作,例如:

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

关于javascript - gulp 暂停和缓冲流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32460937/

相关文章:

javascript - 如何向 selectize.js 添加/删除类?

node.js - 使用现有套接字的 Node http请求

javascript - 如果任何端口 9229 有什么意义,可以更改吗?

javascript - Gulp imagemin优化去除svg符号

javascript - Gulp LiveReload 崩溃

用于在多行文本区域中搜索表达式的 Javascript 正则表达式

javascript - 没有 JSX 的 React.js - "Warning: Something is calling a React component directly. Use a factory or JSX instead"

javascript - 取消选中一个复选框,取消选中所有其他复选框不工作

mysql - 获取列数据并插入到另一列中给出的 id 的行中 - sql nodejs

node.js - 从 gulp 运行时在 WebStorm 中调试 Node 应用程序