Skip to content

Commit 4e66b19

Browse files
committed
use refs and update them
1 parent 37e3c8b commit 4e66b19

File tree

2 files changed

+20
-23
lines changed

2 files changed

+20
-23
lines changed

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -393,13 +393,11 @@ describe('shell', function () {
393393
await listener?.onPrint?.([{ type: null, printable: 42 }]);
394394

395395
await waitFor(() => {
396-
expect(output).to.deep.equal([
397-
{
398-
format: 'output',
399-
type: null,
400-
value: 42,
401-
},
402-
]);
396+
expect(output[output.length - 1]).to.deep.equal({
397+
format: 'output',
398+
type: null,
399+
value: 42,
400+
});
403401
});
404402
});
405403

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

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,9 @@ const _Shell: ForwardRefRenderFunction<EditorRef | null, ShellProps> = (
208208

209209
const editorRef = useRef<EditorRef | null>(null);
210210
const shellInputContainerRef = useRef<HTMLDivElement>(null);
211+
const initialEvaluateRef = useRef(initialEvaluate);
212+
const outputRef = useRef(output);
213+
const historyRef = useRef(history);
211214

212215
useImperativeHandle(
213216
ref,
@@ -256,7 +259,7 @@ const _Shell: ForwardRefRenderFunction<EditorRef | null, ShellProps> = (
256259
return {
257260
onPrint: (result: RuntimeEvaluationResult[]): void => {
258261
const newOutput = [
259-
...(output ?? []),
262+
...(outputRef.current ?? []),
260263
...result.map(
261264
(entry): ShellOutputEntry => ({
262265
format: 'output',
@@ -267,6 +270,7 @@ const _Shell: ForwardRefRenderFunction<EditorRef | null, ShellProps> = (
267270
];
268271

269272
capLengthEnd(newOutput, maxOutputLength);
273+
outputRef.current = newOutput;
270274
onOutputChanged?.(newOutput);
271275
},
272276
onPrompt: async (
@@ -300,10 +304,11 @@ const _Shell: ForwardRefRenderFunction<EditorRef | null, ShellProps> = (
300304
return ret;
301305
},
302306
onClearCommand: (): void => {
307+
outputRef.current = [];
303308
onOutputChanged?.([]);
304309
},
305310
};
306-
}, [focusEditor, maxOutputLength, onOutputChanged, output]);
311+
}, [focusEditor, maxOutputLength, onOutputChanged]);
307312

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

371376
const onInput = useCallback(
372377
async (code: string) => {
373-
const newOutput = [...(output ?? [])];
374-
const newHistory = [...(history ?? [])];
378+
const newOutput = [...(outputRef.current ?? [])];
379+
const newHistory = [...(historyRef.current ?? [])];
375380

376381
// don't evaluate empty input, but do add it to the output
377382
if (!code || code.trim() === '') {
@@ -380,6 +385,7 @@ const _Shell: ForwardRefRenderFunction<EditorRef | null, ShellProps> = (
380385
value: ' ',
381386
});
382387
capLengthEnd(newOutput, maxOutputLength);
388+
outputRef.current = newOutput;
383389
onOutputChanged?.(newOutput);
384390
return;
385391
}
@@ -390,13 +396,15 @@ const _Shell: ForwardRefRenderFunction<EditorRef | null, ShellProps> = (
390396
value: code,
391397
});
392398
capLengthEnd(newOutput, maxOutputLength);
399+
outputRef.current = newOutput;
393400
onOutputChanged?.(newOutput);
394401

395402
const outputLine = await evaluate(code);
396403

397404
// add output to output
398405
newOutput.push(outputLine);
399406
capLengthEnd(newOutput, maxOutputLength);
407+
outputRef.current = newOutput;
400408
onOutputChanged?.(newOutput);
401409

402410
// update history
@@ -406,11 +414,10 @@ const _Shell: ForwardRefRenderFunction<EditorRef | null, ShellProps> = (
406414
newHistory,
407415
redactInfo ? 'redact-sensitive-data' : 'keep-sensitive-data'
408416
);
417+
historyRef.current = newHistory;
409418
onHistoryChanged?.(newHistory);
410419
},
411420
[
412-
output,
413-
history,
414421
onOutputChanged,
415422
evaluate,
416423
redactInfo,
@@ -450,16 +457,8 @@ const _Shell: ForwardRefRenderFunction<EditorRef | null, ShellProps> = (
450457
return Promise.resolve(false);
451458
}, [isOperationInProgress, runtime]);
452459

453-
const [isFirstRun, setIsFirstRun] = useState(true);
454-
455460
useEffect(() => {
456-
if (!isFirstRun) {
457-
return;
458-
}
459-
460-
setIsFirstRun(false);
461-
462-
const evalLines = normalizeInitialEvaluate(initialEvaluate);
461+
const evalLines = normalizeInitialEvaluate(initialEvaluateRef.current);
463462
if (evalLines.length) {
464463
void (async () => {
465464
for (const input of evalLines) {
@@ -469,7 +468,7 @@ const _Shell: ForwardRefRenderFunction<EditorRef | null, ShellProps> = (
469468
} else {
470469
void updateShellPrompt();
471470
}
472-
}, [initialEvaluate, isFirstRun, onInput, updateShellPrompt]);
471+
}, [onInput, updateShellPrompt]);
473472

474473
useEffect(() => {
475474
rafraf(() => {

0 commit comments

Comments
 (0)