update
This commit is contained in:
114
package/node_modules/proggy/lib/client.js
generated
vendored
Normal file
114
package/node_modules/proggy/lib/client.js
generated
vendored
Normal file
@@ -0,0 +1,114 @@
|
||||
const EE = require('events')
|
||||
const onProgress = Symbol('onProgress')
|
||||
const bars = Symbol('bars')
|
||||
const listener = Symbol('listener')
|
||||
const normData = Symbol('normData')
|
||||
class Client extends EE {
|
||||
constructor ({ normalize = false, stopOnDone = false } = {}) {
|
||||
super()
|
||||
this.normalize = !!normalize
|
||||
this.stopOnDone = !!stopOnDone
|
||||
this[bars] = new Map()
|
||||
this[listener] = null
|
||||
}
|
||||
|
||||
get size () {
|
||||
return this[bars].size
|
||||
}
|
||||
|
||||
get listening () {
|
||||
return !!this[listener]
|
||||
}
|
||||
|
||||
addListener (...args) {
|
||||
return this.on(...args)
|
||||
}
|
||||
|
||||
on (ev, ...args) {
|
||||
if (ev === 'progress' && !this[listener]) {
|
||||
this.start()
|
||||
}
|
||||
return super.on(ev, ...args)
|
||||
}
|
||||
|
||||
off (ev, ...args) {
|
||||
return this.removeListener(ev, ...args)
|
||||
}
|
||||
|
||||
removeListener (ev, ...args) {
|
||||
const ret = super.removeListener(ev, ...args)
|
||||
if (ev === 'progress' && this.listeners(ev).length === 0) {
|
||||
this.stop()
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
stop () {
|
||||
if (this[listener]) {
|
||||
process.removeListener('progress', this[listener])
|
||||
this[listener] = null
|
||||
}
|
||||
}
|
||||
|
||||
start () {
|
||||
if (!this[listener]) {
|
||||
this[listener] = (...args) => this[onProgress](...args)
|
||||
process.on('progress', this[listener])
|
||||
}
|
||||
}
|
||||
|
||||
[onProgress] (key, data) {
|
||||
data = this[normData](key, data)
|
||||
if (!this[bars].has(key)) {
|
||||
this.emit('bar', key, data)
|
||||
}
|
||||
this[bars].set(key, data)
|
||||
this.emit('progress', key, data)
|
||||
if (data.done) {
|
||||
this[bars].delete(key)
|
||||
this.emit('barDone', key, data)
|
||||
if (this.size === 0) {
|
||||
if (this.stopOnDone) {
|
||||
this.stop()
|
||||
}
|
||||
this.emit('done')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[normData] (key, data) {
|
||||
const actualValue = data.value
|
||||
const actualTotal = data.total
|
||||
let value = actualValue
|
||||
let total = actualTotal
|
||||
const done = data.done || value >= total
|
||||
if (this.normalize) {
|
||||
const bar = this[bars].get(key)
|
||||
total = 100
|
||||
if (done) {
|
||||
value = 100
|
||||
} else {
|
||||
// show value as a portion of 100
|
||||
const pct = 100 * actualValue / actualTotal
|
||||
if (bar) {
|
||||
// don't ever go backwards, and don't stand still
|
||||
// move at least 1% of the remaining value if it wouldn't move.
|
||||
value = (pct > bar.value) ? pct
|
||||
: (100 - bar.value) / 100 + bar.value
|
||||
}
|
||||
}
|
||||
}
|
||||
// include the key
|
||||
return {
|
||||
...data,
|
||||
key,
|
||||
name: data.name || key,
|
||||
value,
|
||||
total,
|
||||
actualValue,
|
||||
actualTotal,
|
||||
done,
|
||||
}
|
||||
}
|
||||
}
|
||||
module.exports = Client
|
||||
15
package/node_modules/proggy/lib/index.js
generated
vendored
Normal file
15
package/node_modules/proggy/lib/index.js
generated
vendored
Normal file
@@ -0,0 +1,15 @@
|
||||
exports.Client = require('./client.js')
|
||||
exports.Tracker = require('./tracker.js')
|
||||
|
||||
const trackers = new Map()
|
||||
exports.createTracker = (name, key, total) => {
|
||||
const tracker = new exports.Tracker(name, key, total)
|
||||
if (trackers.has(tracker.key)) {
|
||||
const msg = `proggy: duplicate progress id ${JSON.stringify(tracker.key)}`
|
||||
throw new Error(msg)
|
||||
}
|
||||
trackers.set(tracker.key, tracker)
|
||||
tracker.on('done', () => trackers.delete(tracker.key))
|
||||
return tracker
|
||||
}
|
||||
exports.createClient = (options = {}) => new exports.Client(options)
|
||||
68
package/node_modules/proggy/lib/tracker.js
generated
vendored
Normal file
68
package/node_modules/proggy/lib/tracker.js
generated
vendored
Normal file
@@ -0,0 +1,68 @@
|
||||
// The tracker class is intentionally as naive as possible. it is just
|
||||
// an ergonomic wrapper around process.emit('progress', ...)
|
||||
const EE = require('events')
|
||||
class Tracker extends EE {
|
||||
constructor (name, key, total) {
|
||||
super()
|
||||
if (!name) {
|
||||
throw new Error('proggy: Tracker needs a name')
|
||||
}
|
||||
|
||||
if (typeof key === 'number' && !total) {
|
||||
total = key
|
||||
key = null
|
||||
}
|
||||
|
||||
if (!total) {
|
||||
total = 100
|
||||
}
|
||||
|
||||
if (!key) {
|
||||
key = name
|
||||
}
|
||||
|
||||
this.done = false
|
||||
this.name = name
|
||||
this.key = key
|
||||
this.value = 0
|
||||
this.total = total
|
||||
}
|
||||
|
||||
finish (metadata = {}) {
|
||||
this.update(this.total, this.total, metadata)
|
||||
}
|
||||
|
||||
update (value, total, metadata) {
|
||||
if (!metadata) {
|
||||
if (total && typeof total === 'object') {
|
||||
metadata = total
|
||||
} else {
|
||||
metadata = {}
|
||||
}
|
||||
}
|
||||
if (typeof total !== 'number') {
|
||||
total = this.total
|
||||
}
|
||||
|
||||
if (this.done) {
|
||||
const msg = `proggy: updating completed tracker: ${JSON.stringify(this.key)}`
|
||||
throw new Error(msg)
|
||||
}
|
||||
this.value = value
|
||||
this.total = total
|
||||
const done = this.value >= this.total
|
||||
process.emit('progress', this.key, {
|
||||
...metadata,
|
||||
name: this.name,
|
||||
key: this.key,
|
||||
value,
|
||||
total,
|
||||
done,
|
||||
})
|
||||
if (done) {
|
||||
this.done = true
|
||||
this.emit('done')
|
||||
}
|
||||
}
|
||||
}
|
||||
module.exports = Tracker
|
||||
Reference in New Issue
Block a user