mirror of
https://litchi.icu/ngc2207/judge.git
synced 2025-05-18 20:07:41 +00:00
fix(playground): improve language change handling with proper cleanup and error handling
This commit is contained in:
parent
58e4f1220b
commit
d1d8edb8cc
@ -51,31 +51,43 @@ export default function PlaygroundPage() {
|
|||||||
const languageClientRef = useRef<MonacoLanguageClient | null>(null);
|
const languageClientRef = useRef<MonacoLanguageClient | null>(null);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (editorRef.current) {
|
const handleLanguageChange = async () => {
|
||||||
const model = editorRef.current.getModel();
|
if (editorRef.current) {
|
||||||
if (model) {
|
const model = editorRef.current.getModel();
|
||||||
const lineCount = model.getLineCount();
|
if (model) {
|
||||||
const lastLineLength = model.getLineLength(lineCount);
|
const lineCount = model.getLineCount();
|
||||||
editorRef.current.setPosition({
|
const lastLineLength = model.getLineLength(lineCount);
|
||||||
lineNumber: lineCount,
|
editorRef.current.setPosition({
|
||||||
column: lastLineLength + 1,
|
lineNumber: lineCount,
|
||||||
});
|
column: lastLineLength + 1,
|
||||||
editorRef.current.focus();
|
});
|
||||||
if (webSocketRef.current) {
|
editorRef.current.focus();
|
||||||
webSocketRef.current.close();
|
if (languageClientRef.current) {
|
||||||
webSocketRef.current = null;
|
await languageClientRef.current.stop();
|
||||||
}
|
languageClientRef.current = null;
|
||||||
if (languageClientRef.current) {
|
|
||||||
languageClientRef.current.stop();
|
|
||||||
languageClientRef.current = null;
|
|
||||||
}
|
|
||||||
connectToLanguageServer(language, webSocketRef).then(
|
|
||||||
(languageClient) => {
|
|
||||||
languageClientRef.current = languageClient;
|
|
||||||
}
|
}
|
||||||
);
|
if (
|
||||||
|
webSocketRef.current &&
|
||||||
|
webSocketRef.current.readyState !== WebSocket.CLOSED &&
|
||||||
|
webSocketRef.current.readyState !== WebSocket.CLOSING
|
||||||
|
) {
|
||||||
|
webSocketRef.current.close();
|
||||||
|
webSocketRef.current = null;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const languageClient = await connectToLanguageServer(
|
||||||
|
language,
|
||||||
|
webSocketRef
|
||||||
|
);
|
||||||
|
languageClientRef.current = languageClient;
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Failed to connect to language server:", error);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
|
handleLanguageChange();
|
||||||
}, [language]);
|
}, [language]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@ -146,11 +158,13 @@ export default function PlaygroundPage() {
|
|||||||
column: lastLineLength + 1,
|
column: lastLineLength + 1,
|
||||||
});
|
});
|
||||||
editorRef.current.focus();
|
editorRef.current.focus();
|
||||||
connectToLanguageServer(language, webSocketRef).then(
|
connectToLanguageServer(language, webSocketRef)
|
||||||
(languageClient) => {
|
.then((languageClient) => {
|
||||||
languageClientRef.current = languageClient;
|
languageClientRef.current = languageClient;
|
||||||
}
|
})
|
||||||
);
|
.catch((error) => {
|
||||||
|
console.error("Failed to connect to language server:", error);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
|
Loading…
Reference in New Issue
Block a user