Skip to content

Commit 001771f

Browse files
committed
clear timeout
1 parent fe0dbc0 commit 001771f

File tree

2 files changed

+32
-21
lines changed

2 files changed

+32
-21
lines changed

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

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,33 @@ export type { ShellOutputEntry } from './shell-output-line';
88

99
type ShellIOListProps = {
1010
output: ShellOutputEntry[];
11-
InputPrompt: JSX.Element;
11+
renderInputPrompt: () => JSX.Element;
1212
setScrollRef: (ref: HTMLDivElement) => void;
13+
__TEST_OVERSCAN_COUNT?: number;
14+
__TEST_LIST_HEIGHT?: number;
1315
};
1416

15-
export const ShellIOList = ({
17+
export const ShellOutput = ({
1618
output,
17-
InputPrompt,
19+
renderInputPrompt,
1820
setScrollRef,
21+
__TEST_OVERSCAN_COUNT,
22+
__TEST_LIST_HEIGHT,
1923
}: ShellIOListProps) => {
2024
const listRef: VirtualListRef = useRef();
2125

2226
useEffect(() => {
23-
if (listRef.current) {
24-
listRef.current.resetAfterIndex(0);
25-
setTimeout(() => {
26-
// After output changes, scroll to the end (which is
27-
// prompt input)
28-
listRef.current.scrollToItem(output.length, 'end');
29-
}, 100);
27+
if (!listRef.current) {
28+
return;
3029
}
30+
(window as any).listRef = listRef.current;
31+
listRef.current.resetAfterIndex(0);
32+
const timeout = setTimeout(() => {
33+
// After output changes, scroll to the end (which is
34+
// prompt input)
35+
listRef.current.scrollToItem(output.length, 'end');
36+
}, 100);
37+
return () => clearTimeout(timeout);
3138
}, [output, listRef]);
3239

3340
// Adding prompt to the input list so that its also rendered
@@ -38,21 +45,25 @@ export const ShellIOList = ({
3845
<VirtualList
3946
dataTestId="shell-output-virtual-list"
4047
items={items}
41-
overScanCount={20}
48+
overScanCount={__TEST_OVERSCAN_COUNT ?? 1}
4249
listRef={listRef}
4350
scrollableContainerRef={setScrollRef}
4451
renderItem={(item, ref) => {
52+
if (item.type === 'inputPrompt') {
53+
return (
54+
<div ref={ref} data-testid="shell-input-prompt">
55+
{renderInputPrompt()}
56+
</div>
57+
);
58+
}
4559
return (
46-
<div ref={ref}>
47-
{item.type === 'inputPrompt' ? (
48-
InputPrompt
49-
) : (
50-
<ShellOutputLine entry={item as ShellOutputEntry} />
51-
)}
60+
<div ref={ref} data-testid="shell-output-line">
61+
<ShellOutputLine entry={item as ShellOutputEntry} />
5262
</div>
5363
);
5464
}}
5565
estimateItemInitialHeight={() => 24}
66+
__TEST_LIST_HEIGHT={__TEST_LIST_HEIGHT}
5667
/>
5768
);
5869
};

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import type { WorkerRuntime } from '@mongosh/node-runtime-worker-thread';
2626
import { PasswordPrompt } from './password-prompt';
2727
import { ShellInput } from './shell-input';
2828
import type { ShellOutputEntry } from './shell-output';
29-
import { ShellIOList } from './shell-output';
29+
import { ShellOutput } from './shell-output';
3030

3131
const shellContainer = css({
3232
fontSize: '13px',
@@ -551,10 +551,10 @@ const _Shell: ForwardRefRenderFunction<EditorRef | null, ShellProps> = (
551551
)}
552552
onClick={onShellClicked}
553553
>
554-
<ShellIOList
554+
<ShellOutput
555555
setScrollRef={setScrollRef}
556556
output={output ?? []}
557-
InputPrompt={
557+
renderInputPrompt={() => (
558558
<div ref={shellInputContainerRef}>
559559
{passwordPrompt ? (
560560
<PasswordPrompt
@@ -580,7 +580,7 @@ const _Shell: ForwardRefRenderFunction<EditorRef | null, ShellProps> = (
580580
/>
581581
)}
582582
</div>
583-
}
583+
)}
584584
/>
585585
</div>
586586
);

0 commit comments

Comments
 (0)