Skip to content

Commit 37e3c8b

Browse files
committed
optimisation: don't get the shell prompt before executing initialEvaluate
1 parent 6f20f37 commit 37e3c8b

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

packages/browser-repl/src/components/shell.spec.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ describe('shell', function () {
526526
return {};
527527
};
528528

529-
const initialEvaluate = 'my command';
529+
const initialEvaluate = ['command 1', 'command 2'];
530530
const onOutputChanged = sinon.spy();
531531
render(
532532
<ShellWrapper
@@ -537,10 +537,8 @@ describe('shell', function () {
537537
);
538538

539539
await waitFor(() => {
540-
expect(onOutputChanged).to.have.been.called;
540+
expect(screen.getByText('rs0:primary')).to.exist;
541541
});
542-
543-
expect(screen.getByText('rs0:primary')).to.exist;
544542
});
545543
});
546544
});

packages/browser-repl/src/components/shell.tsx

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,11 @@ interface ShellProps {
159159
onEditorChanged?: (editor: EditorRef | null) => void;
160160
}
161161

162-
const normalizeInitialEvaluate = (initialEvaluate: string | string[]) => {
162+
const normalizeInitialEvaluate = (initialEvaluate?: string | string[]) => {
163+
if (!initialEvaluate) {
164+
return [];
165+
}
166+
163167
return (
164168
Array.isArray(initialEvaluate) ? initialEvaluate : [initialEvaluate]
165169
).filter((line) => {
@@ -455,18 +459,22 @@ const _Shell: ForwardRefRenderFunction<EditorRef | null, ShellProps> = (
455459

456460
setIsFirstRun(false);
457461

458-
void updateShellPrompt().then(async () => {
459-
if (initialEvaluate) {
460-
const evalLines = normalizeInitialEvaluate(initialEvaluate);
462+
const evalLines = normalizeInitialEvaluate(initialEvaluate);
463+
if (evalLines.length) {
464+
void (async () => {
461465
for (const input of evalLines) {
462466
await onInput(input);
463467
}
464-
}
465-
});
468+
})();
469+
} else {
470+
void updateShellPrompt();
471+
}
466472
}, [initialEvaluate, isFirstRun, onInput, updateShellPrompt]);
467473

468474
useEffect(() => {
469475
rafraf(() => {
476+
// Scroll to the bottom every time we render so the input/output will be
477+
// in view.
470478
scrollToBottom();
471479
});
472480
});

0 commit comments

Comments
 (0)