Skip to content

Commit 5a5ef02

Browse files
committed
fix(visualizer): add await to async screenshot.getData() calls
- Make generateAnimationScripts async and return Promise - Add await to all getData() calls in replay-scripts.ts - Make allScriptsFromDump async with Promise return type - Convert forEach to for...of loop to support await - Use useState + useEffect for async data in blackboard - Add await to allScriptsFromDump calls in usePlaygroundExecution
1 parent b16c97b commit 5a5ef02

File tree

3 files changed

+33
-21
lines changed

3 files changed

+33
-21
lines changed

packages/visualizer/src/component/blackboard/index.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,12 @@ export const Blackboard = (props: {
101101

102102
const context = props.uiContext;
103103
const { size, screenshot } = context;
104-
const screenshotBase64 = screenshot.getData();
104+
const [screenshotBase64, setScreenshotBase64] = useState<string>('');
105+
106+
// Load screenshot data asynchronously
107+
useEffect(() => {
108+
screenshot.getData().then(setScreenshotBase64);
109+
}, [screenshot]);
105110

106111
const screenWidth = size.width;
107112
const screenHeight = size.height;

packages/visualizer/src/hooks/usePlaygroundExecution.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ export function usePlaygroundExecution(
245245
if (result?.dump) {
246246
if (result.dump.tasks && Array.isArray(result.dump.tasks)) {
247247
const groupedDump = wrapExecutionDumpForReplay(result.dump);
248-
const info = allScriptsFromDump(groupedDump);
248+
const info = await allScriptsFromDump(groupedDump);
249249
setReplayCounter((c) => c + 1);
250250
replayInfo = info;
251251
counter = replayCounter + 1;
@@ -382,7 +382,7 @@ export function usePlaygroundExecution(
382382
Array.isArray(executionData.dump.tasks)
383383
) {
384384
const groupedDump = wrapExecutionDumpForReplay(executionData.dump);
385-
replayInfo = allScriptsFromDump(groupedDump);
385+
replayInfo = await allScriptsFromDump(groupedDump);
386386
setReplayCounter((c) => c + 1);
387387
counter = replayCounter + 1;
388388
}

packages/visualizer/src/utils/replay-scripts.ts

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,14 @@ const capitalizeFirstLetter = (str: string) => {
145145
return str.charAt(0).toUpperCase() + str.slice(1);
146146
};
147147

148-
export const allScriptsFromDump = (
148+
export const allScriptsFromDump = async (
149149
dump:
150150
| GroupedActionDump
151151
| IGroupedActionDump
152152
| ExecutionDump
153153
| null
154154
| undefined,
155-
): ReplayScriptsInfo | null => {
155+
): Promise<ReplayScriptsInfo | null> => {
156156
if (!dump) {
157157
console.warn('[allScriptsFromDump] dump is empty');
158158
return {
@@ -205,8 +205,9 @@ export const allScriptsFromDump = (
205205

206206
// Use first dimensions as default for the overall player size
207207
const allScripts: AnimationScript[] = [];
208-
normalizedDump.executions?.filter(Boolean).forEach((execution) => {
209-
const scripts = generateAnimationScripts(
208+
const executions = normalizedDump.executions?.filter(Boolean) || [];
209+
for (const execution of executions) {
210+
const scripts = await generateAnimationScripts(
210211
execution,
211212
-1,
212213
firstWidth!,
@@ -227,7 +228,7 @@ export const allScriptsFromDump = (
227228
}
228229
}
229230
});
230-
});
231+
}
231232

232233
const allScriptsWithoutIntermediateDoneFrame = allScripts.filter(
233234
(script, index) => {
@@ -270,12 +271,12 @@ export const allScriptsFromDump = (
270271
};
271272
};
272273

273-
export const generateAnimationScripts = (
274+
export const generateAnimationScripts = async (
274275
execution: ExecutionDump | IExecutionDump | null,
275276
task: ExecutionTask | number,
276277
imageWidth: number,
277278
imageHeight: number,
278-
): AnimationScript[] | null => {
279+
): Promise<AnimationScript[] | null> => {
279280
if (!execution || !execution.tasks.length) return null;
280281
if (imageWidth === 0 || imageHeight === 0) {
281282
return null;
@@ -342,8 +343,9 @@ export const generateAnimationScripts = (
342343
const taskCount = tasksIncluded.length;
343344
let initSubTitle = '';
344345
let errorStateFlag = false;
345-
tasksIncluded.forEach((task, index) => {
346-
if (errorStateFlag) return;
346+
for (let index = 0; index < tasksIncluded.length; index++) {
347+
const task = tasksIncluded[index];
348+
if (errorStateFlag) continue;
347349

348350
if (index === 0) {
349351
initSubTitle = paramStr(task);
@@ -395,7 +397,7 @@ export const generateAnimationScripts = (
395397
const screenshotData =
396398
typeof context.screenshot === 'string'
397399
? context.screenshot
398-
: context.screenshot.getData();
400+
: await context.screenshot.getData();
399401
scripts.push({
400402
type: 'img',
401403
img: screenshotData,
@@ -450,8 +452,8 @@ export const generateAnimationScripts = (
450452

451453
const planningTask = task as ExecutionTaskPlanning;
452454
if (planningTask.recorder && planningTask.recorder.length > 0) {
453-
const screenshotData =
454-
planningTask.recorder[0]?.screenshot?.getData() || '';
455+
const screenshot = planningTask.recorder[0]?.screenshot;
456+
const screenshotData = screenshot ? await screenshot.getData() : '';
455457
scripts.push({
456458
type: 'img',
457459
img: screenshotData,
@@ -488,8 +490,10 @@ export const generateAnimationScripts = (
488490

489491
currentCameraState = insightCameraState ?? fullPageCameraState;
490492
// const ifLastTask = index === taskCount - 1;
491-
const actionScreenshotData =
492-
task.recorder?.[0]?.screenshot?.getData() || '';
493+
const actionScreenshot = task.recorder?.[0]?.screenshot;
494+
const actionScreenshotData = actionScreenshot
495+
? await actionScreenshot.getData()
496+
: '';
493497
scripts.push({
494498
type: 'img',
495499
img: actionScreenshotData,
@@ -508,7 +512,7 @@ export const generateAnimationScripts = (
508512

509513
if (screenshot) {
510514
const screenshotData =
511-
typeof screenshot === 'string' ? screenshot : screenshot.getData();
515+
typeof screenshot === 'string' ? screenshot : await screenshot.getData();
512516
scripts.push({
513517
type: 'img',
514518
img: screenshotData,
@@ -529,10 +533,13 @@ export const generateAnimationScripts = (
529533
errorMsg.indexOf('NOT_IMPLEMENTED_AS_DESIGNED') > 0
530534
? 'Further actions cannot be performed in the current environment'
531535
: errorMsg;
532-
const errorScreenshotData =
536+
const errorScreenshot =
533537
task.recorder && task.recorder.length > 0
534-
? task.recorder[task.recorder.length - 1].screenshot?.getData() || ''
535-
: '';
538+
? task.recorder[task.recorder.length - 1].screenshot
539+
: undefined;
540+
const errorScreenshotData = errorScreenshot
541+
? await errorScreenshot.getData()
542+
: '';
536543
scripts.push({
537544
type: 'img',
538545
img: errorScreenshotData,

0 commit comments

Comments
 (0)