马宇豪
2024-07-16 f591c27b57e2418c9495bc02ae8cfff84d35bc18
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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