Skip to content

Commit 1fdb850

Browse files
committed
feat: time remaining will return infinity if average speed is zero
1 parent 8bfadb4 commit 1fdb850

File tree

4 files changed

+67
-50
lines changed

4 files changed

+67
-50
lines changed

src/flow.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -589,11 +589,16 @@
589589
* @returns {number}
590590
*/
591591
timeRemaining: function () {
592-
var time = 0;
592+
var sizeDelta = 0;
593+
var averageSpeed = 0;
593594
each(this.files, function (file) {
594-
time += file.timeRemaining();
595+
sizeDelta += file.size - file.sizeUploaded();
596+
averageSpeed += file.averageSpeed;
595597
});
596-
return time;
598+
if (!averageSpeed) {
599+
return Number.POSITIVE_INFINITY;
600+
}
601+
return sizeDelta / averageSpeed;
597602
}
598603
};
599604

@@ -708,8 +713,11 @@
708713
* @function
709714
*/
710715
measureSpeed: function () {
711-
var smoothingFactor = this.flowObj.opts.speedSmoothingFactor;
712716
var timeSpan = Date.now() - this._lastProgressCallback;
717+
if (!timeSpan) {
718+
return ;
719+
}
720+
var smoothingFactor = this.flowObj.opts.speedSmoothingFactor;
713721
var uploaded = this.sizeUploaded();
714722
// Prevent negative upload speed after file upload resume
715723
this.currentSpeed = Math.max((uploaded - this._prevUploadedSize) / timeSpan * 1000, 0);
@@ -722,7 +730,7 @@
722730
* Callback when something happens within the chunk.
723731
* @function
724732
* @param {string} event can be 'progress', 'success', 'error' or 'retry'
725-
* @param {string} message
733+
* @param {string} [message]
726734
*/
727735
chunkEvent: function (event, message) {
728736
switch (event) {
@@ -746,8 +754,10 @@
746754
if (this.error) {
747755
return;
748756
}
757+
this.measureSpeed();
749758
this.flowObj.fire('fileProgress', this);
750759
this.flowObj.fire('progress');
760+
this._lastProgressCallback = Date.now();
751761
if (this.isComplete()) {
752762
this.flowObj.fire('fileSuccess', this, message);
753763
}
@@ -915,7 +925,7 @@
915925
*/
916926
timeRemaining: function () {
917927
if (!this.averageSpeed) {
918-
return 0;
928+
return Number.POSITIVE_INFINITY;
919929
}
920930
return Math.floor(Math.max(this.size - this.sizeUploaded(), 0) / this.averageSpeed);
921931
},
@@ -972,7 +982,7 @@
972982
* File size
973983
* @type {number}
974984
*/
975-
this. fileObjSize = fileObj.size;
985+
this.fileObjSize = fileObj.size;
976986

977987
/**
978988
* File offset

test/setupSpec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ describe('setup', function() {
3939

4040
expect(flow.progress()).toBe(0);
4141
expect(flow.isUploading()).toBe(false);
42-
expect(flow.timeRemaining()).toBe(0);
42+
expect(flow.timeRemaining()).toBe(Number.POSITIVE_INFINITY);
4343
expect(flow.sizeUploaded()).toBe(0);
4444
});
4545

test/uploadSpec.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,4 +380,53 @@ describe('upload file', function() {
380380
expect(success).wasCalledWith(file, "response");
381381
expect(error).not.toHaveBeenCalled();
382382
});
383+
384+
it('should have upload speed', function() {
385+
var clock = sinon.useFakeTimers();
386+
flow.opts.testChunks = false;
387+
flow.opts.speedSmoothingFactor = 0.5;
388+
flow.opts.simultaneousUploads = 1;
389+
var fileProgress = jasmine.createSpy('fileProgress');
390+
flow.on('fileProgress', fileProgress);
391+
flow.addFile(new Blob(['0123456789']));
392+
flow.addFile(new Blob(['12345']));
393+
var fileFirst = flow.files[0];
394+
var fileSecond = flow.files[1];
395+
expect(fileFirst.currentSpeed).toBe(0);
396+
expect(fileFirst.averageSpeed).toBe(0);
397+
expect(fileFirst.sizeUploaded()).toBe(0);
398+
expect(fileFirst.timeRemaining()).toBe(Number.POSITIVE_INFINITY);
399+
expect(flow.sizeUploaded()).toBe(0);
400+
expect(flow.timeRemaining()).toBe(Number.POSITIVE_INFINITY);
401+
flow.upload();
402+
403+
clock.tick(1000);
404+
requests[0].progress(5, 10, true);
405+
expect(fileProgress).toHaveBeenCalled();
406+
expect(fileFirst.currentSpeed).toBe(5);
407+
expect(fileFirst.averageSpeed).toBe(2.5);
408+
expect(fileFirst.sizeUploaded()).toBe(5);
409+
expect(fileFirst.timeRemaining()).toBe(2);
410+
411+
expect(flow.sizeUploaded()).toBe(5);
412+
expect(flow.timeRemaining()).toBe(4);
413+
414+
clock.tick(1000);
415+
requests[0].progress(10, 10, true);
416+
expect(fileFirst.currentSpeed).toBe(5);
417+
expect(fileFirst.averageSpeed).toBe(3.75);
418+
419+
requests[0].respond(200, [], "response");
420+
expect(fileFirst.currentSpeed).toBe(5);
421+
expect(fileFirst.averageSpeed).toBe(3.75);
422+
423+
requests[1].respond(200, [], "response");
424+
expect(fileFirst.sizeUploaded()).toBe(10);
425+
expect(fileFirst.timeRemaining()).toBe(0);
426+
expect(fileSecond.sizeUploaded()).toBe(5);
427+
expect(fileSecond.timeRemaining()).toBe(0);
428+
expect(flow.sizeUploaded()).toBe(15);
429+
expect(flow.timeRemaining()).toBe(0);
430+
431+
});
383432
});

test/uploadSpeedSpec.js

Lines changed: 0 additions & 42 deletions
This file was deleted.

0 commit comments

Comments
 (0)