msyavuz commented on code in PR #37642:
URL: https://github.com/apache/superset/pull/37642#discussion_r2763111797
##########
superset-frontend/src/core/sqlLab/index.ts:
##########
@@ -70,39 +78,117 @@ const findQueryEditor = (editorId: string) => {
return editor;
};
-const createTab = (
+/**
+ * Registry for editor handles. Editor components register their handles here
+ * when they mount, allowing the SQL Lab API to access them.
+ */
+const editorHandleRegistry = new Map<string, sqlLabApi.Editor>();
+
+/**
+ * Pending promises waiting for editor handles to be registered.
+ */
+const pendingEditorPromises = new Map<
+ string,
+ Array<(handle: sqlLabApi.Editor) => void>
+>();
+
+/**
+ * Registers an editor handle for a tab. Called by EditorWrapper when it
mounts.
+ * Resolves any pending promises waiting for this editor.
+ */
+export const registerEditorHandle = (
+ tabId: string,
+ handle: sqlLabApi.Editor,
+): void => {
+ editorHandleRegistry.set(tabId, handle);
+
+ // Resolve any pending promises waiting for this editor
+ const pending = pendingEditorPromises.get(tabId);
+ if (pending) {
+ pending.forEach(resolve => resolve(handle));
+ pendingEditorPromises.delete(tabId);
+ }
+};
+
+/**
+ * Unregisters an editor handle for a tab. Called when EditorWrapper unmounts.
+ */
+export const unregisterEditorHandle = (tabId: string): void => {
+ editorHandleRegistry.delete(tabId);
+};
+
+/**
+ * Creates a Proxy that always delegates to the current editor handle in the
registry.
+ * This handles editor hot-swapping (e.g., Ace to Monaco).
+ */
+const createEditorProxy = (tabId: string): sqlLabApi.Editor =>
+ new Proxy({} as sqlLabApi.Editor, {
+ get(_, prop: keyof sqlLabApi.Editor) {
+ const handle = editorHandleRegistry.get(tabId);
+ if (!handle) {
+ throw new Error(`Editor handle not found for tab ${tabId}`);
+ }
+ const value = handle[prop];
+ return typeof value === 'function' ? value.bind(handle) : value;
+ },
+ });
+
+/**
+ * Gets the editor for a tab, waiting for it to be registered if necessary.
+ * Returns a Proxy that always delegates to the current handle.
+ */
+const getEditorAsync = (tabId: string): Promise<sqlLabApi.Editor> => {
+ const existingHandle = editorHandleRegistry.get(tabId);
+ if (existingHandle) {
+ // Editor already registered, return proxy immediately
+ return Promise.resolve(createEditorProxy(tabId));
+ }
+
+ // Wait for the editor to be registered
+ return new Promise(resolve => {
+ const pending = pendingEditorPromises.get(tabId) ?? [];
+ pending.push(() => resolve(createEditorProxy(tabId)));
+ pendingEditorPromises.set(tabId, pending);
+ });
+};
+
+const makeTab = (
id: string,
name: string,
- sql: string,
dbId: number,
- catalog?: string,
- schema?: string,
- table?: any,
-) => {
- const editor = new Editor(sql, dbId, catalog, schema, table);
+ catalog?: string | null,
+ schema?: string | null,
+): Tab => {
const panels: Panel[] = []; // TODO: Populate panels
- return new Tab(id, name, editor, panels);
+ return new Tab(
+ id,
+ name,
+ dbId,
+ catalog ?? null,
+ schema ?? null,
Review Comment:
Should we default these in the parameters?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]