-
Notifications
You must be signed in to change notification settings - Fork 0
/
listener.js
44 lines (39 loc) · 948 Bytes
/
listener.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
module.exports = class listener {
constructor(cb) {
this.start = 0;
this.done = 0;
this.total = 0;
this.time_left = 0;
this.onAction = (chunk, isDone) => {
if (isDone) {
return cb({
percentage: 100,
time_left: 0,
file_size: this.total,
uploaded_bytes: this.total,
});
}
let info = this.update(chunk);
cb(info);
};
}
update(addition) {
let step = addition.toString().length;
this.done = this.done + step;
let speed = this.done / (Date.now() - this.start);
let left = this.total - this.done;
this.time_left = left / speed;
return {
percentage: Math.round((this.done / this.total) * 100),
time_left: Math.round(this.time_left),
file_size: this.total,
uploaded_bytes: this.done,
};
}
setStart(milis) {
this.start = milis;
}
setTotal(total) {
this.total = total;
}
};