tracker.js 826 B

123456789101112131415161718192021222324252627282930
  1. 'use strict'
  2. var util = require('util')
  3. var TrackerBase = require('./tracker-base.js')
  4. var Tracker = module.exports = function (name, todo) {
  5. TrackerBase.call(this, name)
  6. this.workDone = 0
  7. this.workTodo = todo || 0
  8. }
  9. util.inherits(Tracker, TrackerBase)
  10. Tracker.prototype.completed = function () {
  11. return this.workTodo === 0 ? 0 : this.workDone / this.workTodo
  12. }
  13. Tracker.prototype.addWork = function (work) {
  14. this.workTodo += work
  15. this.emit('change', this.name, this.completed(), this)
  16. }
  17. Tracker.prototype.completeWork = function (work) {
  18. this.workDone += work
  19. if (this.workDone > this.workTodo) this.workDone = this.workTodo
  20. this.emit('change', this.name, this.completed(), this)
  21. }
  22. Tracker.prototype.finish = function () {
  23. this.workTodo = this.workDone = 1
  24. this.emit('change', this.name, 1, this)
  25. }