@@ -10,6 +10,7 @@ import {
10
10
} from "../schemas/index.js" ;
11
11
import { ExecutorToWorkerProcessConnection } from "../zodIpc.js" ;
12
12
import { RuntimeManager } from "./manager.js" ;
13
+ import { preventMultipleWaits } from "./preventMultipleWaits.js" ;
13
14
14
15
type Resolver = ( value : CompletedWaitpoint ) => void ;
15
16
@@ -19,6 +20,8 @@ export class ManagedRuntimeManager implements RuntimeManager {
19
20
// Maps a waitpoint ID to a wait ID
20
21
private readonly resolversByWaitpoint : Map < string , string > = new Map ( ) ;
21
22
23
+ private _preventMultipleWaits = preventMultipleWaits ( ) ;
24
+
22
25
constructor (
23
26
private ipc : ExecutorToWorkerProcessConnection ,
24
27
private showLogs : boolean
@@ -36,40 +39,44 @@ export class ManagedRuntimeManager implements RuntimeManager {
36
39
}
37
40
38
41
async waitForTask ( params : { id : string ; ctx : TaskRunContext } ) : Promise < TaskRunExecutionResult > {
39
- const promise = new Promise < CompletedWaitpoint > ( ( resolve ) => {
40
- this . resolversByWaitId . set ( params . id , resolve ) ;
41
- } ) ;
42
+ return this . _preventMultipleWaits ( async ( ) => {
43
+ const promise = new Promise < CompletedWaitpoint > ( ( resolve ) => {
44
+ this . resolversByWaitId . set ( params . id , resolve ) ;
45
+ } ) ;
42
46
43
- const waitpoint = await promise ;
44
- const result = this . waitpointToTaskRunExecutionResult ( waitpoint ) ;
47
+ const waitpoint = await promise ;
48
+ const result = this . waitpointToTaskRunExecutionResult ( waitpoint ) ;
45
49
46
- return result ;
50
+ return result ;
51
+ } ) ;
47
52
}
48
53
49
54
async waitForBatch ( params : {
50
55
id : string ;
51
56
runCount : number ;
52
57
ctx : TaskRunContext ;
53
58
} ) : Promise < BatchTaskRunExecutionResult > {
54
- if ( ! params . runCount ) {
55
- return Promise . resolve ( { id : params . id , items : [ ] } ) ;
56
- }
59
+ return this . _preventMultipleWaits ( async ( ) => {
60
+ if ( ! params . runCount ) {
61
+ return Promise . resolve ( { id : params . id , items : [ ] } ) ;
62
+ }
63
+
64
+ const promise = Promise . all (
65
+ Array . from ( { length : params . runCount } , ( _ , index ) => {
66
+ const resolverId = `${ params . id } _${ index } ` ;
67
+ return new Promise < CompletedWaitpoint > ( ( resolve , reject ) => {
68
+ this . resolversByWaitId . set ( resolverId , resolve ) ;
69
+ } ) ;
70
+ } )
71
+ ) ;
72
+
73
+ const waitpoints = await promise ;
57
74
58
- const promise = Promise . all (
59
- Array . from ( { length : params . runCount } , ( _ , index ) => {
60
- const resolverId = `${ params . id } _${ index } ` ;
61
- return new Promise < CompletedWaitpoint > ( ( resolve , reject ) => {
62
- this . resolversByWaitId . set ( resolverId , resolve ) ;
63
- } ) ;
64
- } )
65
- ) ;
66
-
67
- const waitpoints = await promise ;
68
-
69
- return {
70
- id : params . id ,
71
- items : waitpoints . map ( this . waitpointToTaskRunExecutionResult ) ,
72
- } ;
75
+ return {
76
+ id : params . id ,
77
+ items : waitpoints . map ( this . waitpointToTaskRunExecutionResult ) ,
78
+ } ;
79
+ } ) ;
73
80
}
74
81
75
82
async waitForWaitpoint ( {
@@ -79,17 +86,19 @@ export class ManagedRuntimeManager implements RuntimeManager {
79
86
waitpointFriendlyId : string ;
80
87
finishDate ?: Date ;
81
88
} ) : Promise < WaitpointTokenResult > {
82
- const promise = new Promise < CompletedWaitpoint > ( ( resolve ) => {
83
- this . resolversByWaitId . set ( waitpointFriendlyId , resolve ) ;
84
- } ) ;
89
+ return this . _preventMultipleWaits ( async ( ) => {
90
+ const promise = new Promise < CompletedWaitpoint > ( ( resolve ) => {
91
+ this . resolversByWaitId . set ( waitpointFriendlyId , resolve ) ;
92
+ } ) ;
85
93
86
- const waitpoint = await promise ;
94
+ const waitpoint = await promise ;
87
95
88
- return {
89
- ok : ! waitpoint . outputIsError ,
90
- output : waitpoint . output ,
91
- outputType : waitpoint . outputType ,
92
- } ;
96
+ return {
97
+ ok : ! waitpoint . outputIsError ,
98
+ output : waitpoint . output ,
99
+ outputType : waitpoint . outputType ,
100
+ } ;
101
+ } ) ;
93
102
}
94
103
95
104
associateWaitWithWaitpoint ( waitId : string , waitpointId : string ) {
0 commit comments