Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
tracker.js87 linesDownload Raw Back to lib
1const proggy = require('proggy')
2
3module.exports = cls => class Tracker extends cls {
4  #progress = new Map()
5
6  #createTracker (key, name) {
7    const tracker = new proggy.Tracker(name ?? key)
8    tracker.on('done', () => this.#progress.delete(key))
9    this.#progress.set(key, tracker)
10  }
11
12  addTracker (section, subsection = null, key = null) {
13    if (section === null || section === undefined) {
14      this.#onError(`Tracker can't be null or undefined`)
15    }
16
17    if (key === null) {
18      key = subsection
19    }
20
21    const hasTracker = this.#progress.has(section)
22    const hasSubtracker = this.#progress.has(`${section}:${key}`)
23
24    if (hasTracker && subsection === null) {
25      // 0. existing tracker, no subsection
26      this.#onError(`Tracker "${section}" already exists`)
27    } else if (!hasTracker && subsection === null) {
28      // 1. no existing tracker, no subsection
29      // Create a new progress tracker
30      this.#createTracker(section)
31    } else if (!hasTracker && subsection !== null) {
32      // 2. no parent tracker and subsection
33      this.#onError(`Parent tracker "${section}" does not exist`)
34    } else if (!hasTracker || !hasSubtracker) {
35      // 3. existing parent tracker, no subsection tracker
36      // Create a new subtracker and update parents
37      const parentTracker = this.#progress.get(section)
38      parentTracker.update(parentTracker.value, parentTracker.total + 1)
39      this.#createTracker(`${section}:${key}`, `${section}:${subsection}`)
40    }
41    // 4. existing parent tracker, existing subsection tracker
42    // skip it
43  }
44
45  finishTracker (section, subsection = null, key = null) {
46    if (section === null || section === undefined) {
47      this.#onError(`Tracker can't be null or undefined`)
48    }
49
50    if (key === null) {
51      key = subsection
52    }
53
54    const hasTracker = this.#progress.has(section)
55    const hasSubtracker = this.#progress.has(`${section}:${key}`)
56
57    // 0. parent tracker exists, no subsection
58    // Finish parent tracker and remove from this.#progress
59    if (hasTracker && subsection === null) {
60      // check if parent tracker does
61      // not have any remaining children
62      const keys = this.#progress.keys()
63      for (const key of keys) {
64        if (key.match(new RegExp(section + ':'))) {
65          this.finishTracker(section, key)
66        }
67      }
68      // remove parent tracker
69      this.#progress.get(section).finish()
70    } else if (!hasTracker && subsection === null) {
71      // 1. no existing parent tracker, no subsection
72      this.#onError(`Tracker "${section}" does not exist`)
73    } else if (!hasTracker || hasSubtracker) {
74      // 2. subtracker exists
75      // Finish subtracker and remove from this.#progress
76      const parentTracker = this.#progress.get(section)
77      parentTracker.update(parentTracker.value + 1)
78      this.#progress.get(`${section}:${key}`).finish()
79    }
80    // 3. existing parent tracker, no subsection
81  }
82
83  #onError (msg) {
84    throw new Error(msg)
85  }
86}
87 
codekingpro/portable-devtools · Team Ai