Skip to content

Commit 92a728b

Browse files
committed
feat: Deduplicate loadBackendState requests and enhance error handling in backend services.
1 parent b0973b2 commit 92a728b

2 files changed

Lines changed: 66 additions & 37 deletions

File tree

src/services/gitFederationService.js

Lines changed: 59 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -413,43 +413,66 @@ async function buildSyncStatusMap(universes) {
413413
}, {});
414414
}
415415

416+
let _pendingLoadPromise = null;
417+
let _loadCounter = 0;
418+
416419
async 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

455478
async function fetchAuthState() {

src/services/universeBackendBridge.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ class UniverseBackendBridge {
128128

129129
flushQueuedCommandsWithError(error) {
130130
console.warn('[UniverseBackendBridge] Flushing queued commands with error:', error.message);
131+
this.isBackendReady = false; // Reset backend readiness on critical error
131132
while (this.commandQueue.length > 0) {
132133
const queuedCommand = this.commandQueue.shift();
133134
queuedCommand.reject(error);
@@ -254,7 +255,12 @@ class UniverseBackendBridge {
254255
}
255256

256257
// Execute command immediately if backend is ready
257-
await this.executeCommand(commandData);
258+
try {
259+
await this.executeCommand(commandData);
260+
} catch (error) {
261+
// If execution fails, reject immediately
262+
reject(error);
263+
}
258264
});
259265
}
260266

0 commit comments

Comments
 (0)