Skip to content

Commit 90291d5

Browse files
authored
fix safari not updating state correctly (#484)
1 parent b8486ca commit 90291d5

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/hooks/usePythonConsole.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export default function usePythonConsole(props?: UsePythonConsoleProps) {
2626
const [banner, setBanner] = useState<string | undefined>()
2727
const [consoleState, setConsoleState] = useState<ConsoleState>()
2828
const [isRunning, setIsRunning] = useState(false)
29+
const [output, setOutput] = useState<string[]>([])
2930
const [stdout, setStdout] = useState('')
3031
const [stderr, setStderr] = useState('')
3132
const [pendingCode, setPendingCode] = useState<string | undefined>()
@@ -72,6 +73,13 @@ export default function usePythonConsole(props?: UsePythonConsoleProps) {
7273
}
7374
}, [])
7475

76+
// Immediately set stdout upon receiving new input
77+
useEffect(() => {
78+
if (output.length > 0 && !isRunning) {
79+
setStdout(output.join(''))
80+
}
81+
}, [output, isRunning])
82+
7583
const allPackages = useMemo(() => {
7684
const official = [
7785
...new Set([
@@ -104,7 +112,7 @@ export default function usePythonConsole(props?: UsePythonConsoleProps) {
104112
if (suppressedMessages.includes(msg)) {
105113
return
106114
}
107-
setStdout(msg)
115+
setOutput((prev) => [...prev, msg])
108116
}),
109117
proxy(({ id, version, banner }) => {
110118
setRunnerId(id)
@@ -150,6 +158,7 @@ del sys
150158
const runPython = useCallback(
151159
async (code: string) => {
152160
// Clear stdout and stderr
161+
setOutput([])
153162
setStdout('')
154163
setStderr('')
155164

0 commit comments

Comments
 (0)