Created
October 29, 2014 09:40
-
-
Save iarna/23bc36c279331e9d7181 to your computer and use it in GitHub Desktop.
progress tracker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict" | |
var EventEmitter = require("events").EventEmitter | |
var util = require("util") | |
var Tracker = exports.Tracker = function (done,todo) { | |
this.workDone = done || 0 | |
this.workTodo = todo || 0 | |
} | |
util.extend(Tracker, EventEmitter) | |
Tracker.prototype.completed = function () { | |
return this.workTodo==0 ? 0 : this.workDone / this.workTodo | |
} | |
Tracker.prototype.addWork = function (work) { | |
this.workTodo += work | |
this.emit('change') | |
} | |
Tracker.prototype.completeWork = function (work) { | |
this.workDone += work | |
this.emit('change') | |
} | |
var TrackGroup = exports.TrackGroup = function (units) { | |
this.trackGroup = units || [] | |
var self = this | |
var noteChange = this.noteChange = function () { self.emit('change') } | |
this.trackGroup.forEach(function(unit) { | |
unit.on('change', noteChange) | |
}) | |
} | |
util.extend(TrackGroup, EventEmitter) | |
TrackGroup.prototype.completed = function () { | |
if (this.trackGroup.length==0) return 0 | |
var valPerUnit = 1 / this.trackGroup.length | |
var completed = 0 | |
this.trackGroup.forEach(function(T) { | |
completed += valPerUnit * T.completed() | |
}) | |
return completed | |
} | |
TrackGroup.prototype.addUnit = function (unit) { | |
this.units.push(unit) | |
unit.on('change', this.noteChange) | |
return unit | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment