Skip to content

Commit c1bdb59

Browse files
authored
fix(browser-repl): don't override output that came from onPrint (#2304)
don't override output that came from onPrint
1 parent 7c26c7a commit c1bdb59

File tree

2 files changed

+44
-20
lines changed

2 files changed

+44
-20
lines changed

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

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,22 @@ describe('shell', function () {
372372
expect(filterEvaluateCalls(fakeRuntime.evaluate.args)).to.have.length(1);
373373
});
374374

375-
it('prints when onPrint is called', async function () {
375+
it('prints when onPrint is called while evaluating', async function () {
376+
fakeRuntime = {
377+
evaluate: sinon.stub().callsFake(async (command: string) => {
378+
if (command === 'my command') {
379+
// don't print every time we update the prompt, only once for the actual command being input
380+
await listener?.onPrint?.([
381+
{ type: null, printable: 'from inside onPrint' },
382+
]);
383+
}
384+
return { printable: 'some result' };
385+
}),
386+
setEvaluationListener: (_listener) => {
387+
listener = _listener;
388+
},
389+
};
390+
376391
const initialEvaluate = 'my command';
377392

378393
let output = [];
@@ -389,16 +404,22 @@ describe('shell', function () {
389404
/>
390405
);
391406

392-
await waitFor(() => expect(listener).to.exist);
393-
await listener?.onPrint?.([{ type: null, printable: 42 }]);
394-
395407
await waitFor(() => {
396408
expect(output[output.length - 1]).to.deep.equal({
397409
format: 'output',
398-
type: null,
399-
value: 42,
410+
type: undefined,
411+
value: 'some result',
400412
});
401413
});
414+
415+
expect(output).to.deep.equal([
416+
// we typed "my command"
417+
{ format: 'input', value: 'my command' },
418+
// while evaluating it printed something
419+
{ format: 'output', type: null, value: 'from inside onPrint' },
420+
// we then printed the result of the evaluation
421+
{ format: 'output', type: undefined, value: 'some result' },
422+
]);
402423
});
403424

404425
it('clears the output when onClearCommand is called', async function () {

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

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -375,39 +375,42 @@ const _Shell: ForwardRefRenderFunction<EditorRef | null, ShellProps> = (
375375

376376
const onInput = useCallback(
377377
async (code: string) => {
378-
const newOutput = [...(outputRef.current ?? [])];
379-
const newHistory = [...(historyRef.current ?? [])];
378+
const newOutputBeforeEval = [...(outputRef.current ?? [])];
380379

381380
// don't evaluate empty input, but do add it to the output
382381
if (!code || code.trim() === '') {
383-
newOutput.push({
382+
newOutputBeforeEval.push({
384383
format: 'input',
385384
value: ' ',
386385
});
387-
capLengthEnd(newOutput, maxOutputLength);
388-
outputRef.current = newOutput;
389-
onOutputChanged?.(newOutput);
386+
capLengthEnd(newOutputBeforeEval, maxOutputLength);
387+
outputRef.current = newOutputBeforeEval;
388+
onOutputChanged?.(newOutputBeforeEval);
390389
return;
391390
}
392391

393392
// add input to output
394-
newOutput.push({
393+
newOutputBeforeEval.push({
395394
format: 'input',
396395
value: code,
397396
});
398-
capLengthEnd(newOutput, maxOutputLength);
399-
outputRef.current = newOutput;
400-
onOutputChanged?.(newOutput);
397+
capLengthEnd(newOutputBeforeEval, maxOutputLength);
398+
outputRef.current = newOutputBeforeEval;
399+
onOutputChanged?.(newOutputBeforeEval);
401400

402401
const outputLine = await evaluate(code);
403402

403+
// outputRef.current could have changed if evaluate() used onPrint
404+
const newOutputAfterEval = [...(outputRef.current ?? [])];
405+
404406
// add output to output
405-
newOutput.push(outputLine);
406-
capLengthEnd(newOutput, maxOutputLength);
407-
outputRef.current = newOutput;
408-
onOutputChanged?.(newOutput);
407+
newOutputAfterEval.push(outputLine);
408+
capLengthEnd(newOutputAfterEval, maxOutputLength);
409+
outputRef.current = newOutputAfterEval;
410+
onOutputChanged?.(newOutputAfterEval);
409411

410412
// update history
413+
const newHistory = [...(historyRef.current ?? [])];
411414
newHistory.unshift(code);
412415
capLengthStart(newHistory, maxHistoryLength);
413416
changeHistory(

0 commit comments

Comments
 (0)