perf: reduce duplicate app initialization and heavy view imports

Deduplicates concurrent app initialization in the config store
Lazy-loads VS Code-only app surfaces
Avoids broad view barrel imports for better chunk isolation
This commit is contained in:
Bohdan Triapitsyn
2026-05-06 02:43:13 +03:00
parent 170d334921
commit dbc06dd6fa
7 changed files with 71 additions and 79 deletions
+41 -29
View File
@@ -583,6 +583,7 @@ declare global {
// In-flight dedup: prevent concurrent duplicate loadProviders/loadAgents calls for the same directory
const _inFlightProviders = new Map<string, Promise<void>>();
const _inFlightAgents = new Map<string, Promise<boolean>>();
let _initializeAppInFlight: Promise<void> | null = null;
export const useConfigStore = create<ConfigStore>()(
devtools(
@@ -1965,43 +1966,54 @@ export const useConfigStore = create<ConfigStore>()(
},
initializeApp: async () => {
try {
const debug = streamDebugEnabled();
if (debug) console.log("Starting app initialization...");
if (_initializeAppInFlight) {
return _initializeAppInFlight;
}
const isConnected = await get().checkConnection();
if (debug) console.log("Connection check result:", isConnected);
const run = (async () => {
try {
const debug = streamDebugEnabled();
if (debug) console.log("Starting app initialization...");
if (!isConnected) {
if (debug) console.log("Server not connected");
// checkConnection already set lastDisconnectReason; do not overwrite.
const isConnected = await get().checkConnection();
if (debug) console.log("Connection check result:", isConnected);
if (!isConnected) {
if (debug) console.log("Server not connected");
// checkConnection already set lastDisconnectReason; do not overwrite.
set({
isConnected: false,
connectionPhase: get().hasEverConnected ? "reconnecting" : "connecting",
});
return;
}
if (debug) console.log("Initializing app...");
await opencodeClient.initApp();
if (debug) console.log("Loading providers...");
await get().loadProviders();
if (debug) console.log("Loading agents...");
await get().loadAgents();
set({ isInitialized: true, isConnected: true, hasEverConnected: true, connectionPhase: "connected" });
if (debug) console.log("App initialized successfully");
} catch (error) {
console.error("Failed to initialize app:", error);
set({
isInitialized: false,
isConnected: false,
connectionPhase: get().hasEverConnected ? "reconnecting" : "connecting",
lastDisconnectReason: 'init_error',
});
return;
}
})().finally(() => {
_initializeAppInFlight = null;
});
if (debug) console.log("Initializing app...");
await opencodeClient.initApp();
if (debug) console.log("Loading providers...");
await get().loadProviders();
if (debug) console.log("Loading agents...");
await get().loadAgents();
set({ isInitialized: true, isConnected: true, hasEverConnected: true, connectionPhase: "connected" });
if (debug) console.log("App initialized successfully");
} catch (error) {
console.error("Failed to initialize app:", error);
set({
isInitialized: false,
isConnected: false,
connectionPhase: get().hasEverConnected ? "reconnecting" : "connecting",
lastDisconnectReason: 'init_error',
});
}
_initializeAppInFlight = run;
return run;
},
getCurrentProvider: () => {