mirror of
https://github.com/zhigang1992/firebase-tools.git
synced 2026-01-12 22:47:24 +08:00
Merge branch 'master' into rpb/open-emulators
This commit is contained in:
20
src/queue.ts
20
src/queue.ts
@@ -41,7 +41,8 @@ export class Queue<T> {
|
||||
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<T> {
|
||||
this.startTime = Date.now();
|
||||
}
|
||||
|
||||
this.tasks.push(task);
|
||||
this.tasks[this.total] = task;
|
||||
this.total++;
|
||||
this.process();
|
||||
}
|
||||
|
||||
@@ -100,11 +102,7 @@ export class Queue<T> {
|
||||
}
|
||||
|
||||
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<T> {
|
||||
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<T> {
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user