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:
@@ -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: () => {
|
||||
|
||||
Reference in New Issue
Block a user