From 55c3da9821706ff12470152f6e9525bd3a94cfd5 Mon Sep 17 00:00:00 2001 From: Vincent Gire Date: Fri, 21 Apr 2017 19:07:57 +0200 Subject: [PATCH 1/2] Allow Throttle to be piped to some time after instanciation --- test/throttle.js | 20 ++++++++++++++++++++ throttle.js | 4 +++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/test/throttle.js b/test/throttle.js index cc3ef16..020fb0b 100644 --- a/test/throttle.js +++ b/test/throttle.js @@ -167,6 +167,26 @@ describe('Throttle', function () { r.pipe(t); }); + it('should work if being piped to some time after instanciation', function (done) { + var r = new Random(1024); + var t = new Throttle(1024); + setTimeout( + function () { + var start = Date.now(); + var bytes = 0; + t.on('data', function (data) { + bytes += data.length; + }); + t.on('end', function () { + assertTimespan(start, new Date(), 1000); + assert.equal(1024, bytes); + done(); + }); + r.pipe(t); + }, + 200); + }); + }); function assertTimespan (start, end, expected, tolerance) { diff --git a/throttle.js b/throttle.js index 2526a06..e98c6bd 100644 --- a/throttle.js +++ b/throttle.js @@ -57,7 +57,9 @@ function Throttle (opts) { this.chunkSize = Math.max(1, opts.chunkSize); this.totalBytes = 0; - this.startTime = Date.now(); + this.once('pipe', () => { + this.startTime = Date.now(); + }); this._passthroughChunk(); } From 2676decf95d9a48580783d3d0957a9aea1a9149f Mon Sep 17 00:00:00 2001 From: Vincent Gire Date: Sat, 22 Apr 2017 07:19:59 +0200 Subject: [PATCH 2/2] Allow Throttle to be piped to some time after instanciation --- throttle.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/throttle.js b/throttle.js index e98c6bd..e2358eb 100644 --- a/throttle.js +++ b/throttle.js @@ -59,9 +59,8 @@ function Throttle (opts) { this.totalBytes = 0; this.once('pipe', () => { this.startTime = Date.now(); + this._passthroughChunk(); }); - - this._passthroughChunk(); } inherits(Throttle, Transform);