Skip to content

Commit

Permalink
fix: revert DepsQueue to re-sort on pop() (#7499)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukekarrys authored May 10, 2024
1 parent c3d2819 commit e290352
Showing 1 changed file with 8 additions and 30 deletions.
38 changes: 8 additions & 30 deletions workspaces/arborist/lib/arborist/build-ideal-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,48 +53,26 @@ const _addNodeToTrashList = Symbol.for('addNodeToTrashList')
// 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.#length
return this.#deps.length
}

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
}
if (!this.#deps.includes(item)) {
this.#sorted = false
this.#deps.push(item)
}
}

pop () {
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
if (!this.#sorted) {
this.#deps.sort((a, b) => (a.depth - b.depth) || localeCompare(a.path, b.path))
this.#sorted = true
}
this.#length--
return depth.items.shift()
return this.#deps.shift()
}
}

Expand Down

0 comments on commit e290352

Please sign in to comment.