Skip to content

Commit 97812f9

Browse files
committed
fix: time remaining function returns negative value
1 parent 4d7630c commit 97812f9

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

src/flow.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@
599599
if (!averageSpeed) {
600600
return Number.POSITIVE_INFINITY;
601601
}
602-
return sizeDelta / averageSpeed;
602+
return Math.floor(sizeDelta / averageSpeed);
603603
}
604604
};
605605

@@ -909,12 +909,7 @@
909909
sizeUploaded: function () {
910910
var size = 0;
911911
each(this.chunks, function (chunk) {
912-
// can't sum only chunk.loaded values, because it is bigger than chunk size
913-
if (chunk.status() === 'success') {
914-
size += chunk.endByte - chunk.startByte;
915-
} else {
916-
size += chunk.loaded;
917-
}
912+
size += chunk.sizeUploaded();
918913
});
919914
return size;
920915
},
@@ -929,7 +924,7 @@
929924
if (!this.averageSpeed) {
930925
return Number.POSITIVE_INFINITY;
931926
}
932-
return Math.floor(Math.max(this.size - this.sizeUploaded(), 0) / this.averageSpeed);
927+
return Math.floor((this.size - this.sizeUploaded()) / this.averageSpeed);
933928
},
934929

935930
/**
@@ -1290,6 +1285,20 @@
12901285
}
12911286
},
12921287

1288+
/**
1289+
* Count total size uploaded
1290+
* @function
1291+
* @returns {number}
1292+
*/
1293+
sizeUploaded: function () {
1294+
var size = this.endByte - this.startByte;
1295+
// can't return only chunk.loaded value, because it is bigger than chunk size
1296+
if (this.status() !== 'success') {
1297+
size = this.progress() * size;
1298+
}
1299+
return size;
1300+
},
1301+
12931302
/**
12941303
* Prepare Xhr request. Set query, headers and data
12951304
* @param {string} method GET or POST
@@ -1399,7 +1408,7 @@
13991408
* @type {FlowChunk}
14001409
*/
14011410
Flow.FlowChunk = FlowChunk;
1402-
1411+
14031412
if (typeof module !== 'undefined') {
14041413
module.exports = Flow;
14051414
} else if (typeof define === "function" && define.amd) {

test/uploadSpec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ describe('upload file', function() {
401401
flow.upload();
402402

403403
clock.tick(1000);
404-
requests[0].progress(5, 10, true);
404+
requests[0].progress(50, 100, true);
405405
expect(fileProgress).toHaveBeenCalled();
406406
expect(fileFirst.currentSpeed).toBe(5);
407407
expect(fileFirst.averageSpeed).toBe(2.5);

0 commit comments

Comments
 (0)