mirror of
https://github.com/excalidraw/excalidraw.git
synced 2025-05-03 10:00:07 -04:00
Move queue in common
This commit is contained in:
parent
a9b4b758cf
commit
a3fef45365
3 changed files with 3 additions and 2 deletions
48
packages/common/src/queue.ts
Normal file
48
packages/common/src/queue.ts
Normal file
|
@ -0,0 +1,48 @@
|
|||
import { promiseTry, resolvablePromise } from "@excalidraw/common";
|
||||
|
||||
import type { ResolvablePromise } from "@excalidraw/common";
|
||||
|
||||
import type { MaybePromise } from "@excalidraw/common/utility-types";
|
||||
|
||||
type Job<T, TArgs extends unknown[]> = (...args: TArgs) => MaybePromise<T>;
|
||||
|
||||
type QueueJob<T, TArgs extends unknown[]> = {
|
||||
jobFactory: Job<T, TArgs>;
|
||||
promise: ResolvablePromise<T>;
|
||||
args: TArgs;
|
||||
};
|
||||
|
||||
export class Queue {
|
||||
private jobs: QueueJob<any, any[]>[] = [];
|
||||
private running = false;
|
||||
|
||||
private tick() {
|
||||
if (this.running) {
|
||||
return;
|
||||
}
|
||||
const job = this.jobs.shift();
|
||||
if (job) {
|
||||
this.running = true;
|
||||
job.promise.resolve(
|
||||
promiseTry(job.jobFactory, ...job.args).finally(() => {
|
||||
this.running = false;
|
||||
this.tick();
|
||||
}),
|
||||
);
|
||||
} else {
|
||||
this.running = false;
|
||||
}
|
||||
}
|
||||
|
||||
push<TValue, TArgs extends unknown[]>(
|
||||
jobFactory: Job<TValue, TArgs>,
|
||||
...args: TArgs
|
||||
): Promise<TValue> {
|
||||
const promise = resolvablePromise<TValue>();
|
||||
this.jobs.push({ jobFactory, promise, args });
|
||||
|
||||
this.tick();
|
||||
|
||||
return promise;
|
||||
}
|
||||
}
|
62
packages/common/tests/queue.test.ts
Normal file
62
packages/common/tests/queue.test.ts
Normal file
|
@ -0,0 +1,62 @@
|
|||
import { Queue } from "../src/queue";
|
||||
|
||||
describe("Queue", () => {
|
||||
const calls: any[] = [];
|
||||
|
||||
const createJobFactory =
|
||||
<T>(
|
||||
// for purpose of this test, Error object will become a rejection value
|
||||
resolutionOrRejectionValue: T,
|
||||
ms = 1,
|
||||
) =>
|
||||
() => {
|
||||
return new Promise<T>((resolve, reject) => {
|
||||
setTimeout(() => {
|
||||
if (resolutionOrRejectionValue instanceof Error) {
|
||||
reject(resolutionOrRejectionValue);
|
||||
} else {
|
||||
resolve(resolutionOrRejectionValue);
|
||||
}
|
||||
}, ms);
|
||||
}).then((x) => {
|
||||
calls.push(x);
|
||||
return x;
|
||||
});
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
calls.length = 0;
|
||||
});
|
||||
|
||||
it("should await and resolve values in order of enqueueing", async () => {
|
||||
const queue = new Queue();
|
||||
|
||||
const p1 = queue.push(createJobFactory("A", 50));
|
||||
const p2 = queue.push(createJobFactory("B"));
|
||||
const p3 = queue.push(createJobFactory("C"));
|
||||
|
||||
expect(await p3).toBe("C");
|
||||
expect(await p2).toBe("B");
|
||||
expect(await p1).toBe("A");
|
||||
|
||||
expect(calls).toEqual(["A", "B", "C"]);
|
||||
});
|
||||
|
||||
it("should reject a job if it throws, and not affect other jobs", async () => {
|
||||
const queue = new Queue();
|
||||
|
||||
const err = new Error("B");
|
||||
|
||||
queue.push(createJobFactory("A", 50));
|
||||
const p2 = queue.push(createJobFactory(err));
|
||||
const p3 = queue.push(createJobFactory("C"));
|
||||
|
||||
const p2err = p2.catch((err) => err);
|
||||
|
||||
await p3;
|
||||
|
||||
expect(await p2err).toBe(err);
|
||||
|
||||
expect(calls).toEqual(["A", "C"]);
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue