Skip to content

Commit 6f20f37

Browse files
committed
don't re-run initialEvaluate AND don't lose the output
1 parent add38a0 commit 6f20f37

File tree

2 files changed

+27
-21
lines changed

2 files changed

+27
-21
lines changed

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,9 @@ describe('shell', function () {
121121
expect(filterEvaluateCalls(fakeRuntime.evaluate.args)).to.have.length(1);
122122

123123
// scrolls to the bottom initially and every time it outputs
124-
expect(Element.prototype.scrollIntoView).to.have.been.calledTwice;
124+
await waitFor(() => {
125+
expect(Element.prototype.scrollIntoView).to.have.been.calledTwice;
126+
});
125127

126128
// make sure we scroll to the bottom every time output changes
127129
rerender(
@@ -478,10 +480,8 @@ describe('shell', function () {
478480
render(<ShellWrapper runtime={fakeRuntime} />);
479481

480482
await waitFor(() => {
481-
expect(Element.prototype.scrollIntoView).to.have.been.calledTwice;
483+
expect(screen.getByLabelText('Chevron Right Icon')).to.exist;
482484
});
483-
484-
expect(screen.getByLabelText('Chevron Right Icon')).to.exist;
485485
});
486486

487487
it('initializes with the value of getShellPrompt', async function () {
@@ -492,10 +492,8 @@ describe('shell', function () {
492492
render(<ShellWrapper runtime={fakeRuntime} />);
493493

494494
await waitFor(() => {
495-
expect(Element.prototype.scrollIntoView).to.have.been.calledTwice;
495+
expect(screen.getByText('$custom$')).to.exist;
496496
});
497-
498-
expect(screen.getByText('$custom$')).to.exist;
499497
});
500498

501499
it('works with a custom user-provided prompt', async function () {
@@ -509,10 +507,8 @@ describe('shell', function () {
509507
render(<ShellWrapper runtime={fakeRuntime} />);
510508

511509
await waitFor(() => {
512-
expect(Element.prototype.scrollIntoView).to.have.been.calledTwice;
510+
expect(screen.getByText('abc')).to.exist;
513511
});
514-
515-
expect(screen.getByText('abc')).to.exist;
516512
});
517513

518514
it('updates after evaluation', async function () {

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

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
fontFamilies,
1515
useDarkMode,
1616
cx,
17+
rafraf,
1718
} from '@mongodb-js/compass-components';
1819
import type {
1920
Runtime,
@@ -202,10 +203,7 @@ const _Shell: ForwardRefRenderFunction<EditorRef | null, ShellProps> = (
202203
const darkMode = useDarkMode();
203204

204205
const editorRef = useRef<EditorRef | null>(null);
205-
const outputRef = useRef(output);
206-
const historyRef = useRef(history);
207206
const shellInputContainerRef = useRef<HTMLDivElement>(null);
208-
const initialEvaluateRef = useRef(initialEvaluate);
209207

210208
useImperativeHandle(
211209
ref,
@@ -254,7 +252,7 @@ const _Shell: ForwardRefRenderFunction<EditorRef | null, ShellProps> = (
254252
return {
255253
onPrint: (result: RuntimeEvaluationResult[]): void => {
256254
const newOutput = [
257-
...(outputRef.current ?? []),
255+
...(output ?? []),
258256
...result.map(
259257
(entry): ShellOutputEntry => ({
260258
format: 'output',
@@ -301,7 +299,7 @@ const _Shell: ForwardRefRenderFunction<EditorRef | null, ShellProps> = (
301299
onOutputChanged?.([]);
302300
},
303301
};
304-
}, [focusEditor, maxOutputLength, onOutputChanged]);
302+
}, [focusEditor, maxOutputLength, onOutputChanged, output]);
305303

306304
const updateShellPrompt = useCallback(async (): Promise<void> => {
307305
let newShellPrompt = '>';
@@ -368,8 +366,8 @@ const _Shell: ForwardRefRenderFunction<EditorRef | null, ShellProps> = (
368366

369367
const onInput = useCallback(
370368
async (code: string) => {
371-
const newOutput = [...(outputRef.current ?? [])];
372-
const newHistory = [...(historyRef.current ?? [])];
369+
const newOutput = [...(output ?? [])];
370+
const newHistory = [...(history ?? [])];
373371

374372
// don't evaluate empty input, but do add it to the output
375373
if (!code || code.trim() === '') {
@@ -407,6 +405,8 @@ const _Shell: ForwardRefRenderFunction<EditorRef | null, ShellProps> = (
407405
onHistoryChanged?.(newHistory);
408406
},
409407
[
408+
output,
409+
history,
410410
onOutputChanged,
411411
evaluate,
412412
redactInfo,
@@ -446,19 +446,29 @@ const _Shell: ForwardRefRenderFunction<EditorRef | null, ShellProps> = (
446446
return Promise.resolve(false);
447447
}, [isOperationInProgress, runtime]);
448448

449+
const [isFirstRun, setIsFirstRun] = useState(true);
450+
449451
useEffect(() => {
452+
if (!isFirstRun) {
453+
return;
454+
}
455+
456+
setIsFirstRun(false);
457+
450458
void updateShellPrompt().then(async () => {
451-
if (initialEvaluateRef.current) {
452-
const evalLines = normalizeInitialEvaluate(initialEvaluateRef.current);
459+
if (initialEvaluate) {
460+
const evalLines = normalizeInitialEvaluate(initialEvaluate);
453461
for (const input of evalLines) {
454462
await onInput(input);
455463
}
456464
}
457465
});
458-
}, [onInput, updateShellPrompt]);
466+
}, [initialEvaluate, isFirstRun, onInput, updateShellPrompt]);
459467

460468
useEffect(() => {
461-
scrollToBottom();
469+
rafraf(() => {
470+
scrollToBottom();
471+
});
462472
});
463473

464474
/* eslint-disable jsx-a11y/no-static-element-interactions */

0 commit comments

Comments
 (0)