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..e2358eb 100644 --- a/throttle.js +++ b/throttle.js @@ -57,9 +57,10 @@ function Throttle (opts) { this.chunkSize = Math.max(1, opts.chunkSize); this.totalBytes = 0; - this.startTime = Date.now(); - - this._passthroughChunk(); + this.once('pipe', () => { + this.startTime = Date.now(); + this._passthroughChunk(); + }); } inherits(Throttle, Transform);