Skip to content

Commit

Permalink
optimisation: don't get the shell prompt before executing initialEval…
Browse files Browse the repository at this point in the history
…uate
  • Loading branch information
lerouxb committed Dec 12, 2024
1 parent 6f20f37 commit 37e3c8b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
6 changes: 2 additions & 4 deletions packages/browser-repl/src/components/shell.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ describe('shell', function () {
return {};
};

const initialEvaluate = 'my command';
const initialEvaluate = ['command 1', 'command 2'];
const onOutputChanged = sinon.spy();
render(
<ShellWrapper
Expand All @@ -537,10 +537,8 @@ describe('shell', function () {
);

await waitFor(() => {
expect(onOutputChanged).to.have.been.called;
expect(screen.getByText('rs0:primary')).to.exist;
});

expect(screen.getByText('rs0:primary')).to.exist;
});
});
});
20 changes: 14 additions & 6 deletions packages/browser-repl/src/components/shell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,11 @@ interface ShellProps {
onEditorChanged?: (editor: EditorRef | null) => void;
}

const normalizeInitialEvaluate = (initialEvaluate: string | string[]) => {
const normalizeInitialEvaluate = (initialEvaluate?: string | string[]) => {
if (!initialEvaluate) {
return [];
}

return (
Array.isArray(initialEvaluate) ? initialEvaluate : [initialEvaluate]
).filter((line) => {
Expand Down Expand Up @@ -455,18 +459,22 @@ const _Shell: ForwardRefRenderFunction<EditorRef | null, ShellProps> = (

setIsFirstRun(false);

void updateShellPrompt().then(async () => {
if (initialEvaluate) {
const evalLines = normalizeInitialEvaluate(initialEvaluate);
const evalLines = normalizeInitialEvaluate(initialEvaluate);
if (evalLines.length) {
void (async () => {
for (const input of evalLines) {
await onInput(input);
}
}
});
})();
} else {
void updateShellPrompt();
}
}, [initialEvaluate, isFirstRun, onInput, updateShellPrompt]);

useEffect(() => {
rafraf(() => {
// Scroll to the bottom every time we render so the input/output will be
// in view.
scrollToBottom();
});
});
Expand Down

0 comments on commit 37e3c8b

Please sign in to comment.