77 isBoardWidgetBridgeRequest ,
88} from "./widget-bridge.ts" ;
99
10- const SANDBOX_READY_TIMEOUT_MS = 10_000 ;
10+ const WIDGET_LOAD_TIMEOUT_MS = 10_000 ;
1111
1212type BoardWidgetSandboxHostOptions = {
1313 frame : HTMLIFrameElement ;
@@ -39,8 +39,11 @@ export class BoardWidgetSandboxHost {
3939 private ready = false ;
4040 private readyTimer : number | null = null ;
4141 private loadedDocumentKey = "" ;
42- private loadingDocumentKey = "" ;
43- private loadGeneration = 0 ;
42+ private activeDocumentLoad : {
43+ controller : AbortController ;
44+ key : string ;
45+ timeout : number ;
46+ } | null = null ;
4447 private requestGeneration = 0 ;
4548 private readonly pendingRequests = new Map < string , number > ( ) ;
4649
@@ -60,8 +63,7 @@ export class BoardWidgetSandboxHost {
6063 this . active = active ;
6164 if ( ! active ) {
6265 this . clearReadyTimeout ( ) ;
63- this . loadGeneration += 1 ;
64- this . loadingDocumentKey = "" ;
66+ this . cancelDocumentLoad ( ) ;
6567 this . cancelPendingRequests ( "Widget inactive" ) ;
6668 this . requestGeneration += 1 ;
6769 return ;
@@ -116,11 +118,10 @@ export class BoardWidgetSandboxHost {
116118 }
117119
118120 reset ( ) : void {
119- this . loadGeneration += 1 ;
121+ this . cancelDocumentLoad ( ) ;
120122 this . requestGeneration += 1 ;
121123 this . pendingRequests . clear ( ) ;
122124 this . loadedDocumentKey = "" ;
123- this . loadingDocumentKey = "" ;
124125 this . bridgePort ?. close ( ) ;
125126 this . bridgePort = null ;
126127 this . adoptedTicket = "" ;
@@ -292,6 +293,18 @@ export class BoardWidgetSandboxHost {
292293 }
293294 }
294295
296+ private cancelDocumentLoad ( ) : void {
297+ const load = this . activeDocumentLoad ;
298+ // Clear ownership before aborting so the rejection is stale and cannot
299+ // spend the retry budget or clear a replacement load.
300+ this . activeDocumentLoad = null ;
301+ if ( ! load ) {
302+ return ;
303+ }
304+ window . clearTimeout ( load . timeout ) ;
305+ load . controller . abort ( ) ;
306+ }
307+
295308 private scheduleReadyTimeout ( ) : void {
296309 if ( ! this . active || this . ready || this . readyTimer !== null ) {
297310 return ;
@@ -304,7 +317,7 @@ export class BoardWidgetSandboxHost {
304317 // Browsers do not expose iframe HTTP failures through `error`. Bound the
305318 // proxy handshake so an unavailable adjacent listener cannot stay blank.
306319 this . retrySandboxFrame ( ) ;
307- } , SANDBOX_READY_TIMEOUT_MS ) ;
320+ } , WIDGET_LOAD_TIMEOUT_MS ) ;
308321 }
309322
310323 private retrySandboxFrame ( ) : void {
@@ -379,16 +392,25 @@ export class BoardWidgetSandboxHost {
379392 return ;
380393 }
381394 const documentKey = this . documentKey ( ) ;
382- if ( documentKey === this . loadedDocumentKey || documentKey === this . loadingDocumentKey ) {
395+ if ( documentKey === this . loadedDocumentKey || documentKey === this . activeDocumentLoad ?. key ) {
383396 return ;
384397 }
385- this . loadingDocumentKey = documentKey ;
398+ this . cancelDocumentLoad ( ) ;
386399 const sourceHref = sourceUrl . href ;
387400 this . options . onFrameUrl ( sourceHref ) ;
388- const generation = ++ this . loadGeneration ;
401+ const controller = new AbortController ( ) ;
402+ const load = {
403+ controller,
404+ key : documentKey ,
405+ timeout : window . setTimeout (
406+ ( ) => controller . abort ( new DOMException ( "The operation timed out." , "TimeoutError" ) ) ,
407+ WIDGET_LOAD_TIMEOUT_MS ,
408+ ) ,
409+ } ;
410+ this . activeDocumentLoad = load ;
389411 try {
390- const response = await fetch ( sourceHref , { cache : "no-store" } ) ;
391- if ( ! this . active || generation !== this . loadGeneration || ! frame . isConnected ) {
412+ const response = await fetch ( sourceHref , { cache : "no-store" , signal : controller . signal } ) ;
413+ if ( ! this . active || this . activeDocumentLoad !== load || ! frame . isConnected ) {
392414 return ;
393415 }
394416 if ( response . status === 401 ) {
@@ -399,7 +421,7 @@ export class BoardWidgetSandboxHost {
399421 throw new Error ( `widget content request failed (${ response . status } )` ) ;
400422 }
401423 const documentHtml = await response . text ( ) ;
402- if ( ! this . active || generation !== this . loadGeneration || ! frame . isConnected ) {
424+ if ( ! this . active || this . activeDocumentLoad !== load || ! frame . isConnected ) {
403425 return ;
404426 }
405427 frame . contentWindow ?. postMessage (
@@ -416,12 +438,13 @@ export class BoardWidgetSandboxHost {
416438 // pending. Complete the handshake once these exact bytes become current.
417439 this . postHostInit ( ) ;
418440 } catch {
419- if ( generation === this . loadGeneration ) {
441+ if ( this . activeDocumentLoad === load ) {
420442 this . options . onLoadFailed ( widget ) ;
421443 }
422444 } finally {
423- if ( generation === this . loadGeneration ) {
424- this . loadingDocumentKey = "" ;
445+ window . clearTimeout ( load . timeout ) ;
446+ if ( this . activeDocumentLoad === load ) {
447+ this . activeDocumentLoad = null ;
425448 }
426449 }
427450 }
0 commit comments