【问题标题】:Combining streams node组合流节点
【发布时间】:2014-07-17 19:55:26
【问题描述】:

这个函数的返回需要是一个流。在非手表模式下,这很容易;只需返回重新捆绑,browserify 流被转换,等等等等,好的。但是,在监视模式下,每次更新都会运行重新捆绑,并且每次都会创建一个新流。我需要一种方法来整合所有这些流,因为它们被创建成一个我可以返回的无限流,并且实际上可以在生产线上消耗掉。使用组合流,似乎一旦读取数据,流就不再是可写的,所以这是不行的。任何帮助将不胜感激!

var bundleify = function(watch) {

    var bundler = (watch?watchify:browserify)('main.js');

    var rebundle = function () {
        return bundler.bundle()
            .on('error', console.log)
            .pipe(source('main.js'))
            .pipe(rename('app.js'))
            .pipe(jsTasks()); // lazypipe with other tasks
    };

    // Regular browserify, just return the stream.
    if (!watch) {
        return rebundle();
    }

    // Watchify, rebundle on update.
    bundler.on('update', function() {
        rebundle();
    });

    // return ????
}

【问题讨论】:

  • 我对您要完成的工作感到困惑。您是否正在尝试执行诸如连接一堆流之类的操作,以便将它们视为字符串(其中一个流完全是第一个,然后是下一个流,然后是下一个)?或者你想交错他们的数据?

标签: javascript node.js stream browserify watchify


【解决方案1】:

这是我想出的解决方案。这是一个非常贫民区(诚然,我对流没有很好的理解),但看起来它正在工作。仍然有兴趣找到更好的方法。

var outstream = through2.obj();
var interceptor = function(){
    return through2.obj(function(obj, enc, cb) {
        outstream.push(obj);
        cb()
    });
}

bundler.on('update', function() {
    rebundle().pipe(interceptor());
});

rebundle().pipe(interceptor());
return outstream;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-15
    • 2013-01-16
    • 2019-07-02
    • 1970-01-01
    • 2019-04-11
    • 2014-06-23
    • 1970-01-01
    • 2014-09-02
    相关资源
    最近更新 更多