Skip to content

Commit

Permalink
fix: clean up idealTree code
Browse files Browse the repository at this point in the history
  • Loading branch information
wraithgar committed Jan 10, 2024
1 parent 3fd5213 commit 6e1f284
Showing 1 changed file with 35 additions and 13 deletions.
48 changes: 35 additions & 13 deletions workspaces/arborist/lib/arborist/build-ideal-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,30 +56,52 @@ const _global = Symbol.for('global')
const _idealTreePrune = Symbol.for('idealTreePrune')

// Push items in, pop them sorted by depth and then path
// Sorts physically shallower deps up to the front of the queue, because
// they'll affect things deeper in, then alphabetical for consistency between
// installs
class DepsQueue {
// [{ sorted, items }] indexed by depth
#deps = []
#sorted = true
#minDepth = 0
#length = 0

get length () {
return this.#deps.length
return this.#length
}

push (item) {
if (!this.#deps.includes(item)) {
this.#sorted = false
this.#deps.push(item)
if (!this.#deps[item.depth]) {
this.#length++
this.#deps[item.depth] = { sorted: true, items: [item] }
// no minDepth check needed, this branch is only reached when we are in
// the middle of a shallower depth and creating a new one
return
}
if (!this.#deps[item.depth].items.includes(item)) {
this.#length++
this.#deps[item.depth].sorted = false
this.#deps[item.depth].items.push(item)
if (item.depth < this.#minDepth) {
this.#minDepth = item.depth
}
}
}

pop () {
if (!this.#sorted) {
// sort physically shallower deps up to the front of the queue, because
// they'll affect things deeper in, then alphabetical
this.#deps.sort((a, b) =>
(a.depth - b.depth) || localeCompare(a.path, b.path))
this.#sorted = true
let depth
while (!depth?.items.length) {
depth = this.#deps[this.#minDepth]
if (!depth?.items.length) {
this.#minDepth++
}
}
if (!depth.sorted) {
depth.items.sort((a, b) => localeCompare(a.path, b.path))
depth.sorted = true
}
return this.#deps.shift()
this.#length--
return depth.items.shift()
}
}

Expand Down Expand Up @@ -1016,7 +1038,7 @@ This is a one-time fix-up, please be patient...
// may well be an optional dep that has gone missing. it'll
// fail later anyway.
for (const e of this.#problemEdges(placed)) {
promises.push(
promises.push(() =>
this.#fetchManifest(npa.resolve(e.name, e.spec, fromPath(placed, e)))
.catch(er => null)
)
Expand All @@ -1031,7 +1053,7 @@ This is a one-time fix-up, please be patient...
}
}

await Promise.all(promises)
await promiseCallLimit(promises)
return this.#buildDepStep()
}

Expand Down

0 comments on commit 6e1f284

Please sign in to comment.