Flatten the screenshot capture batch

Build the task list with a foreach over the theme map instead of a
closure-returning closure that captured the same variables twice, and
return failures from the coroutines instead of threading a by-reference
error string that left null slots in the results.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
loks0nandClaude Fable 5 committed 2026-07-10 20:32:27 +01:00
1 parent b10a2bdcb4
commit d99a780494
1 file changed
+14 -15
@@ -156,30 +156,29 @@ class Screenshots extends Action
$framework = Config::getParam('frameworks', [])[$site->getAttribute('framework', '')] ?? null;
$sleep = $framework['screenshotSleep'] ?? 3000;
$themes = ['screenshotLight' => 'light', 'screenshotDark' => 'dark'];
$screenshotError = null;
$captures = batch(\array_map(function ($key) use ($themes, $screenshots, $routerHost, $headers, $sleep, &$screenshotError) {
return function () use ($key, $themes, $screenshots, $routerHost, $headers, $sleep, &$screenshotError) {
$tasks = [];
foreach (['screenshotLight' => 'light', 'screenshotDark' => 'dark'] as $key => $theme) {
// batch() doesn't propagate exceptions out of coroutines; return them instead.
$tasks[] = function () use ($key, $theme, $screenshots, $routerHost, $headers, $sleep) {
try {
$theme = $themes[$key];
$screenshot = $screenshots->create(
return ['key' => $key, 'screenshot' => $screenshots->create(
url: $routerHost . '/?appwrite-preview=1&appwrite-theme=' . $theme,
theme: $theme,
headers: $headers,
sleep: $sleep,
);
return ['key' => $key, 'screenshot' => $screenshot];
)];
} catch (\Throwable $th) {
$screenshotError = $th->getMessage();
return;
return $th;
}
};
}, \array_keys($themes)));
}
if (!\is_null($screenshotError)) {
throw new \Exception($screenshotError);
$captures = [];
foreach (batch($tasks) as $capture) {
if ($capture instanceof \Throwable) {
throw $capture;
}
$captures[] = $capture;
}
Span::add('screenshot.count', \count($captures));