Fixed issues
This commit is contained in:
@@ -41,10 +41,6 @@ function getLanguageExtension(lang: Language) {
|
|||||||
|
|
||||||
export default function Editor({ run }: { run: LiveRun | null | undefined }) {
|
export default function Editor({ run }: { run: LiveRun | null | undefined }) {
|
||||||
const [editorView, setEditorView] = useState<EditorView>();
|
const [editorView, setEditorView] = useState<EditorView>();
|
||||||
const [isConnected, setConnected] = useState(false);
|
|
||||||
|
|
||||||
const [rawMoves, setRawMoves] = useState<LiveRunMove[]>([]);
|
|
||||||
const [movesLoading, setMovesLoading] = useState(false);
|
|
||||||
const scheduledTimeouts = useRef<NodeJS.Timeout[]>([]);
|
const scheduledTimeouts = useRef<NodeJS.Timeout[]>([]);
|
||||||
|
|
||||||
// WebSocket connection
|
// WebSocket connection
|
||||||
@@ -59,25 +55,8 @@ export default function Editor({ run }: { run: LiveRun | null | undefined }) {
|
|||||||
|
|
||||||
ws.onmessage = (m) => {
|
ws.onmessage = (m) => {
|
||||||
const data: LiveRunMove[] = JSON.parse(m.data);
|
const data: LiveRunMove[] = JSON.parse(m.data);
|
||||||
setRawMoves(data);
|
|
||||||
};
|
|
||||||
|
|
||||||
return () => ws.close();
|
data.forEach((move) => {
|
||||||
}, [run]);
|
|
||||||
|
|
||||||
// Scheduler
|
|
||||||
useEffect(() => {
|
|
||||||
if (!editorView || !rawMoves.length) return;
|
|
||||||
|
|
||||||
setMovesLoading(true);
|
|
||||||
|
|
||||||
// Clear any existing scheduled timeouts
|
|
||||||
scheduledTimeouts.current.forEach((t) => clearTimeout(t));
|
|
||||||
scheduledTimeouts.current = [];
|
|
||||||
|
|
||||||
const startTime = Date.now();
|
|
||||||
|
|
||||||
rawMoves.forEach((move) => {
|
|
||||||
const timeout = setTimeout(() => {
|
const timeout = setTimeout(() => {
|
||||||
if (!editorView) return;
|
if (!editorView) return;
|
||||||
|
|
||||||
@@ -90,21 +69,10 @@ export default function Editor({ run }: { run: LiveRun | null | undefined }) {
|
|||||||
|
|
||||||
scheduledTimeouts.current.push(timeout);
|
scheduledTimeouts.current.push(timeout);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Stop loading after the last move
|
|
||||||
const lastMoveLatency = rawMoves[rawMoves.length - 1].latency;
|
|
||||||
const finishTimeout = setTimeout(
|
|
||||||
() => setMovesLoading(false),
|
|
||||||
lastMoveLatency + 50,
|
|
||||||
);
|
|
||||||
scheduledTimeouts.current.push(finishTimeout);
|
|
||||||
|
|
||||||
return () => {
|
|
||||||
scheduledTimeouts.current.forEach((t) => clearTimeout(t));
|
|
||||||
scheduledTimeouts.current = [];
|
|
||||||
setMovesLoading(false);
|
|
||||||
};
|
};
|
||||||
}, [rawMoves, editorView]);
|
|
||||||
|
return () => ws.close();
|
||||||
|
}, [run]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<CodeMirror
|
<CodeMirror
|
||||||
|
|||||||
Reference in New Issue
Block a user