msyavuz commented on code in PR #35143:
URL: https://github.com/apache/superset/pull/35143#discussion_r2349772572


##########
superset-embedded-sdk/src/index.ts:
##########
@@ -226,15 +229,51 @@ export async function embedDashboard({
   log('sent guest token');
 
   async function refreshGuestToken() {
-    const newGuestToken = await fetchGuestToken();
-    ourPort.emit('guestToken', { guestToken: newGuestToken });
-    setTimeout(refreshGuestToken, getGuestTokenRefreshTiming(newGuestToken));
+    // Check if unmounted before proceeding
+    if (isUnmounted) {
+      log("skipping token refresh - component unmounted");
+      return;
+    }
+
+    try {
+      const newGuestToken = await fetchGuestToken();
+      
+      // Check again after async operation
+      if (isUnmounted) {
+        log("skipping token emission - component unmounted during fetch");
+        return;
+      }
+
+      ourPort.emit("guestToken", { guestToken: newGuestToken });
+      
+      // Schedule next refresh only if not unmounted
+      if (!isUnmounted) {
+        refreshTimerId = setTimeout(refreshGuestToken, 
getGuestTokenRefreshTiming(newGuestToken));
+      }
+    } catch (error) {
+      log("error refreshing guest token:", error);
+      // Still schedule retry if not unmounted (you might want to add 
exponential backoff here)
+      if (!isUnmounted) {
+        refreshTimerId = setTimeout(refreshGuestToken, 30000); // retry in 30 
seconds

Review Comment:
   ```suggestion
           refreshTimerId = setTimeout(refreshGuestToken, 
getGuestTokenRefreshTiming(newGuestToken));
   ```



##########
superset-embedded-sdk/src/index.ts:
##########
@@ -226,15 +229,51 @@ export async function embedDashboard({
   log('sent guest token');
 
   async function refreshGuestToken() {
-    const newGuestToken = await fetchGuestToken();
-    ourPort.emit('guestToken', { guestToken: newGuestToken });
-    setTimeout(refreshGuestToken, getGuestTokenRefreshTiming(newGuestToken));
+    // Check if unmounted before proceeding
+    if (isUnmounted) {
+      log("skipping token refresh - component unmounted");
+      return;
+    }
+
+    try {
+      const newGuestToken = await fetchGuestToken();
+      
+      // Check again after async operation
+      if (isUnmounted) {
+        log("skipping token emission - component unmounted during fetch");
+        return;
+      }
+
+      ourPort.emit("guestToken", { guestToken: newGuestToken });
+      
+      // Schedule next refresh only if not unmounted
+      if (!isUnmounted) {
+        refreshTimerId = setTimeout(refreshGuestToken, 
getGuestTokenRefreshTiming(newGuestToken));
+      }
+    } catch (error) {
+      log("error refreshing guest token:", error);
+      // Still schedule retry if not unmounted (you might want to add 
exponential backoff here)

Review Comment:
   ```suggestion
         // Still schedule retry if not unmounted
   ```



##########
superset-embedded-sdk/src/index.ts:
##########
@@ -107,6 +107,9 @@ export async function embedDashboard({
   iframeSandboxExtras = [],
   referrerPolicy,
 }: EmbedDashboardParams): Promise<EmbeddedDashboard> {
+  let refreshTimerId: NodeJS.Timeout | null = null;
+  let isUnmounted = false;

Review Comment:
   This unmounted and following checks seem unnneccessary to me. When the 
component using this gets unmounted this already won't run again. Am i missing 
something?



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