Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions test/throttle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
7 changes: 4 additions & 3 deletions throttle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down