Skip to content

Commit e6d744c

Browse files
committed
Move preventMultipleWaits out of the RuntimeAPI
1 parent 119cedc commit e6d744c

File tree

2 files changed

+27
-31
lines changed

2 files changed

+27
-31
lines changed

packages/core/src/v3/runtime/index.ts

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,12 @@ import { usage } from "../usage-api.js";
1212

1313
const NOOP_RUNTIME_MANAGER = new NoopRuntimeManager();
1414

15-
const concurrentWaitErrorMessage =
16-
"Parallel waits are not supported, e.g. using Promise.all() around our wait functions.";
17-
1815
/**
1916
* All state must be inside the RuntimeManager, do NOT store it on this class.
2017
* This is because of the "dual package hazard", this can be bundled multiple times.
2118
*/
2219
export class RuntimeAPI {
2320
private static _instance?: RuntimeAPI;
24-
private isExecutingWait = false;
2521

2622
private constructor() {}
2723

@@ -34,31 +30,23 @@ export class RuntimeAPI {
3430
}
3531

3632
public waitForDuration(ms: number): Promise<void> {
37-
return this.#preventConcurrentWaits(() =>
38-
usage.pauseAsync(() => this.#getRuntimeManager().waitForDuration(ms))
39-
);
33+
return usage.pauseAsync(() => this.#getRuntimeManager().waitForDuration(ms));
4034
}
4135

4236
public waitUntil(date: Date): Promise<void> {
43-
return this.#preventConcurrentWaits(() =>
44-
usage.pauseAsync(() => this.#getRuntimeManager().waitUntil(date))
45-
);
37+
return usage.pauseAsync(() => this.#getRuntimeManager().waitUntil(date));
4638
}
4739

4840
public waitForTask(params: { id: string; ctx: TaskRunContext }): Promise<TaskRunExecutionResult> {
49-
return this.#preventConcurrentWaits(() =>
50-
usage.pauseAsync(() => this.#getRuntimeManager().waitForTask(params))
51-
);
41+
return usage.pauseAsync(() => this.#getRuntimeManager().waitForTask(params));
5242
}
5343

5444
public waitForBatch(params: {
5545
id: string;
5646
runs: string[];
5747
ctx: TaskRunContext;
5848
}): Promise<BatchTaskRunExecutionResult> {
59-
return this.#preventConcurrentWaits(() =>
60-
usage.pauseAsync(() => this.#getRuntimeManager().waitForBatch(params))
61-
);
49+
return usage.pauseAsync(() => this.#getRuntimeManager().waitForBatch(params));
6250
}
6351

6452
public setGlobalRuntimeManager(runtimeManager: RuntimeManager): boolean {
@@ -73,19 +61,4 @@ export class RuntimeAPI {
7361
#getRuntimeManager(): RuntimeManager {
7462
return getGlobal(API_NAME) ?? NOOP_RUNTIME_MANAGER;
7563
}
76-
77-
async #preventConcurrentWaits<T>(cb: () => Promise<T>): Promise<T> {
78-
if (this.isExecutingWait) {
79-
console.error(concurrentWaitErrorMessage);
80-
throw new Error(concurrentWaitErrorMessage);
81-
}
82-
83-
this.isExecutingWait = true;
84-
85-
try {
86-
return await cb();
87-
} finally {
88-
this.isExecutingWait = false;
89-
}
90-
}
9164
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { ConcurrentWaitError } from "../errors.js";
2+
3+
const concurrentWaitErrorMessage =
4+
"Parallel waits are not supported, e.g. using Promise.all() around our wait functions.";
5+
6+
export function preventMultipleWaits() {
7+
let isExecutingWait = false;
8+
9+
return async <T>(cb: () => Promise<T>): Promise<T> => {
10+
if (isExecutingWait) {
11+
console.error(concurrentWaitErrorMessage);
12+
throw new ConcurrentWaitError(concurrentWaitErrorMessage);
13+
}
14+
15+
isExecutingWait = true;
16+
17+
try {
18+
return await cb();
19+
} finally {
20+
isExecutingWait = false;
21+
}
22+
};
23+
}

0 commit comments

Comments
 (0)