Skip to content

Commit add38a0

Browse files
committed
more stable
1 parent 9b15c06 commit add38a0

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ describe('shell', function () {
120120

121121
expect(filterEvaluateCalls(fakeRuntime.evaluate.args)).to.have.length(1);
122122

123-
// scrolls to the bottom initially
124-
expect(Element.prototype.scrollIntoView).to.have.been.calledOnce;
123+
// scrolls to the bottom initially and every time it outputs
124+
expect(Element.prototype.scrollIntoView).to.have.been.calledTwice;
125125

126126
// make sure we scroll to the bottom every time output changes
127127
rerender(
@@ -133,7 +133,7 @@ describe('shell', function () {
133133
/>
134134
);
135135
await waitFor(() => {
136-
expect(Element.prototype.scrollIntoView).to.have.been.calledTwice;
136+
expect(Element.prototype.scrollIntoView).to.have.been.calledThrice;
137137
});
138138
});
139139

@@ -478,7 +478,7 @@ describe('shell', function () {
478478
render(<ShellWrapper runtime={fakeRuntime} />);
479479

480480
await waitFor(() => {
481-
expect(Element.prototype.scrollIntoView).to.have.been.calledOnce;
481+
expect(Element.prototype.scrollIntoView).to.have.been.calledTwice;
482482
});
483483

484484
expect(screen.getByLabelText('Chevron Right Icon')).to.exist;
@@ -492,7 +492,7 @@ describe('shell', function () {
492492
render(<ShellWrapper runtime={fakeRuntime} />);
493493

494494
await waitFor(() => {
495-
expect(Element.prototype.scrollIntoView).to.have.been.calledOnce;
495+
expect(Element.prototype.scrollIntoView).to.have.been.calledTwice;
496496
});
497497

498498
expect(screen.getByText('$custom$')).to.exist;
@@ -509,7 +509,7 @@ describe('shell', function () {
509509
render(<ShellWrapper runtime={fakeRuntime} />);
510510

511511
await waitFor(() => {
512-
expect(Element.prototype.scrollIntoView).to.have.been.calledOnce;
512+
expect(Element.prototype.scrollIntoView).to.have.been.calledTwice;
513513
});
514514

515515
expect(screen.getByText('abc')).to.exist;

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,10 @@ const _Shell: ForwardRefRenderFunction<EditorRef | null, ShellProps> = (
202202
const darkMode = useDarkMode();
203203

204204
const editorRef = useRef<EditorRef | null>(null);
205+
const outputRef = useRef(output);
206+
const historyRef = useRef(history);
207+
const shellInputContainerRef = useRef<HTMLDivElement>(null);
208+
const initialEvaluateRef = useRef(initialEvaluate);
205209

206210
useImperativeHandle(
207211
ref,
@@ -233,8 +237,6 @@ const _Shell: ForwardRefRenderFunction<EditorRef | null, ShellProps> = (
233237
[]
234238
);
235239

236-
const shellInputContainerRef = useRef<HTMLDivElement>(null);
237-
238240
const [passwordPrompt, setPasswordPrompt] = useState('');
239241
const [shellPrompt, setShellPrompt] = useState('>');
240242
const [onFinishPasswordPrompt, setOnFinishPasswordPrompt] = useState<
@@ -252,7 +254,7 @@ const _Shell: ForwardRefRenderFunction<EditorRef | null, ShellProps> = (
252254
return {
253255
onPrint: (result: RuntimeEvaluationResult[]): void => {
254256
const newOutput = [
255-
...(output ?? []),
257+
...(outputRef.current ?? []),
256258
...result.map(
257259
(entry): ShellOutputEntry => ({
258260
format: 'output',
@@ -299,7 +301,7 @@ const _Shell: ForwardRefRenderFunction<EditorRef | null, ShellProps> = (
299301
onOutputChanged?.([]);
300302
},
301303
};
302-
}, [focusEditor, maxOutputLength, onOutputChanged, output]);
304+
}, [focusEditor, maxOutputLength, onOutputChanged]);
303305

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

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

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

449-
const initialEvaluateRef = useRef(initialEvaluate);
450-
451449
useEffect(() => {
452-
scrollToBottom();
453-
454450
void updateShellPrompt().then(async () => {
455451
if (initialEvaluateRef.current) {
456452
const evalLines = normalizeInitialEvaluate(initialEvaluateRef.current);
@@ -459,7 +455,11 @@ const _Shell: ForwardRefRenderFunction<EditorRef | null, ShellProps> = (
459455
}
460456
}
461457
});
462-
}, [onInput, scrollToBottom, updateShellPrompt]);
458+
}, [onInput, updateShellPrompt]);
459+
460+
useEffect(() => {
461+
scrollToBottom();
462+
});
463463

464464
/* eslint-disable jsx-a11y/no-static-element-interactions */
465465
/* eslint-disable jsx-a11y/click-events-have-key-events */

0 commit comments

Comments
 (0)