refactor(browser): share one settle-scoped hold helper for paint and debugger leases

This commit is contained in:
Kelvin Amoaba
2026-11-10 19:43:41 +00:00
parent 7896541762
commit d338548a56
4 changed files with 21 additions and 33 deletions
@@ -284,9 +284,7 @@ export async function captureScreenshot(
}
const fallback = await readRemoteCliAdmittedPage(webContents.id, () =>
withTimeout(
Promise.resolve().then(() =>
holdPaintUntilSettled(holdPaint, () => webContents.capturePage())
),
holdPaintUntilSettled(holdPaint, () => webContents.capturePage()),
FALLBACK_CAPTURE_TIMEOUT_MS,
SCREENSHOT_TIMEOUT_MESSAGE
)
@@ -4,6 +4,18 @@ import { acquireElectronDebugger } from './electron-debugger-lease'
/** Draws a hidden guest while a capture or agent input needs its frames; returns its release. */
export type CapturePaintHold = () => () => void
function retainUntilSettled<T>(acquire: () => () => void, run: () => Promise<T>): Promise<T> {
let release: () => void
try {
release = acquire()
} catch (error) {
return Promise.reject(error)
}
const pending = new Promise<T>((resolve) => resolve(run()))
void pending.then(release, release)
return pending
}
/**
* Holds paint until Chromium answers the capture, not just while the caller waits. A capture stays
* pending until the guest draws, and while pending it can hold macOS display sleep off ("Capturing");
@@ -13,16 +25,7 @@ export function holdPaintUntilSettled<T>(
holdPaint: CapturePaintHold,
capture: () => Promise<T>
): Promise<T> {
const release = holdPaint()
let pending: Promise<T>
try {
pending = capture()
} catch (error) {
release()
throw error
}
void pending.then(release, release)
return pending
return retainUntilSettled(holdPaint, capture)
}
/** Also keeps the debugger attached: a detach rejects the command while Chromium keeps the capture. */
@@ -31,19 +34,8 @@ export function holdPaintUntilCdpCaptureSettles<T>(
holdPaint: CapturePaintHold,
capture: () => Promise<T>
): Promise<T> {
let debuggerLease: ReturnType<typeof acquireElectronDebugger>
try {
debuggerLease = acquireElectronDebugger(webContents)
} catch (error) {
return Promise.reject(error)
}
let pending: Promise<T>
try {
pending = holdPaintUntilSettled(holdPaint, capture)
} catch (error) {
debuggerLease.release()
throw error
}
void pending.then(debuggerLease.release, debuggerLease.release)
return pending
return retainUntilSettled(
() => acquireElectronDebugger(webContents).release,
() => retainUntilSettled(holdPaint, capture)
)
}
@@ -4,8 +4,7 @@ import { readBrowserScreencastImageSize } from './browser-screencast-image-size'
import { runDebuggerCommandWithTimeout } from './browser-screencast-debugger-command'
import {
holdPaintUntilCdpCaptureSettles,
holdPaintUntilSettled,
type CapturePaintHold
holdPaintUntilSettled
} from '../debugger/guest-paint-hold'
import type {
BrowserScreencastOptions,
@@ -26,7 +25,6 @@ type BrowserScreencastSnapshotCaptureDeps = {
getSeq: () => number
queueFrame: (frame: PendingScreencastFrame) => void
applyDeviceMetricsOverride: () => Promise<void>
holdPaint: CapturePaintHold
}
export type BrowserScreencastSnapshotCapture = {
@@ -40,7 +38,8 @@ export function createBrowserScreencastSnapshotCapture(
deps: BrowserScreencastSnapshotCaptureDeps
): BrowserScreencastSnapshotCapture {
const { webContents, dbg, options, isClosed, isStopping, getSeq, queueFrame } = deps
const { applyDeviceMetricsOverride, holdPaint } = deps
const { applyDeviceMetricsOverride } = deps
const { holdPaint } = options
let snapshotGeneration = 0
let navigationCaptureTimer: ReturnType<typeof setTimeout> | null = null
@@ -90,7 +90,6 @@ export async function startBrowserScreencast(
isStopping,
getSeq: framePacer.getSeq,
queueFrame: framePacer.queueFrame,
holdPaint: options.holdPaint,
applyDeviceMetricsOverride: () => {
const generation = emulationGeneration
const applied = deviceMetrics.apply()