@@ -413,43 +413,66 @@ async function buildSyncStatusMap(universes) {
413413 } , { } ) ;
414414}
415415
416+ let _pendingLoadPromise = null ;
417+ let _loadCounter = 0 ;
418+
416419async function loadBackendState ( ) {
417- console . log ( `[Perf] loadBackendState Start at ${ ( performance . now ( ) / 1000 ) . toFixed ( 3 ) } s` ) ;
418- console . time ( '[GF-DEBUG] loadBackendState' ) ;
419- const [ universes = [ ] , activeUniverse , gitDashboard ] = await Promise . all ( [
420- ( async ( ) => {
421- console . time ( '[GF-DEBUG] getAllUniverses' ) ;
422- const res = await universeBackendBridge . getAllUniverses ( ) ;
423- console . timeEnd ( '[GF-DEBUG] getAllUniverses' ) ;
424- return res ;
425- } ) ( ) ,
426- ( async ( ) => {
427- console . time ( '[GF-DEBUG] getActiveUniverse' ) ;
428- const res = await universeBackendBridge . getActiveUniverse ( ) ;
429- console . timeEnd ( '[GF-DEBUG] getActiveUniverse' ) ;
430- return res ;
431- } ) ( ) ,
432- ( async ( ) => {
433- console . time ( '[GF-DEBUG] getGitStatusDashboard' ) ;
434- const res = await universeBackendBridge . getGitStatusDashboard ?. ( ) ;
435- console . timeEnd ( '[GF-DEBUG] getGitStatusDashboard' ) ;
436- return res ;
437- } ) ( )
438- ] ) ;
439-
440- const syncStatusMap = await buildSyncStatusMap ( universes ) ;
441- const activeSlug = activeUniverse ?. slug || null ;
442-
443- const mapped = {
444- universes : universes . map ( universe => mapUniverse ( universe , activeSlug , syncStatusMap ) ) ,
445- activeUniverseSlug : activeSlug ,
446- activeUniverse : activeSlug ? universes . find ( u => u . slug === activeSlug ) : null ,
447- syncStatuses : syncStatusMap ,
448- gitDashboard : gitDashboard || null
449- } ;
450- console . timeEnd ( '[GF-DEBUG] loadBackendState' ) ;
451- console . log ( `[Perf] loadBackendState End at ${ ( performance . now ( ) / 1000 ) . toFixed ( 3 ) } s` ) ;
452- return mapped ;
420+ // Deduplicate requests: if a load is already in progress, reuse the existing promise
421+ if ( _pendingLoadPromise ) {
422+ // console.log('[Perf] Reusing in-flight loadBackendState promise');
423+ return _pendingLoadPromise ;
424+ }
425+
426+ const loadId = ++ _loadCounter ;
427+ const label = `[GF-DEBUG:${ loadId } ]` ;
428+
429+ _pendingLoadPromise = ( async ( ) => {
430+ // console.log(`[Perf] loadBackendState #${loadId} Start at ${(performance.now() / 1000).toFixed(3)}s`);
431+ console . time ( `${ label } loadBackendState` ) ;
432+
433+ try {
434+ const [ universes = [ ] , activeUniverse , gitDashboard ] = await Promise . all ( [
435+ ( async ( ) => {
436+ console . time ( `${ label } getAllUniverses` ) ;
437+ const res = await universeBackendBridge . getAllUniverses ( ) ;
438+ console . timeEnd ( `${ label } getAllUniverses` ) ;
439+ return res ;
440+ } ) ( ) ,
441+ ( async ( ) => {
442+ console . time ( `${ label } getActiveUniverse` ) ;
443+ const res = await universeBackendBridge . getActiveUniverse ( ) ;
444+ console . timeEnd ( `${ label } getActiveUniverse` ) ;
445+ return res ;
446+ } ) ( ) ,
447+ ( async ( ) => {
448+ if ( ! universeBackendBridge . getGitStatusDashboard ) return null ;
449+ console . time ( `${ label } getGitStatusDashboard` ) ;
450+ const res = await universeBackendBridge . getGitStatusDashboard ( ) ;
451+ console . timeEnd ( `${ label } getGitStatusDashboard` ) ;
452+ return res ;
453+ } ) ( )
454+ ] ) ;
455+
456+ const syncStatusMap = await buildSyncStatusMap ( universes ) ;
457+ const activeSlug = activeUniverse ?. slug || null ;
458+
459+ const mapped = {
460+ universes : universes . map ( universe => mapUniverse ( universe , activeSlug , syncStatusMap ) ) ,
461+ activeUniverseSlug : activeSlug ,
462+ activeUniverse : activeSlug ? universes . find ( u => u . slug === activeSlug ) : null ,
463+ syncStatuses : syncStatusMap ,
464+ gitDashboard : gitDashboard || null
465+ } ;
466+
467+ console . timeEnd ( `${ label } loadBackendState` ) ;
468+ // console.log(`[Perf] loadBackendState #${loadId} End at ${(performance.now() / 1000).toFixed(3)}s`);
469+ return mapped ;
470+ } finally {
471+ _pendingLoadPromise = null ;
472+ }
473+ } ) ( ) ;
474+
475+ return _pendingLoadPromise ;
453476}
454477
455478async function fetchAuthState ( ) {
0 commit comments