@@ -27,6 +27,7 @@ import * as Schema from "effect/Schema";
2727import * as Stream from "effect/Stream" ;
2828
2929import { createCdpRelayConnection , type CdpRelayConnection } from "./CdpRelay.ts" ;
30+ import type { PreviewManagerError } from "./Manager.ts" ;
3031
3132const encodeEvent = Schema . encodeSync ( Schema . fromJsonString ( DesktopBrowserEvent ) ) ;
3233const decodeCommand = Schema . decodeUnknownOption ( Schema . fromJsonString ( DesktopBrowserCommand ) ) ;
@@ -42,21 +43,22 @@ export interface DesktopBrowserTabDebugger {
4243 readonly webContents : Electron . WebContents ;
4344 readonly debugger : Electron . Debugger ;
4445 /** Shares the manager's recording/PiP throttling lease while preparing a still frame. */
45- readonly withCaptureActivity : (
46- capture : ( ) => Promise < unknown > ,
47- signal : AbortSignal ,
48- ) => Promise < unknown > ;
46+ readonly withCaptureActivity : < A , E > (
47+ capture : Effect . Effect < A , E > ,
48+ ) => Effect . Effect < A , E | PreviewManagerError > ;
4949}
5050
5151class DesktopBrowserCaptureError extends Schema . TaggedError < DesktopBrowserCaptureError > ( ) (
5252 "DesktopBrowserCaptureError" ,
53- { reason : Schema . String } ,
53+ { reason : Schema . String , cause : Schema . optional ( Schema . Defect ( ) ) } ,
5454) {
5555 override get message ( ) : string {
5656 return `Desktop browser screenshot failed: ${ this . reason } ` ;
5757 }
5858}
5959
60+ const isDesktopBrowserCaptureError = Schema . is ( DesktopBrowserCaptureError ) ;
61+
6062const keyOf = ( { threadId, tabId } : DesktopBrowserTabKey ) => `${ threadId } \u0000${ tabId } ` ;
6163
6264interface AttachedTab {
@@ -140,105 +142,102 @@ export const make = Effect.gen(function* () {
140142 }
141143 } ;
142144 try {
143- return await tab . debuggee . withCaptureActivity (
144- ( ) =>
145- runPromise (
146- Effect . acquireUseRelease (
145+ return await runPromise (
146+ tab . debuggee . withCaptureActivity (
147+ Effect . acquireUseRelease (
148+ Effect . gen ( function * ( ) {
149+ const ready = yield * Deferred . make < void > ( ) ;
150+ const requestId = NodeCrypto . randomUUID ( ) ;
151+ pendingCaptures . set ( requestId , ready ) ;
152+ return { requestId, ready, webContentsId : webContents . id } ;
153+ } ) ,
154+ ( { requestId, ready, webContentsId } ) =>
147155 Effect . gen ( function * ( ) {
148- const ready = yield * Deferred . make < void > ( ) ;
149- const requestId = NodeCrypto . randomUUID ( ) ;
150- pendingCaptures . set ( requestId , ready ) ;
151- return { requestId, ready, webContentsId : webContents . id } ;
152- } ) ,
153- ( { requestId, ready, webContentsId } ) =>
154- Effect . gen ( function * ( ) {
155- yield * PubSub . publish ( captureRequests , {
156- requestId,
157- webContentsId,
158- active : true ,
159- } ) ;
160- yield * Deferred . await ( ready ) . pipe (
161- Effect . timeoutOrElse ( {
162- duration : "2 seconds" ,
163- orElse : ( ) =>
164- Effect . fail (
165- new DesktopBrowserCaptureError ( {
166- reason :
167- "the browser surface did not become paintable within 2 seconds." ,
168- } ) ,
169- ) ,
170- } ) ,
171- ) ;
172- return yield * Effect . tryPromise ( {
173- try : ( signal ) => {
174- pending = ( async ( ) => {
175- requireCurrent ( signal ) ;
176- // CDP may resize the surface for a scaled clip. Request its
177- // screenshot first, then produce an initial frame and, if
178- // CDP is still waiting, a frame after that capture setup.
179- let screenshotSettled = false ;
180- const screenshotRequest = Promise . resolve ( )
181- . then ( ( ) => debuggee . sendCommand ( "Page.captureScreenshot" , parameters ) )
182- . finally ( ( ) => {
183- screenshotSettled = true ;
156+ yield * PubSub . publish ( captureRequests , {
157+ requestId,
158+ webContentsId,
159+ active : true ,
160+ } ) ;
161+ yield * Deferred . await ( ready ) . pipe (
162+ Effect . timeoutOrElse ( {
163+ duration : "2 seconds" ,
164+ orElse : ( ) =>
165+ Effect . fail (
166+ new DesktopBrowserCaptureError ( {
167+ reason : "the browser surface did not become paintable within 2 seconds." ,
168+ } ) ,
169+ ) ,
170+ } ) ,
171+ ) ;
172+ return yield * Effect . tryPromise ( {
173+ try : ( signal ) => {
174+ pending = ( async ( ) => {
175+ requireCurrent ( signal ) ;
176+ // CDP may resize the surface for a scaled clip. Request its
177+ // screenshot first, then produce an initial frame and, if
178+ // CDP is still waiting, a frame after that capture setup.
179+ let screenshotSettled = false ;
180+ const screenshotRequest = Promise . resolve ( )
181+ . then ( ( ) => debuggee . sendCommand ( "Page.captureScreenshot" , parameters ) )
182+ . finally ( ( ) => {
183+ screenshotSettled = true ;
184+ } ) ;
185+ // Neither API is cancellable. Retain the slot until both
186+ // settle, including when one rejects or the caller times out.
187+ const [ screenshot , warmup ] = await Promise . allSettled ( [
188+ screenshotRequest ,
189+ Promise . resolve ( ) . then ( async ( ) => {
190+ await webContents . capturePage ( undefined , {
191+ stayHidden : true ,
192+ stayAwake : false ,
184193 } ) ;
185- // Neither API is cancellable. Retain the slot until both
186- // settle, including when one rejects or the caller times out.
187- const [ screenshot , warmup ] = await Promise . allSettled ( [
188- screenshotRequest ,
189- Promise . resolve ( ) . then ( async ( ) => {
194+ requireCurrent ( signal ) ;
195+ if ( ! screenshotSettled ) {
190196 await webContents . capturePage ( undefined , {
191197 stayHidden : true ,
192198 stayAwake : false ,
193199 } ) ;
194- requireCurrent ( signal ) ;
195- if ( ! screenshotSettled ) {
196- await webContents . capturePage ( undefined , {
197- stayHidden : true ,
198- stayAwake : false ,
199- } ) ;
200- }
201- } ) ,
202- ] ) ;
203- requireCurrent ( signal ) ;
204- if ( screenshot . status === "rejected" ) throw screenshot . reason ;
205- if ( warmup . status === "rejected" ) throw warmup . reason ;
206- return screenshot . value ;
207- } ) ( ) ;
208- return pending ;
209- } ,
210- catch : ( cause ) =>
211- new DesktopBrowserCaptureError ( {
212- reason :
213- cause instanceof Error
214- ? cause . message
215- : "the compositor could not capture a frame." ,
216- } ) ,
217- } ) . pipe (
218- Effect . timeoutOrElse ( {
219- duration : "8 seconds" ,
220- orElse : ( ) =>
221- Effect . fail (
222- new DesktopBrowserCaptureError ( {
223- reason : "the compositor did not supply a screenshot within 8 seconds." ,
224- } ) ,
225- ) ,
226- } ) ,
227- ) ;
228- } ) ,
229- ( { requestId, webContentsId } ) =>
230- Effect . gen ( function * ( ) {
231- pendingCaptures . delete ( requestId ) ;
232- yield * PubSub . publish ( captureRequests , {
233- requestId,
234- webContentsId,
235- active : false ,
236- } ) ;
237- } ) ,
238- ) ,
239- { signal : relaySignal } ,
200+ }
201+ } ) ,
202+ ] ) ;
203+ requireCurrent ( signal ) ;
204+ if ( screenshot . status === "rejected" ) throw screenshot . reason ;
205+ if ( warmup . status === "rejected" ) throw warmup . reason ;
206+ return screenshot . value ;
207+ } ) ( ) ;
208+ return pending ;
209+ } ,
210+ catch : ( cause ) =>
211+ isDesktopBrowserCaptureError ( cause )
212+ ? cause
213+ : new DesktopBrowserCaptureError ( {
214+ reason : "the compositor could not capture a frame." ,
215+ cause,
216+ } ) ,
217+ } ) . pipe (
218+ Effect . timeoutOrElse ( {
219+ duration : "8 seconds" ,
220+ orElse : ( ) =>
221+ Effect . fail (
222+ new DesktopBrowserCaptureError ( {
223+ reason : "the compositor did not supply a screenshot within 8 seconds." ,
224+ } ) ,
225+ ) ,
226+ } ) ,
227+ ) ;
228+ } ) ,
229+ ( { requestId, webContentsId } ) =>
230+ Effect . gen ( function * ( ) {
231+ pendingCaptures . delete ( requestId ) ;
232+ yield * PubSub . publish ( captureRequests , {
233+ requestId,
234+ webContentsId,
235+ active : false ,
236+ } ) ;
237+ } ) ,
240238 ) ,
241- relaySignal ,
239+ ) ,
240+ { signal : relaySignal } ,
242241 ) ;
243242 } finally {
244243 const release = ( ) => {
0 commit comments