Skip to content

Commit e0163c6

Browse files
committed
1 parent fca804a commit e0163c6

File tree

6 files changed

+158
-155
lines changed

6 files changed

+158
-155
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
'use strict'
2-
var EventEmitter = require('events').EventEmitter
3-
var util = require('util')
2+
const EventEmitter = require('events')
43

5-
var trackerId = 0
6-
var TrackerBase = module.exports = function (name) {
7-
EventEmitter.call(this)
8-
this.id = ++trackerId
9-
this.name = name
4+
let trackerId = 0
5+
class TrackerBase extends EventEmitter {
6+
constructor (name) {
7+
super()
8+
this.id = ++trackerId
9+
this.name = name
10+
}
1011
}
11-
util.inherits(TrackerBase, EventEmitter)
12+
13+
module.exports = TrackerBase
Original file line numberDiff line numberDiff line change
@@ -1,116 +1,112 @@
11
'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')
65

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)
1814

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
2421
}
25-
trackerGroup.emit('change', name || trackerGroup.name, trackerGroup.completed(), trackerGroup)
22+
return names.join('/')
2623
}
27-
}
2824

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
4836
}
49-
toTest = toTest.parentGroup
37+
unit.parentGroup = this
5038
}
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
5248
}
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
6062
}
61-
return unit
62-
}
6363

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)
6766
}
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)
7470
}
75-
return completed
76-
}
7771

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+
}
8175

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+
}
8588

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`
8992

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
9999
}
100-
this.emit('change', this.name, 1, this)
101100
}
102101

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
113107
}
114-
})
115-
return output
108+
trackerGroup.emit('change', name || trackerGroup.name, trackerGroup.completed(), trackerGroup)
109+
}
116110
}
111+
112+
module.exports = TrackerGroup
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,38 @@
11
'use strict'
2-
var util = require('util')
3-
var stream = require('readable-stream')
4-
var delegate = require('delegates')
5-
var Tracker = require('./tracker.js')
2+
const stream = require('readable-stream')
3+
const delegate = require('delegates')
4+
const Tracker = require('./tracker.js')
65

7-
var TrackerStream = module.exports = function (name, size, options) {
8-
stream.Transform.call(this, options)
9-
this.tracker = new Tracker(name, size)
10-
this.name = name
11-
this.id = this.tracker.id
12-
this.tracker.on('change', delegateChange(this))
6+
class TrackerStream extends stream.Transform {
7+
constructor (name, size, options) {
8+
super(options)
9+
this.tracker = new Tracker(name, size)
10+
this.name = name
11+
this.id = this.tracker.id
12+
this.tracker.on('change', delegateChange(this))
13+
}
14+
15+
_transform (data, encoding, cb) {
16+
this.tracker.completeWork(data.length ? data.length : 1)
17+
this.push(data)
18+
cb()
19+
}
20+
21+
_flush (cb) {
22+
this.tracker.finish()
23+
cb()
24+
}
1325
}
14-
util.inherits(TrackerStream, stream.Transform)
1526

1627
function delegateChange (trackerStream) {
1728
return function (name, completion, tracker) {
1829
trackerStream.emit('change', name, completion, trackerStream)
1930
}
2031
}
2132

22-
TrackerStream.prototype._transform = function (data, encoding, cb) {
23-
this.tracker.completeWork(data.length ? data.length : 1)
24-
this.push(data)
25-
cb()
26-
}
27-
28-
TrackerStream.prototype._flush = function (cb) {
29-
this.tracker.finish()
30-
cb()
31-
}
32-
3333
delegate(TrackerStream.prototype, 'tracker')
3434
.method('completed')
3535
.method('addWork')
3636
.method('finish')
37+
38+
module.exports = TrackerStream
+26-24
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,34 @@
11
'use strict'
2-
var util = require('util')
3-
var TrackerBase = require('./tracker-base.js')
2+
const TrackerBase = require('./tracker-base.js')
43

5-
var Tracker = module.exports = function (name, todo) {
6-
TrackerBase.call(this, name)
7-
this.workDone = 0
8-
this.workTodo = todo || 0
9-
}
10-
util.inherits(Tracker, TrackerBase)
4+
class Tracker extends TrackerBase {
5+
constructor (name, todo) {
6+
super(name)
7+
this.workDone = 0
8+
this.workTodo = todo || 0
9+
}
1110

12-
Tracker.prototype.completed = function () {
13-
return this.workTodo === 0 ? 0 : this.workDone / this.workTodo
14-
}
11+
completed () {
12+
return this.workTodo === 0 ? 0 : this.workDone / this.workTodo
13+
}
1514

16-
Tracker.prototype.addWork = function (work) {
17-
this.workTodo += work
18-
this.emit('change', this.name, this.completed(), this)
19-
}
15+
addWork (work) {
16+
this.workTodo += work
17+
this.emit('change', this.name, this.completed(), this)
18+
}
2019

21-
Tracker.prototype.completeWork = function (work) {
22-
this.workDone += work
23-
if (this.workDone > this.workTodo) {
24-
this.workDone = this.workTodo
20+
completeWork (work) {
21+
this.workDone += work
22+
if (this.workDone > this.workTodo) {
23+
this.workDone = this.workTodo
24+
}
25+
this.emit('change', this.name, this.completed(), this)
2526
}
26-
this.emit('change', this.name, this.completed(), this)
27-
}
2827

29-
Tracker.prototype.finish = function () {
30-
this.workTodo = this.workDone = 1
31-
this.emit('change', this.name, 1, this)
28+
finish () {
29+
this.workTodo = this.workDone = 1
30+
this.emit('change', this.name, 1, this)
31+
}
3232
}
33+
34+
module.exports = Tracker

node_modules/are-we-there-yet/package.json

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "are-we-there-yet",
3-
"version": "4.0.0",
3+
"version": "4.0.1",
44
"description": "Keep track of the overall completion of many disparate processes",
55
"main": "lib/index.js",
66
"scripts": {
@@ -24,8 +24,8 @@
2424
},
2525
"homepage": "https://github.com/npm/are-we-there-yet",
2626
"devDependencies": {
27-
"@npmcli/eslint-config": "^3.0.1",
28-
"@npmcli/template-oss": "4.5.1",
27+
"@npmcli/eslint-config": "^4.0.0",
28+
"@npmcli/template-oss": "4.17.0",
2929
"tap": "^16.0.1"
3030
},
3131
"dependencies": {
@@ -51,6 +51,7 @@
5151
},
5252
"templateOSS": {
5353
"//@npmcli/template-oss": "This file is partially managed by @npmcli/template-oss. Edits may be overwritten.",
54-
"version": "4.5.1"
54+
"version": "4.17.0",
55+
"publish": true
5556
}
5657
}

package-lock.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -3765,9 +3765,9 @@
37653765
"inBundle": true
37663766
},
37673767
"node_modules/are-we-there-yet": {
3768-
"version": "4.0.0",
3769-
"resolved": "https://registry.npmjs.org/are-we-there-yet/-/are-we-there-yet-4.0.0.tgz",
3770-
"integrity": "sha512-nSXlV+u3vtVjRgihdTzbfWYzxPWGo424zPgQbHD0ZqIla3jqYAewDcvee0Ua2hjS5IfTAmjGlx1Jf0PKwjZDEw==",
3768+
"version": "4.0.1",
3769+
"resolved": "https://registry.npmjs.org/are-we-there-yet/-/are-we-there-yet-4.0.1.tgz",
3770+
"integrity": "sha512-2zuA+jpOYBRgoBCfa+fB87Rk0oGJjDX6pxGzqH6f33NzUhG25Xur6R0u0Z9VVAq8Z5JvQpQI6j6rtonuivC8QA==",
37713771
"inBundle": true,
37723772
"dependencies": {
37733773
"delegates": "^1.0.0",

0 commit comments

Comments
 (0)