Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore(sql-editor): improve SQL Editor's WebSocket experience (#12579)

Liu Ji45559335ce69
diff --git a/frontend/src/components/MonacoEditor/editor.ts b/frontend/src/components/MonacoEditor/editor.ts
index 0a48b89131..a3d4b1bd45 100644
--- a/frontend/src/components/MonacoEditor/editor.ts
+++ b/frontend/src/components/MonacoEditor/editor.ts
@@ -39,15 +39,30 @@ const initializeTheme = () => {
state.themeInitialized = true;
};
+const initializeNewLSP = async () => {
+ const { initializeLSPClient } = await import("./lsp-client");
+ await initializeLSPClient();
+};
+
+const initializeLegacyLSP = async () => {
+ const { useLanguageClient } = await import("@/plugins/sql-lsp/client");
+ const { start } = useLanguageClient();
+ start();
+};
+
const initialize = async () => {
await initializeMonacoServices();
+
if (shouldUseNewLSP()) {
- const { initializeLSPClient } = await import("./lsp-client");
- await initializeLSPClient();
+ try {
+ await initializeNewLSP();
+ } catch (err) {
+ console.error("[MonacoEditor] initialize", err);
+
+ await initializeLegacyLSP();
+ }
} else {
- const { useLanguageClient } = await import("@/plugins/sql-lsp/client");
- const { start } = useLanguageClient();
- start();
+ await initializeLegacyLSP();
}
initializeTheme();
@@ -93,7 +108,7 @@ export const defaultEditorOptions =
// Learn more: https://github.com/microsoft/monaco-editor/issues/311
renderValidationDecorations: "on",
// Learn more: https://github.com/microsoft/monaco-editor/issues/4270
- accessibilitySupport: 'off',
+ accessibilitySupport: "off",
theme: "bb",
tabSize: 2,
insertSpaces: true,
@@ -128,7 +143,7 @@ export const defaultDiffEditorOptions =
// Learn more: https://github.com/microsoft/monaco-editor/issues/311
enableSplitViewResizing: false,
// Learn more: https://github.com/microsoft/monaco-editor/issues/4270
- accessibilitySupport: 'off',
+ accessibilitySupport: "off",
renderValidationDecorations: "on",
theme: "bb",
autoClosingQuotes: "always",
diff --git a/frontend/src/components/MonacoEditor/lsp-client.ts b/frontend/src/components/MonacoEditor/lsp-client.ts
index 29c6921476..cb70485fa9 100644
--- a/frontend/src/components/MonacoEditor/lsp-client.ts
+++ b/frontend/src/components/MonacoEditor/lsp-client.ts
@@ -9,6 +9,9 @@ import {
WebSocketMessageWriter,
toSocket,
} from "vscode-ws-jsonrpc";
+import { h } from "vue";
+import { pushNotification } from "@/store";
+import LearnMoreLink from "../LearnMoreLink.vue";
export const createLanguageClient = (
transports: MessageTransports
@@ -59,20 +62,41 @@ export const createWebSocketAndStartClient = (
languageClient: Promise<MonacoLanguageClient>;
} => {
const webSocket = new WebSocket(url);
- const languageClient = new Promise<MonacoLanguageClient>((resolve) => {
- webSocket.onopen = () => {
- const socket = toSocket(webSocket);
- const reader = new WebSocketMessageReader(socket);
- const writer = new WebSocketMessageWriter(socket);
- const languageClient = createLanguageClient({
- reader,
- writer,
- });
- languageClient.start();
- reader.onClose(() => languageClient.stop());
- resolve(languageClient);
- };
- });
+ const languageClient = new Promise<MonacoLanguageClient>(
+ (resolve, reject) => {
+ webSocket.onopen = () => {
+ const socket = toSocket(webSocket);
+ const reader = new WebSocketMessageReader(socket);
+ const writer = new WebSocketMessageWriter(socket);
+ const languageClient = createLanguageClient({
+ reader,
+ writer,
+ });
+ languageClient.start();
+ reader.onClose(() => languageClient.stop());
+ resolve(languageClient);
+ };
+ webSocket.onerror = (e: any) => {
+ console.error("[MonacoLanguageClient] WebSocket error", e);
+ const message = typeof e.message === "string" ? e.message : "";
+ pushNotification({
+ module: "bytebase",
+ style: "CRITICAL",
+ title: "Error",
+ description: () => {
+ return [
+ h("p", {}, `Error occurred when initializing WebSocket`),
+ message ? h("p", {}, message) : null,
+ h(LearnMoreLink, {
+ url: "https://www.bytebase.com/docs/administration/production-setup/#enable-https-and-websocket",
+ }),
+ ];
+ },
+ });
+ reject(e);
+ };
+ }
+ );
return {
webSocket,