-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathraceSimulationSystem.ts
More file actions
33 lines (24 loc) · 762 Bytes
/
raceSimulationSystem.ts
File metadata and controls
33 lines (24 loc) · 762 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import { promiseWithResolvers } from "@trigger.dev/core";
export class RaceSimulationSystem {
private racepoints: Record<string, Promise<void> | undefined> = {};
constructor() {}
async waitForRacepoint({ runId }: { runId: string }): Promise<void> {
if (this.racepoints[runId]) {
return this.racepoints[runId];
}
return Promise.resolve();
}
registerRacepointForRun({ runId, waitInterval }: { runId: string; waitInterval: number }) {
if (this.racepoints[runId]) {
return;
}
const { promise, resolve } = promiseWithResolvers<void>();
this.racepoints[runId] = promise;
setTimeout(() => {
resolve();
}, waitInterval);
promise.then(() => {
delete this.racepoints[runId];
});
}
}