-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
158 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
'use strict' | ||
var EventEmitter = require('events').EventEmitter | ||
var util = require('util') | ||
const EventEmitter = require('events') | ||
|
||
var trackerId = 0 | ||
var TrackerBase = module.exports = function (name) { | ||
EventEmitter.call(this) | ||
this.id = ++trackerId | ||
this.name = name | ||
let trackerId = 0 | ||
class TrackerBase extends EventEmitter { | ||
constructor (name) { | ||
super() | ||
this.id = ++trackerId | ||
this.name = name | ||
} | ||
} | ||
util.inherits(TrackerBase, EventEmitter) | ||
|
||
module.exports = TrackerBase |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,116 +1,112 @@ | ||
'use strict' | ||
var util = require('util') | ||
var TrackerBase = require('./tracker-base.js') | ||
var Tracker = require('./tracker.js') | ||
var TrackerStream = require('./tracker-stream.js') | ||
const TrackerBase = require('./tracker-base.js') | ||
const Tracker = require('./tracker.js') | ||
const TrackerStream = require('./tracker-stream.js') | ||
|
||
var TrackerGroup = module.exports = function (name) { | ||
TrackerBase.call(this, name) | ||
this.parentGroup = null | ||
this.trackers = [] | ||
this.completion = {} | ||
this.weight = {} | ||
this.totalWeight = 0 | ||
this.finished = false | ||
this.bubbleChange = bubbleChange(this) | ||
} | ||
util.inherits(TrackerGroup, TrackerBase) | ||
class TrackerGroup extends TrackerBase { | ||
parentGroup = null | ||
trackers = [] | ||
completion = {} | ||
weight = {} | ||
totalWeight = 0 | ||
finished = false | ||
bubbleChange = bubbleChange(this) | ||
|
||
function bubbleChange (trackerGroup) { | ||
return function (name, completed, tracker) { | ||
trackerGroup.completion[tracker.id] = completed | ||
if (trackerGroup.finished) { | ||
return | ||
nameInTree () { | ||
var names = [] | ||
var from = this | ||
while (from) { | ||
names.unshift(from.name) | ||
from = from.parentGroup | ||
} | ||
trackerGroup.emit('change', name || trackerGroup.name, trackerGroup.completed(), trackerGroup) | ||
return names.join('/') | ||
} | ||
} | ||
|
||
TrackerGroup.prototype.nameInTree = function () { | ||
var names = [] | ||
var from = this | ||
while (from) { | ||
names.unshift(from.name) | ||
from = from.parentGroup | ||
} | ||
return names.join('/') | ||
} | ||
|
||
TrackerGroup.prototype.addUnit = function (unit, weight) { | ||
if (unit.addUnit) { | ||
var toTest = this | ||
while (toTest) { | ||
if (unit === toTest) { | ||
throw new Error( | ||
'Attempted to add tracker group ' + | ||
unit.name + ' to tree that already includes it ' + | ||
this.nameInTree(this)) | ||
addUnit (unit, weight) { | ||
if (unit.addUnit) { | ||
var toTest = this | ||
while (toTest) { | ||
if (unit === toTest) { | ||
throw new Error( | ||
'Attempted to add tracker group ' + | ||
unit.name + ' to tree that already includes it ' + | ||
this.nameInTree(this)) | ||
} | ||
toTest = toTest.parentGroup | ||
} | ||
toTest = toTest.parentGroup | ||
unit.parentGroup = this | ||
} | ||
unit.parentGroup = this | ||
this.weight[unit.id] = weight || 1 | ||
this.totalWeight += this.weight[unit.id] | ||
this.trackers.push(unit) | ||
this.completion[unit.id] = unit.completed() | ||
unit.on('change', this.bubbleChange) | ||
if (!this.finished) { | ||
this.emit('change', unit.name, this.completion[unit.id], unit) | ||
} | ||
return unit | ||
} | ||
this.weight[unit.id] = weight || 1 | ||
this.totalWeight += this.weight[unit.id] | ||
this.trackers.push(unit) | ||
this.completion[unit.id] = unit.completed() | ||
unit.on('change', this.bubbleChange) | ||
if (!this.finished) { | ||
this.emit('change', unit.name, this.completion[unit.id], unit) | ||
|
||
completed () { | ||
if (this.trackers.length === 0) { | ||
return 0 | ||
} | ||
var valPerWeight = 1 / this.totalWeight | ||
var completed = 0 | ||
for (var ii = 0; ii < this.trackers.length; ii++) { | ||
var trackerId = this.trackers[ii].id | ||
completed += | ||
valPerWeight * this.weight[trackerId] * this.completion[trackerId] | ||
} | ||
return completed | ||
} | ||
return unit | ||
} | ||
|
||
TrackerGroup.prototype.completed = function () { | ||
if (this.trackers.length === 0) { | ||
return 0 | ||
newGroup (name, weight) { | ||
return this.addUnit(new TrackerGroup(name), weight) | ||
} | ||
var valPerWeight = 1 / this.totalWeight | ||
var completed = 0 | ||
for (var ii = 0; ii < this.trackers.length; ii++) { | ||
var trackerId = this.trackers[ii].id | ||
completed += | ||
valPerWeight * this.weight[trackerId] * this.completion[trackerId] | ||
|
||
newItem (name, todo, weight) { | ||
return this.addUnit(new Tracker(name, todo), weight) | ||
} | ||
return completed | ||
} | ||
|
||
TrackerGroup.prototype.newGroup = function (name, weight) { | ||
return this.addUnit(new TrackerGroup(name), weight) | ||
} | ||
newStream (name, todo, weight) { | ||
return this.addUnit(new TrackerStream(name, todo), weight) | ||
} | ||
|
||
TrackerGroup.prototype.newItem = function (name, todo, weight) { | ||
return this.addUnit(new Tracker(name, todo), weight) | ||
} | ||
finish () { | ||
this.finished = true | ||
if (!this.trackers.length) { | ||
this.addUnit(new Tracker(), 1, true) | ||
} | ||
for (var ii = 0; ii < this.trackers.length; ii++) { | ||
var tracker = this.trackers[ii] | ||
tracker.finish() | ||
tracker.removeListener('change', this.bubbleChange) | ||
} | ||
this.emit('change', this.name, 1, this) | ||
} | ||
|
||
TrackerGroup.prototype.newStream = function (name, todo, weight) { | ||
return this.addUnit(new TrackerStream(name, todo), weight) | ||
} | ||
debug (depth = 0) { | ||
const indent = ' '.repeat(depth) | ||
let output = `${indent}${this.name || 'top'}: ${this.completed()}\n` | ||
|
||
TrackerGroup.prototype.finish = function () { | ||
this.finished = true | ||
if (!this.trackers.length) { | ||
this.addUnit(new Tracker(), 1, true) | ||
} | ||
for (var ii = 0; ii < this.trackers.length; ii++) { | ||
var tracker = this.trackers[ii] | ||
tracker.finish() | ||
tracker.removeListener('change', this.bubbleChange) | ||
this.trackers.forEach(function (tracker) { | ||
output += tracker instanceof TrackerGroup | ||
? tracker.debug(depth + 1) | ||
: `${indent} ${tracker.name}: ${tracker.completed()}\n` | ||
}) | ||
return output | ||
} | ||
this.emit('change', this.name, 1, this) | ||
} | ||
|
||
var buffer = ' ' | ||
TrackerGroup.prototype.debug = function (depth) { | ||
depth = depth || 0 | ||
var indent = depth ? buffer.slice(0, depth) : '' | ||
var output = indent + (this.name || 'top') + ': ' + this.completed() + '\n' | ||
this.trackers.forEach(function (tracker) { | ||
if (tracker instanceof TrackerGroup) { | ||
output += tracker.debug(depth + 1) | ||
} else { | ||
output += indent + ' ' + tracker.name + ': ' + tracker.completed() + '\n' | ||
function bubbleChange (trackerGroup) { | ||
return function (name, completed, tracker) { | ||
trackerGroup.completion[tracker.id] = completed | ||
if (trackerGroup.finished) { | ||
return | ||
} | ||
}) | ||
return output | ||
trackerGroup.emit('change', name || trackerGroup.name, trackerGroup.completed(), trackerGroup) | ||
} | ||
} | ||
|
||
module.exports = TrackerGroup |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,38 @@ | ||
'use strict' | ||
var util = require('util') | ||
var stream = require('readable-stream') | ||
var delegate = require('delegates') | ||
var Tracker = require('./tracker.js') | ||
const stream = require('readable-stream') | ||
const delegate = require('delegates') | ||
const Tracker = require('./tracker.js') | ||
|
||
var TrackerStream = module.exports = function (name, size, options) { | ||
stream.Transform.call(this, options) | ||
this.tracker = new Tracker(name, size) | ||
this.name = name | ||
this.id = this.tracker.id | ||
this.tracker.on('change', delegateChange(this)) | ||
class TrackerStream extends stream.Transform { | ||
constructor (name, size, options) { | ||
super(options) | ||
this.tracker = new Tracker(name, size) | ||
this.name = name | ||
this.id = this.tracker.id | ||
this.tracker.on('change', delegateChange(this)) | ||
} | ||
|
||
_transform (data, encoding, cb) { | ||
this.tracker.completeWork(data.length ? data.length : 1) | ||
this.push(data) | ||
cb() | ||
} | ||
|
||
_flush (cb) { | ||
this.tracker.finish() | ||
cb() | ||
} | ||
} | ||
util.inherits(TrackerStream, stream.Transform) | ||
|
||
function delegateChange (trackerStream) { | ||
return function (name, completion, tracker) { | ||
trackerStream.emit('change', name, completion, trackerStream) | ||
} | ||
} | ||
|
||
TrackerStream.prototype._transform = function (data, encoding, cb) { | ||
this.tracker.completeWork(data.length ? data.length : 1) | ||
this.push(data) | ||
cb() | ||
} | ||
|
||
TrackerStream.prototype._flush = function (cb) { | ||
this.tracker.finish() | ||
cb() | ||
} | ||
|
||
delegate(TrackerStream.prototype, 'tracker') | ||
.method('completed') | ||
.method('addWork') | ||
.method('finish') | ||
|
||
module.exports = TrackerStream |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,34 @@ | ||
'use strict' | ||
var util = require('util') | ||
var TrackerBase = require('./tracker-base.js') | ||
const TrackerBase = require('./tracker-base.js') | ||
|
||
var Tracker = module.exports = function (name, todo) { | ||
TrackerBase.call(this, name) | ||
this.workDone = 0 | ||
this.workTodo = todo || 0 | ||
} | ||
util.inherits(Tracker, TrackerBase) | ||
class Tracker extends TrackerBase { | ||
constructor (name, todo) { | ||
super(name) | ||
this.workDone = 0 | ||
this.workTodo = todo || 0 | ||
} | ||
|
||
Tracker.prototype.completed = function () { | ||
return this.workTodo === 0 ? 0 : this.workDone / this.workTodo | ||
} | ||
completed () { | ||
return this.workTodo === 0 ? 0 : this.workDone / this.workTodo | ||
} | ||
|
||
Tracker.prototype.addWork = function (work) { | ||
this.workTodo += work | ||
this.emit('change', this.name, this.completed(), this) | ||
} | ||
addWork (work) { | ||
this.workTodo += work | ||
this.emit('change', this.name, this.completed(), this) | ||
} | ||
|
||
Tracker.prototype.completeWork = function (work) { | ||
this.workDone += work | ||
if (this.workDone > this.workTodo) { | ||
this.workDone = this.workTodo | ||
completeWork (work) { | ||
this.workDone += work | ||
if (this.workDone > this.workTodo) { | ||
this.workDone = this.workTodo | ||
} | ||
this.emit('change', this.name, this.completed(), this) | ||
} | ||
this.emit('change', this.name, this.completed(), this) | ||
} | ||
|
||
Tracker.prototype.finish = function () { | ||
this.workTodo = this.workDone = 1 | ||
this.emit('change', this.name, 1, this) | ||
finish () { | ||
this.workTodo = this.workDone = 1 | ||
this.emit('change', this.name, 1, this) | ||
} | ||
} | ||
|
||
module.exports = Tracker |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters