tracker.js 836 B

1234567891011121314151617181920212223242526272829303132
  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) {
  20. this.workDone = this.workTodo
  21. }
  22. this.emit('change', this.name, this.completed(), this)
  23. }
  24. Tracker.prototype.finish = function () {
  25. this.workTodo = this.workDone = 1
  26. this.emit('change', this.name, 1, this)
  27. }