11cb0ef41Sopenharmony_ci'use strict'
21cb0ef41Sopenharmony_ciconst TrackerBase = require('./tracker-base.js')
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ciclass Tracker extends TrackerBase {
51cb0ef41Sopenharmony_ci  constructor (name, todo) {
61cb0ef41Sopenharmony_ci    super(name)
71cb0ef41Sopenharmony_ci    this.workDone = 0
81cb0ef41Sopenharmony_ci    this.workTodo = todo || 0
91cb0ef41Sopenharmony_ci  }
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ci  completed () {
121cb0ef41Sopenharmony_ci    return this.workTodo === 0 ? 0 : this.workDone / this.workTodo
131cb0ef41Sopenharmony_ci  }
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci  addWork (work) {
161cb0ef41Sopenharmony_ci    this.workTodo += work
171cb0ef41Sopenharmony_ci    this.emit('change', this.name, this.completed(), this)
181cb0ef41Sopenharmony_ci  }
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci  completeWork (work) {
211cb0ef41Sopenharmony_ci    this.workDone += work
221cb0ef41Sopenharmony_ci    if (this.workDone > this.workTodo) {
231cb0ef41Sopenharmony_ci      this.workDone = this.workTodo
241cb0ef41Sopenharmony_ci    }
251cb0ef41Sopenharmony_ci    this.emit('change', this.name, this.completed(), this)
261cb0ef41Sopenharmony_ci  }
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci  finish () {
291cb0ef41Sopenharmony_ci    this.workTodo = this.workDone = 1
301cb0ef41Sopenharmony_ci    this.emit('change', this.name, 1, this)
311cb0ef41Sopenharmony_ci  }
321cb0ef41Sopenharmony_ci}
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_cimodule.exports = Tracker
35