-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
35 lines (29 loc) · 916 Bytes
/
index.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
var events = require('events');
module.exports = function() {
var tasks = {},
ret = {};
ret.emitter = new events.EventEmitter(),
ret.registerTask = function(task, details, weight) {
weight = weight || 1;
tasks[task] = { details: details, weight: weight, percent: 0 };
};
ret.updateTask = function(task, percent) {
tasks[task].percent = percent;
ret.emitter.emit('progress', { task: task, details: tasks[task].details, percent: percent });
// Gives weighted average for overall progress
var totalWeights = 0;
for (var task in tasks) {
totalWeights += tasks[task].weight;
}
ret.emitter.emit('totalProgress',
Object.keys(tasks).reduce(function(acc, task) {
var _task = tasks[task];
return acc + _task.percent * _task.weight / totalWeights;
}, 0)
);
}
ret.getTasks = function() {
return tasks;
}
return ret;
}