Skip to content

Commit

Permalink
use refs and update them
Browse files Browse the repository at this point in the history
  • Loading branch information
lerouxb committed Dec 13, 2024
1 parent 37e3c8b commit 4e66b19
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 23 deletions.
12 changes: 5 additions & 7 deletions packages/browser-repl/src/components/shell.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -393,13 +393,11 @@ describe('shell', function () {
await listener?.onPrint?.([{ type: null, printable: 42 }]);

await waitFor(() => {
expect(output).to.deep.equal([
{
format: 'output',
type: null,
value: 42,
},
]);
expect(output[output.length - 1]).to.deep.equal({
format: 'output',
type: null,
value: 42,
});
});
});

Expand Down
31 changes: 15 additions & 16 deletions packages/browser-repl/src/components/shell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@ const _Shell: ForwardRefRenderFunction<EditorRef | null, ShellProps> = (

const editorRef = useRef<EditorRef | null>(null);
const shellInputContainerRef = useRef<HTMLDivElement>(null);
const initialEvaluateRef = useRef(initialEvaluate);
const outputRef = useRef(output);
const historyRef = useRef(history);

useImperativeHandle(
ref,
Expand Down Expand Up @@ -256,7 +259,7 @@ const _Shell: ForwardRefRenderFunction<EditorRef | null, ShellProps> = (
return {
onPrint: (result: RuntimeEvaluationResult[]): void => {
const newOutput = [
...(output ?? []),
...(outputRef.current ?? []),
...result.map(
(entry): ShellOutputEntry => ({
format: 'output',
Expand All @@ -267,6 +270,7 @@ const _Shell: ForwardRefRenderFunction<EditorRef | null, ShellProps> = (
];

capLengthEnd(newOutput, maxOutputLength);
outputRef.current = newOutput;
onOutputChanged?.(newOutput);
},
onPrompt: async (
Expand Down Expand Up @@ -300,10 +304,11 @@ const _Shell: ForwardRefRenderFunction<EditorRef | null, ShellProps> = (
return ret;
},
onClearCommand: (): void => {
outputRef.current = [];
onOutputChanged?.([]);
},
};
}, [focusEditor, maxOutputLength, onOutputChanged, output]);
}, [focusEditor, maxOutputLength, onOutputChanged]);

const updateShellPrompt = useCallback(async (): Promise<void> => {
let newShellPrompt = '>';
Expand Down Expand Up @@ -370,8 +375,8 @@ const _Shell: ForwardRefRenderFunction<EditorRef | null, ShellProps> = (

const onInput = useCallback(
async (code: string) => {
const newOutput = [...(output ?? [])];
const newHistory = [...(history ?? [])];
const newOutput = [...(outputRef.current ?? [])];
const newHistory = [...(historyRef.current ?? [])];

// don't evaluate empty input, but do add it to the output
if (!code || code.trim() === '') {
Expand All @@ -380,6 +385,7 @@ const _Shell: ForwardRefRenderFunction<EditorRef | null, ShellProps> = (
value: ' ',
});
capLengthEnd(newOutput, maxOutputLength);
outputRef.current = newOutput;
onOutputChanged?.(newOutput);
return;
}
Expand All @@ -390,13 +396,15 @@ const _Shell: ForwardRefRenderFunction<EditorRef | null, ShellProps> = (
value: code,
});
capLengthEnd(newOutput, maxOutputLength);
outputRef.current = newOutput;
onOutputChanged?.(newOutput);

const outputLine = await evaluate(code);

// add output to output
newOutput.push(outputLine);
capLengthEnd(newOutput, maxOutputLength);
outputRef.current = newOutput;
onOutputChanged?.(newOutput);

// update history
Expand All @@ -406,11 +414,10 @@ const _Shell: ForwardRefRenderFunction<EditorRef | null, ShellProps> = (
newHistory,
redactInfo ? 'redact-sensitive-data' : 'keep-sensitive-data'
);
historyRef.current = newHistory;
onHistoryChanged?.(newHistory);
},
[
output,
history,
onOutputChanged,
evaluate,
redactInfo,
Expand Down Expand Up @@ -450,16 +457,8 @@ const _Shell: ForwardRefRenderFunction<EditorRef | null, ShellProps> = (
return Promise.resolve(false);
}, [isOperationInProgress, runtime]);

const [isFirstRun, setIsFirstRun] = useState(true);

useEffect(() => {
if (!isFirstRun) {
return;
}

setIsFirstRun(false);

const evalLines = normalizeInitialEvaluate(initialEvaluate);
const evalLines = normalizeInitialEvaluate(initialEvaluateRef.current);
if (evalLines.length) {
void (async () => {
for (const input of evalLines) {
Expand All @@ -469,7 +468,7 @@ const _Shell: ForwardRefRenderFunction<EditorRef | null, ShellProps> = (
} else {
void updateShellPrompt();
}
}, [initialEvaluate, isFirstRun, onInput, updateShellPrompt]);
}, [onInput, updateShellPrompt]);

useEffect(() => {
rafraf(() => {
Expand Down

0 comments on commit 4e66b19

Please sign in to comment.