codeant-ai-for-open-source[bot] commented on code in PR #42561:
URL: https://github.com/apache/superset/pull/42561#discussion_r3673268421


##########
superset-frontend/src/utils/downloadUtils.ts:
##########
@@ -0,0 +1,84 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import { FeatureFlag, isFeatureEnabled } from '@superset-ui/core';
+import { t } from '@apache-superset/core/translation';
+import { logging } from '@apache-superset/core/utils';
+import { addWarningToast } from 'src/components/MessageToasts/actions';
+import {
+  FORCE_IN_VIEW_EVENT,
+  RESTORE_VIRTUALIZATION_EVENT,
+} from 'src/dashboard/constants';
+
+/**
+ * Poll until all `.loading` spinners inside a container disappear,
+ * indicating that lazy-loaded charts have finished rendering.
+ * Returns true if all charts loaded, false if timed out.
+ */
+function waitForChartsToLoad(
+  container: Element,
+  timeoutMs = 60_000,
+): Promise<boolean> {
+  return new Promise(resolve => {
+    const startTime = Date.now();
+    const check = () => {
+      const loadingElements = container.querySelectorAll('.loading');
+      if (loadingElements.length === 0) {
+        resolve(true);
+        return;
+      }
+      if (Date.now() - startTime > timeoutMs) {
+        logging.warn(
+          `Timed out waiting for ${loadingElements.length} chart(s) to load`,
+        );
+        resolve(false);
+        return;
+      }
+      setTimeout(check, 500);
+    };
+    setTimeout(check, 1000);
+  });
+}
+
+/**
+ * When DASHBOARD_VIRTUALIZATION is enabled, forces all lazy-loaded
+ * charts to render and waits for them to finish loading.
+ * Returns true if virtualization was active (caller must restore it).
+ */
+export async function forceLoadAllCharts(container: Element): Promise<boolean> 
{
+  const useVirtualization = isFeatureEnabled(
+    FeatureFlag.DashboardVirtualization,
+  );
+  if (useVirtualization) {
+    window.dispatchEvent(new Event(FORCE_IN_VIEW_EVENT));
+    const allLoaded = await waitForChartsToLoad(container);
+    if (!allLoaded) {
+      addWarningToast(
+        t('Some charts did not finish loading. The export may be incomplete.'),
+      );
+    }
+  }
+  return useVirtualization;
+}
+
+/**
+ * Restores normal lazy loading behavior after a forced load.
+ */
+export function restoreVirtualization(): void {
+  window.dispatchEvent(new Event(RESTORE_VIRTUALIZATION_EVENT));
+}

Review Comment:
   **Suggestion:** The restore event is global and has no ownership or nesting 
protection. If two exports overlap, the first export to finish dispatches 
`RESTORE_VIRTUALIZATION_EVENT` while the second is still capturing, causing 
rows to reconnect their observers and potentially unmount off-screen charts 
during the second export. Coordinate exports with a shared lock or reference 
count so virtualization is restored only after the final active export 
completes. [race condition]
   
   <details>
   <summary><b>Severity Level:</b> Major ⚠️</summary>
   
   ```mdx
   - ❌ Concurrent exports can capture loading spinners.
   - ❌ Dashboard or chart export output may be incomplete.
   - ⚠️ Global row state is restored before all captures finish.
   ```
   </details>
   
   [![Fix in 
Cursor](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/fix-in-cursor-flat.svg)](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=0e8569b7b81d4872b5c0faab80bc85e0&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
 [![Fix in VSCode 
Claude](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/fix-in-vscode-claude-flat.svg)](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=0e8569b7b81d4872b5c0faab80bc85e0&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
   
   *(Use Cmd/Ctrl + Click for best experience)*
   <details>
   <summary><b>Prompt for AI Agent 🤖 </b></summary>
   
   ```mdx
   This is a comment left during a code review.
   
   **Path:** superset-frontend/src/utils/downloadUtils.ts
   **Line:** 82:84
   **Comment:**
        *Race Condition: The restore event is global and has no ownership or 
nesting protection. If two exports overlap, the first export to finish 
dispatches `RESTORE_VIRTUALIZATION_EVENT` while the second is still capturing, 
causing rows to reconnect their observers and potentially unmount off-screen 
charts during the second export. Coordinate exports with a shared lock or 
reference count so virtualization is restored only after the final active 
export completes.
   
   Validate the correctness of the flagged issue. If correct, How can I resolve 
this? If you propose a fix, implement it and please make it concise.
   Once fix is implemented, also check other comments on the same PR, and ask 
user if the user wants to fix the rest of the comments as well. if said yes, 
then fetch all the comments validate the correctness and implement a minimal fix
   ```
   </details>
   <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F42561&comment_hash=7a5f091b3151dfa882df2597f8f2f72141f7b273d19fdf535da012784d8f2284&reaction=like'>👍</a>
 | <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F42561&comment_hash=7a5f091b3151dfa882df2597f8f2f72141f7b273d19fdf535da012784d8f2284&reaction=dislike'>👎</a>



-- 
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]

Reply via email to