@@ -17,6 +17,12 @@ import {
1717 readMaestroSelectorPlatform ,
1818 resolveVisibleMaestroNodeFromSnapshot ,
1919} from './runtime-targets.ts' ;
20+ import { sleep } from '../../utils/timeouts.ts' ;
21+
22+ const MAESTRO_RUN_FLOW_WHEN_POLICY = {
23+ visibleTimeoutMs : 1000 ,
24+ visiblePollMs : 250 ,
25+ } as const ;
2026
2127type MaestroRunFlowWhenCondition =
2228 | { ok : true ; mode : string ; predicate : string ; selector : string }
@@ -92,22 +98,64 @@ async function evaluateMaestroRunFlowWhenCondition(
9298 } ,
9399 condition : Extract < MaestroRunFlowWhenCondition , { ok : true } > ,
94100) : Promise < { ok : true ; matched : boolean } | { ok : false ; response : DaemonResponse } > {
101+ if ( condition . mode === 'visible' ) {
102+ return await waitForMaestroRunFlowVisibleCondition ( params , condition ) ;
103+ }
104+
95105 const response = await captureMaestroRawSnapshot ( params ) ;
96106 if ( ! response . ok ) return { ok : false , response } ;
107+ const visible = readMaestroRunFlowVisibleCondition ( params , condition , response ) ;
108+ if ( ! visible . ok ) {
109+ return {
110+ ok : false ,
111+ response : visible . response ,
112+ } ;
113+ }
114+ return { ok : true , matched : ! visible . matched } ;
115+ }
116+
117+ async function waitForMaestroRunFlowVisibleCondition (
118+ params : {
119+ baseReq : ReplayBaseRequest ;
120+ invoke : ( req : DaemonRequest ) => Promise < DaemonResponse > ;
121+ } ,
122+ condition : Extract < MaestroRunFlowWhenCondition , { ok : true } > ,
123+ ) : Promise < { ok : true ; matched : boolean } | { ok : false ; response : DaemonResponse } > {
124+ const startedAt = Date . now ( ) ;
125+ while ( true ) {
126+ const response = await captureMaestroRawSnapshot ( params ) ;
127+ if ( ! response . ok ) return { ok : false , response } ;
128+ const visible = readMaestroRunFlowVisibleCondition ( params , condition , response ) ;
129+ if ( ! visible . ok ) return { ok : false , response : visible . response } ;
130+ if ( visible . matched ) return { ok : true , matched : true } ;
131+ if ( Date . now ( ) - startedAt >= MAESTRO_RUN_FLOW_WHEN_POLICY . visibleTimeoutMs ) {
132+ return { ok : true , matched : false } ;
133+ }
134+ await sleep ( MAESTRO_RUN_FLOW_WHEN_POLICY . visiblePollMs ) ;
135+ }
136+ }
137+
138+ function readMaestroRunFlowVisibleCondition (
139+ params : {
140+ baseReq : ReplayBaseRequest ;
141+ } ,
142+ condition : Extract < MaestroRunFlowWhenCondition , { ok : true } > ,
143+ response : Extract < DaemonResponse , { ok : true } > ,
144+ ) : { ok : true ; matched : boolean } | { ok : false ; response : DaemonResponse } {
97145 const snapshot = readSnapshotState ( response . data ) ;
98146 if ( ! snapshot ) {
99147 return {
100148 ok : false ,
101149 response : errorResponse ( 'COMMAND_FAILED' , 'Unable to read snapshot data for runFlow.when.' ) ,
102150 } ;
103151 }
104- const visible = resolveVisibleMaestroNodeFromSnapshot (
152+ const matched = resolveVisibleMaestroNodeFromSnapshot (
105153 snapshot ,
106154 condition . selector ,
107155 readMaestroSelectorPlatform ( params . baseReq . flags ) ,
108156 getSnapshotReferenceFrame ( snapshot ) ,
109157 ) . ok ;
110- return { ok : true , matched : condition . mode === 'visible' ? visible : ! visible } ;
158+ return { ok : true , matched } ;
111159}
112160
113161async function invokeMaestroRunFlowWhenSteps (
0 commit comments