Skip to content

Commit 12757c4

Browse files
committed
refactor(android): split content re-capture loop out of helper capture
1 parent f93d8ea commit 12757c4

1 file changed

Lines changed: 54 additions & 33 deletions

File tree

‎packages/platform-android/src/snapshot.ts‎

Lines changed: 54 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -287,56 +287,77 @@ async function captureAndroidUiHierarchyWithHelper(
287287
artifact: AndroidSnapshotHelperArtifact,
288288
): Promise<{ xml: string; metadata: AndroidSnapshotBackendMetadata }> {
289289
const helperDeviceKey = getAndroidSnapshotHelperSessionDeviceKey(device);
290-
const adbProvider = resolveAndroidAdbProvider(device, options.helperAdb);
291290
const releaseHelperSession = releasesHelperSessionAfterCapture(options, helperDeviceKey);
292-
const recaptureDeadlineMs = Math.min(
293-
Date.now() + HELPER_CONTENT_RECAPTURE_WINDOW_MS,
294-
options.transient?.settleBy ?? Number.POSITIVE_INFINITY,
295-
);
291+
try {
292+
return await captureAndroidHelperContentWithinWindow({
293+
options,
294+
adb,
295+
adbProvider: resolveAndroidAdbProvider(device, options.helperAdb),
296+
artifact,
297+
helperDeviceKey,
298+
});
299+
} finally {
300+
if (releaseHelperSession) {
301+
await stopAndroidSnapshotHelperSession(helperDeviceKey);
302+
}
303+
}
304+
}
305+
306+
/**
307+
* Re-captures unusable content until a capture answers, the attempts run out, or the re-capture
308+
* window has closed by the time the next attempt would start.
309+
*/
310+
async function captureAndroidHelperContentWithinWindow(params: {
311+
options: AndroidSnapshotOptions;
312+
adb: AndroidAdbExecutor;
313+
adbProvider: AndroidAdbProvider;
314+
artifact: AndroidSnapshotHelperArtifact;
315+
helperDeviceKey: string;
316+
}): Promise<{ xml: string; metadata: AndroidSnapshotBackendMetadata }> {
317+
const { options } = params;
318+
const recaptureDeadlineMs = resolveContentRecaptureDeadlineMs(options);
296319
const rejectContentUnavailable = async (
297320
contentRecovery: AndroidHelperContentRecoveryDecision,
298321
attempts: number,
299322
) =>
300323
await rejectAndroidHelperContentUnavailable({
301324
contentRecovery,
302325
attempts,
303-
helperDeviceKey,
304-
artifact,
305-
adb,
326+
helperDeviceKey: params.helperDeviceKey,
327+
artifact: params.artifact,
328+
adb: params.adb,
306329
signal: options.signal,
307330
retireHelper: options.transient === undefined,
308331
});
309-
try {
310-
let previousDecision: AndroidHelperContentRecoveryDecision | undefined;
311-
for (let attempt = 0; ; attempt += 1) {
312-
if (previousDecision) {
313-
await delayBeforeContentRecapture(options.signal);
314-
if (Date.now() >= recaptureDeadlineMs) {
315-
return await rejectContentUnavailable(previousDecision, attempt);
316-
}
317-
}
318-
const settled = await captureAndroidHelperContentAttempt({
319-
options,
320-
adb,
321-
adbProvider,
322-
artifact,
323-
helperDeviceKey,
324-
attempt,
325-
previousContentReason: previousDecision?.reason,
326-
});
327-
if (settled.outcome === 'captured') return settled.capture;
328-
if (attempt + 1 >= HELPER_CONTENT_CAPTURE_ATTEMPTS || Date.now() >= recaptureDeadlineMs) {
329-
return await rejectContentUnavailable(settled.decision, attempt + 1);
332+
let previousDecision: AndroidHelperContentRecoveryDecision | undefined;
333+
for (let attempt = 0; ; attempt += 1) {
334+
if (previousDecision) {
335+
await delayBeforeContentRecapture(options.signal);
336+
if (Date.now() >= recaptureDeadlineMs) {
337+
return await rejectContentUnavailable(previousDecision, attempt);
330338
}
331-
previousDecision = settled.decision;
332339
}
333-
} finally {
334-
if (releaseHelperSession) {
335-
await stopAndroidSnapshotHelperSession(helperDeviceKey);
340+
const settled = await captureAndroidHelperContentAttempt({
341+
...params,
342+
attempt,
343+
previousContentReason: previousDecision?.reason,
344+
});
345+
if (settled.outcome === 'captured') return settled.capture;
346+
if (attempt + 1 >= HELPER_CONTENT_CAPTURE_ATTEMPTS || Date.now() >= recaptureDeadlineMs) {
347+
return await rejectContentUnavailable(settled.decision, attempt + 1);
336348
}
349+
previousDecision = settled.decision;
337350
}
338351
}
339352

353+
/** No re-capture starts after this instant: the end of the window, or a transient read's settle-by. */
354+
function resolveContentRecaptureDeadlineMs(options: AndroidSnapshotOptions): number {
355+
return Math.min(
356+
Date.now() + HELPER_CONTENT_RECAPTURE_WINDOW_MS,
357+
options.transient?.settleBy ?? Number.POSITIVE_INFINITY,
358+
);
359+
}
360+
340361
/** A transient read keeps a session it found running and releases one it had to start. */
341362
function releasesHelperSessionAfterCapture(
342363
options: AndroidSnapshotOptions,

0 commit comments

Comments
 (0)