|
1 | 1 | 'use strict' |
2 | | -var util = require('util') |
3 | | -var TrackerBase = require('./tracker-base.js') |
4 | | -var Tracker = require('./tracker.js') |
5 | | -var TrackerStream = require('./tracker-stream.js') |
| 2 | +const TrackerBase = require('./tracker-base.js') |
| 3 | +const Tracker = require('./tracker.js') |
| 4 | +const TrackerStream = require('./tracker-stream.js') |
6 | 5 |
|
7 | | -var TrackerGroup = module.exports = function (name) { |
8 | | - TrackerBase.call(this, name) |
9 | | - this.parentGroup = null |
10 | | - this.trackers = [] |
11 | | - this.completion = {} |
12 | | - this.weight = {} |
13 | | - this.totalWeight = 0 |
14 | | - this.finished = false |
15 | | - this.bubbleChange = bubbleChange(this) |
16 | | -} |
17 | | -util.inherits(TrackerGroup, TrackerBase) |
| 6 | +class TrackerGroup extends TrackerBase { |
| 7 | + parentGroup = null |
| 8 | + trackers = [] |
| 9 | + completion = {} |
| 10 | + weight = {} |
| 11 | + totalWeight = 0 |
| 12 | + finished = false |
| 13 | + bubbleChange = bubbleChange(this) |
18 | 14 |
|
19 | | -function bubbleChange (trackerGroup) { |
20 | | - return function (name, completed, tracker) { |
21 | | - trackerGroup.completion[tracker.id] = completed |
22 | | - if (trackerGroup.finished) { |
23 | | - return |
| 15 | + nameInTree () { |
| 16 | + var names = [] |
| 17 | + var from = this |
| 18 | + while (from) { |
| 19 | + names.unshift(from.name) |
| 20 | + from = from.parentGroup |
24 | 21 | } |
25 | | - trackerGroup.emit('change', name || trackerGroup.name, trackerGroup.completed(), trackerGroup) |
| 22 | + return names.join('/') |
26 | 23 | } |
27 | | -} |
28 | 24 |
|
29 | | -TrackerGroup.prototype.nameInTree = function () { |
30 | | - var names = [] |
31 | | - var from = this |
32 | | - while (from) { |
33 | | - names.unshift(from.name) |
34 | | - from = from.parentGroup |
35 | | - } |
36 | | - return names.join('/') |
37 | | -} |
38 | | - |
39 | | -TrackerGroup.prototype.addUnit = function (unit, weight) { |
40 | | - if (unit.addUnit) { |
41 | | - var toTest = this |
42 | | - while (toTest) { |
43 | | - if (unit === toTest) { |
44 | | - throw new Error( |
45 | | - 'Attempted to add tracker group ' + |
46 | | - unit.name + ' to tree that already includes it ' + |
47 | | - this.nameInTree(this)) |
| 25 | + addUnit (unit, weight) { |
| 26 | + if (unit.addUnit) { |
| 27 | + var toTest = this |
| 28 | + while (toTest) { |
| 29 | + if (unit === toTest) { |
| 30 | + throw new Error( |
| 31 | + 'Attempted to add tracker group ' + |
| 32 | + unit.name + ' to tree that already includes it ' + |
| 33 | + this.nameInTree(this)) |
| 34 | + } |
| 35 | + toTest = toTest.parentGroup |
48 | 36 | } |
49 | | - toTest = toTest.parentGroup |
| 37 | + unit.parentGroup = this |
50 | 38 | } |
51 | | - unit.parentGroup = this |
| 39 | + this.weight[unit.id] = weight || 1 |
| 40 | + this.totalWeight += this.weight[unit.id] |
| 41 | + this.trackers.push(unit) |
| 42 | + this.completion[unit.id] = unit.completed() |
| 43 | + unit.on('change', this.bubbleChange) |
| 44 | + if (!this.finished) { |
| 45 | + this.emit('change', unit.name, this.completion[unit.id], unit) |
| 46 | + } |
| 47 | + return unit |
52 | 48 | } |
53 | | - this.weight[unit.id] = weight || 1 |
54 | | - this.totalWeight += this.weight[unit.id] |
55 | | - this.trackers.push(unit) |
56 | | - this.completion[unit.id] = unit.completed() |
57 | | - unit.on('change', this.bubbleChange) |
58 | | - if (!this.finished) { |
59 | | - this.emit('change', unit.name, this.completion[unit.id], unit) |
| 49 | + |
| 50 | + completed () { |
| 51 | + if (this.trackers.length === 0) { |
| 52 | + return 0 |
| 53 | + } |
| 54 | + var valPerWeight = 1 / this.totalWeight |
| 55 | + var completed = 0 |
| 56 | + for (var ii = 0; ii < this.trackers.length; ii++) { |
| 57 | + var trackerId = this.trackers[ii].id |
| 58 | + completed += |
| 59 | + valPerWeight * this.weight[trackerId] * this.completion[trackerId] |
| 60 | + } |
| 61 | + return completed |
60 | 62 | } |
61 | | - return unit |
62 | | -} |
63 | 63 |
|
64 | | -TrackerGroup.prototype.completed = function () { |
65 | | - if (this.trackers.length === 0) { |
66 | | - return 0 |
| 64 | + newGroup (name, weight) { |
| 65 | + return this.addUnit(new TrackerGroup(name), weight) |
67 | 66 | } |
68 | | - var valPerWeight = 1 / this.totalWeight |
69 | | - var completed = 0 |
70 | | - for (var ii = 0; ii < this.trackers.length; ii++) { |
71 | | - var trackerId = this.trackers[ii].id |
72 | | - completed += |
73 | | - valPerWeight * this.weight[trackerId] * this.completion[trackerId] |
| 67 | + |
| 68 | + newItem (name, todo, weight) { |
| 69 | + return this.addUnit(new Tracker(name, todo), weight) |
74 | 70 | } |
75 | | - return completed |
76 | | -} |
77 | 71 |
|
78 | | -TrackerGroup.prototype.newGroup = function (name, weight) { |
79 | | - return this.addUnit(new TrackerGroup(name), weight) |
80 | | -} |
| 72 | + newStream (name, todo, weight) { |
| 73 | + return this.addUnit(new TrackerStream(name, todo), weight) |
| 74 | + } |
81 | 75 |
|
82 | | -TrackerGroup.prototype.newItem = function (name, todo, weight) { |
83 | | - return this.addUnit(new Tracker(name, todo), weight) |
84 | | -} |
| 76 | + finish () { |
| 77 | + this.finished = true |
| 78 | + if (!this.trackers.length) { |
| 79 | + this.addUnit(new Tracker(), 1, true) |
| 80 | + } |
| 81 | + for (var ii = 0; ii < this.trackers.length; ii++) { |
| 82 | + var tracker = this.trackers[ii] |
| 83 | + tracker.finish() |
| 84 | + tracker.removeListener('change', this.bubbleChange) |
| 85 | + } |
| 86 | + this.emit('change', this.name, 1, this) |
| 87 | + } |
85 | 88 |
|
86 | | -TrackerGroup.prototype.newStream = function (name, todo, weight) { |
87 | | - return this.addUnit(new TrackerStream(name, todo), weight) |
88 | | -} |
| 89 | + debug (depth = 0) { |
| 90 | + const indent = ' '.repeat(depth) |
| 91 | + let output = `${indent}${this.name || 'top'}: ${this.completed()}\n` |
89 | 92 |
|
90 | | -TrackerGroup.prototype.finish = function () { |
91 | | - this.finished = true |
92 | | - if (!this.trackers.length) { |
93 | | - this.addUnit(new Tracker(), 1, true) |
94 | | - } |
95 | | - for (var ii = 0; ii < this.trackers.length; ii++) { |
96 | | - var tracker = this.trackers[ii] |
97 | | - tracker.finish() |
98 | | - tracker.removeListener('change', this.bubbleChange) |
| 93 | + this.trackers.forEach(function (tracker) { |
| 94 | + output += tracker instanceof TrackerGroup |
| 95 | + ? tracker.debug(depth + 1) |
| 96 | + : `${indent} ${tracker.name}: ${tracker.completed()}\n` |
| 97 | + }) |
| 98 | + return output |
99 | 99 | } |
100 | | - this.emit('change', this.name, 1, this) |
101 | 100 | } |
102 | 101 |
|
103 | | -var buffer = ' ' |
104 | | -TrackerGroup.prototype.debug = function (depth) { |
105 | | - depth = depth || 0 |
106 | | - var indent = depth ? buffer.slice(0, depth) : '' |
107 | | - var output = indent + (this.name || 'top') + ': ' + this.completed() + '\n' |
108 | | - this.trackers.forEach(function (tracker) { |
109 | | - if (tracker instanceof TrackerGroup) { |
110 | | - output += tracker.debug(depth + 1) |
111 | | - } else { |
112 | | - output += indent + ' ' + tracker.name + ': ' + tracker.completed() + '\n' |
| 102 | +function bubbleChange (trackerGroup) { |
| 103 | + return function (name, completed, tracker) { |
| 104 | + trackerGroup.completion[tracker.id] = completed |
| 105 | + if (trackerGroup.finished) { |
| 106 | + return |
113 | 107 | } |
114 | | - }) |
115 | | - return output |
| 108 | + trackerGroup.emit('change', name || trackerGroup.name, trackerGroup.completed(), trackerGroup) |
| 109 | + } |
116 | 110 | } |
| 111 | + |
| 112 | +module.exports = TrackerGroup |
0 commit comments