diff --git a/lib/upload-manager.js b/lib/upload-manager.js index 03c0e5f..8cfeaf4 100644 --- a/lib/upload-manager.js +++ b/lib/upload-manager.js @@ -136,6 +136,7 @@ class UploadManager extends EventEmitter { const batchId = `batch-${Date.now()}`; const results = new Map(); // filePath -> { name, size, results: [] } this._batchResults = results; + this._additionalPromises = []; // Track jobs added mid-batch via addJobs() for (const task of tasks) { const fileName = path.basename(task.file); @@ -150,6 +151,11 @@ class UploadManager extends EventEmitter { const promises = tasks.map((task) => this._runJob(task, results, signal)); await Promise.allSettled(promises); + // Wait for any jobs added mid-batch via addJobs() + while (this._additionalPromises.length > 0) { + const batch = this._additionalPromises.splice(0); + await Promise.allSettled(batch); + } this._stopStatsTimer(); this.running = false; @@ -703,9 +709,9 @@ class UploadManager extends EventEmitter { results.set(task.file, { name: fileName, size, results: [] }); } } - // Start each new job — they'll acquire semaphores and run + // Start each new job and track promises so batch-done waits for them for (const task of tasks) { - this._runJob(task, results, signal); + this._additionalPromises.push(this._runJob(task, results, signal)); } }