| 120 | } |
| 121 | |
| 122 | #runNextTask() { |
| 123 | if (this.#pending.length === 0) return; |
| 124 | |
| 125 | const tasks = [this.#pending[0]]; |
| 126 | let weight = this.#getWeight(tasks[0]); |
| 127 | const toRemove = new Set([0]); |
| 128 | for (let i = 1; i < this.#pending.length && weight < this.#maxWeight; i++) { |
| 129 | const task = this.#pending[i]; |
| 130 | const taskWeight = this.#getWeight(task); |
| 131 | if (weight + taskWeight <= this.#maxWeight) { |
| 132 | weight += taskWeight; |
| 133 | tasks.push(task); |
| 134 | toRemove.add(i); |
| 135 | } |
| 136 | } |
| 137 | this.#pending = this.#pending.filter((_, i) => !toRemove.has(i)); |
| 138 | |
| 139 | const taskProcess = cp.fork(this.#filename, ["-b", ...tasks, "--pretty"], { |
| 140 | stdio: this.#showOutput(tasks) ? "inherit" : "ignore", |
| 141 | }); |
| 142 | |
| 143 | // eslint-disable-next-line @typescript-eslint/no-misused-promises |
| 144 | taskProcess.once("exit", async code => { |
| 145 | const startTime = this.#running.get(taskProcess)!; |
| 146 | const duration = performance.now() - startTime; |
| 147 | await this.#onTaskDone(tasks, code!, duration); |
| 148 | this.#running.delete(taskProcess); |
| 149 | |
| 150 | // Spin the event loop so that #run can pick up the next task |
| 151 | await setTimeout(0); |
| 152 | |
| 153 | this.#runTasks(); |
| 154 | |
| 155 | if (this.#running.size === 0 && this.#pending.length === 0) { |
| 156 | this.#emptyListeners.forEach(f => f()); |
| 157 | } |
| 158 | }); |
| 159 | |
| 160 | taskProcess.once("error", err => { |
| 161 | console.error("Error in task process:", err); |
| 162 | this.#onTaskError(tasks); |
| 163 | this.#running.delete(taskProcess); |
| 164 | }); |
| 165 | |
| 166 | this.#onTaskStart(tasks, weight); |
| 167 | this.#running.set(taskProcess, performance.now()); |
| 168 | } |
| 169 | |
| 170 | #runTasks() { |
| 171 | while (this.#running.size < this.#concurrency && this.#pending.length > 0) { |