2014-07-17 64 views
1

該函數的返回需要是一個流。非手錶模式很容易;只是返回rebundle,browserify流轉換,等等等等,好的。但是,在監視模式下,每次更新都會運行rebundle並每次創建一個新流。我需要一種方法來將所有這些流創建成一個無盡的流,我可以返回,實際上可以消耗在線下。使用組合流播放時,似乎只要讀取了數據,數據流便不再可寫,所以這是不可行的。任何幫助,將不勝感激!合併流節點

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 ???? 
} 
+0

我很困惑你想要完成什麼。你是否試圖做一些事情,如串聯一堆流,使它們像字符串一樣對待(一個流完全是第一個,然後是下一個流,然後是下一個流)?還是你想交錯數據? –

回答

0

這是我想出的解決方案。這是相當貧民窟(坦率地說,我不瞭解流),但它看起來像在工作。仍然有興趣找到更好的方法。

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;