@@ -10,6 +10,7 @@ import {
1010} from "../schemas/index.js" ;
1111import { ExecutorToWorkerProcessConnection } from "../zodIpc.js" ;
1212import { RuntimeManager } from "./manager.js" ;
13+ import { preventMultipleWaits } from "./preventMultipleWaits.js" ;
1314
1415type Resolver = ( value : CompletedWaitpoint ) => void ;
1516
@@ -19,6 +20,8 @@ export class ManagedRuntimeManager implements RuntimeManager {
1920 // Maps a waitpoint ID to a wait ID
2021 private readonly resolversByWaitpoint : Map < string , string > = new Map ( ) ;
2122
23+ private _preventMultipleWaits = preventMultipleWaits ( ) ;
24+
2225 constructor (
2326 private ipc : ExecutorToWorkerProcessConnection ,
2427 private showLogs : boolean
@@ -36,40 +39,44 @@ export class ManagedRuntimeManager implements RuntimeManager {
3639 }
3740
3841 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+ } ) ;
4246
43- const waitpoint = await promise ;
44- const result = this . waitpointToTaskRunExecutionResult ( waitpoint ) ;
47+ const waitpoint = await promise ;
48+ const result = this . waitpointToTaskRunExecutionResult ( waitpoint ) ;
4549
46- return result ;
50+ return result ;
51+ } ) ;
4752 }
4853
4954 async waitForBatch ( params : {
5055 id : string ;
5156 runCount : number ;
5257 ctx : TaskRunContext ;
5358 } ) : 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 ;
5774
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+ } ) ;
7380 }
7481
7582 async waitForWaitpoint ( {
@@ -79,17 +86,19 @@ export class ManagedRuntimeManager implements RuntimeManager {
7986 waitpointFriendlyId : string ;
8087 finishDate ?: Date ;
8188 } ) : 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+ } ) ;
8593
86- const waitpoint = await promise ;
94+ const waitpoint = await promise ;
8795
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+ } ) ;
93102 }
94103
95104 associateWaitWithWaitpoint ( waitId : string , waitpointId : string ) {
0 commit comments