diff --git a/src/queue.ts b/src/queue.ts index ba73c4b5..78428be0 100644 --- a/src/queue.ts +++ b/src/queue.ts @@ -41,7 +41,8 @@ export class Queue { public success: number = 0; public errored: number = 0; public retried: number = 0; - public tasks: T[] = []; + public total: number = 0; + public tasks: { [index: number]: T } = {}; public waits: Array<{ resolve: () => void; reject: (err: Error) => void }> = []; public min: number = 9999999999; public max: number = 0; @@ -90,7 +91,8 @@ export class Queue { this.startTime = Date.now(); } - this.tasks.push(task); + this.tasks[this.total] = task; + this.total++; this.process(); } @@ -100,11 +102,7 @@ export class Queue { } public process(): void { - if ( - this._finishIfIdle() || - this.active >= this.concurrency || - this.cursor === this.tasks.length - ) { + if (this._finishIfIdle() || this.active >= this.concurrency || this.cursor === this.total) { return; } @@ -132,6 +130,8 @@ export class Queue { this.success++; this.complete++; this.active--; + delete this.tasks[cursorIndex]; + delete this.retryCounts[cursorIndex]; this.process(); } catch (err) { if (this.retries > 0) { @@ -167,18 +167,18 @@ export class Queue { success: this.success, errored: this.errored, retried: this.retried, - total: this.tasks.length, + total: this.total, elapsed: Date.now() - this.startTime, }; } public taskName(cursorIndex: number): string { - const task = this.tasks[cursorIndex]; + const task = this.tasks[cursorIndex] || "finished task"; return typeof task === "string" ? task : `index ${cursorIndex}`; } private _finishIfIdle(): boolean { - if (this.closed && this.cursor === this.tasks.length && this.active === 0) { + if (this.closed && this.cursor === this.total && this.active === 0) { this._finish(null); return true; } diff --git a/src/test/queue.spec.ts b/src/test/queue.spec.ts index e0a1cbfb..463830cf 100644 --- a/src/test/queue.spec.ts +++ b/src/test/queue.spec.ts @@ -31,6 +31,20 @@ describe("Queue", () => { expect(q.taskName(0)).to.equal("index 0"); }); + it("should return 'finished task' as the task name", () => { + const handler = sinon.stub().resolves(); + const q = new Queue({ + handler, + }); + + q.add(2); + q.close(); + + return q.wait().then(() => { + expect(q.taskName(0)).to.equal("finished task"); + }); + }); + it("should handle function tasks", () => { const task = sinon.stub().resolves(); const q = new Queue({}); @@ -44,6 +58,7 @@ describe("Queue", () => { expect(q.success).to.equal(1); expect(q.errored).to.equal(0); expect(q.retried).to.equal(0); + expect(q.total).to.equal(1); }); }); @@ -62,6 +77,7 @@ describe("Queue", () => { expect(q.success).to.equal(1); expect(q.errored).to.equal(0); expect(q.retried).to.equal(0); + expect(q.total).to.equal(1); }); }); @@ -89,6 +105,7 @@ describe("Queue", () => { expect(q.success).to.equal(0); expect(q.errored).to.equal(1); expect(q.retried).to.equal(0); + expect(q.total).to.equal(1); }); }); @@ -117,6 +134,7 @@ describe("Queue", () => { expect(q.success).to.equal(0); expect(q.errored).to.equal(1); expect(q.retried).to.equal(3); + expect(q.total).to.equal(1); }); }); @@ -157,6 +175,7 @@ describe("Queue", () => { expect(q.success).to.equal(3); expect(q.errored).to.equal(0); expect(q.retried).to.equal(6); + expect(q.total).to.equal(3); }); }); @@ -194,6 +213,7 @@ describe("Queue", () => { expect(q.success).to.equal(3); expect(q.errored).to.equal(0); expect(q.retried).to.equal(6); + expect(q.total).to.equal(3); }); }); });