From 686b806aa521f092f9d8d27ea2b8266b4080eb0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9e=20Kooi?= Date: Thu, 22 Mar 2018 10:31:55 +0100 Subject: [PATCH] Wait for factored pipelines to drain before ending main pipeline The upgrade to labeled-stream-splicer@2 in https://github.com/browserify/factor-bundle/commit/532b1e48397a8a2580fa87c6a867b866069ad1b5 broke a test, because of some fun times with stream end timings. The factored files were not written fully by the time the common bundle was finished. This patch ensures that all entries are fully written before the main bundle stream completes. This way you can reliably use the `bundle.on('end')` event as well. --- index.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/index.js b/index.js index 7367e97..cb54e9e 100644 --- a/index.js +++ b/index.js @@ -72,12 +72,20 @@ module.exports = function f (b, opts) { return [ outpipe(outopt, xopts) ]; } + var ended = 0, numStreams = 0, endedcb; + function onpipelineend () { + ended++; + if (ended === numStreams && endedcb) endedcb(); + } var pipelines = files.reduce(function (acc, x, ix) { var pipeline = splicer.obj([ 'pack', [ pack(packOpts) ], 'wrap', [] ]); + numStreams++; + pipeline.on('end', onpipelineend); + if (ix >= outputs.length) { outputs.push.apply(outputs, moreOutputs(x)); } @@ -99,6 +107,15 @@ module.exports = function f (b, opts) { bundle.pipe(pipelines[bundle.file]); }); + // Wait for all pipelines to drain before ending the main stream. + var wait = through.obj(function (row, enc, next) { + next(null, row); + }, function (done) { + if (ended === numStreams) done(); + else endedcb = done; + }); + + b.pipeline.get('pack').unshift(wait); b.pipeline.get('pack').unshift(s); if (needRecords) files = [];