fix(startup): reduce initial loading and unblock app restoration
This commit is contained in:
23 files changed
+745
-123
No files matched your search
@@ -0,0 +1,58 @@
|
||||
// @vitest-environment happy-dom
|
||||
|
||||
import { createPinia, setActivePinia } from "pinia";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { useSettingsStore } from "../settingsStore";
|
||||
|
||||
const backend = vi.hoisted(() => ({
|
||||
loadAiConfigs: vi.fn(),
|
||||
loadAiChatSelection: vi.fn(),
|
||||
loadAiConfig: vi.fn(),
|
||||
loadAiProviderConfigs: vi.fn(),
|
||||
saveAiChatSelection: vi.fn(),
|
||||
}));
|
||||
vi.mock("@/lib/backend/api", () => backend);
|
||||
|
||||
beforeEach(() => {
|
||||
vi.resetAllMocks();
|
||||
localStorage.clear();
|
||||
setActivePinia(createPinia());
|
||||
backend.loadAiChatSelection.mockResolvedValue(null);
|
||||
backend.loadAiConfig.mockResolvedValue(null);
|
||||
backend.loadAiProviderConfigs.mockResolvedValue(null);
|
||||
});
|
||||
|
||||
describe("AI startup initialization", () => {
|
||||
it("shares pending initialization between startup and the AI panel", async () => {
|
||||
let resolveConfigs!: (configs: []) => void;
|
||||
backend.loadAiConfigs.mockImplementation(
|
||||
() =>
|
||||
new Promise<[]>((resolve) => {
|
||||
resolveConfigs = resolve;
|
||||
}),
|
||||
);
|
||||
const store = useSettingsStore();
|
||||
const first = store.initAiConfigs();
|
||||
const second = store.initAiConfigs();
|
||||
expect(backend.loadAiConfigs).toHaveBeenCalledTimes(1);
|
||||
expect(store.isAiConfigLoaded).toBe(false);
|
||||
resolveConfigs([]);
|
||||
await Promise.all([first, second]);
|
||||
expect(store.isAiConfigLoaded).toBe(true);
|
||||
expect(backend.loadAiChatSelection).toHaveBeenCalledTimes(1);
|
||||
await store.initAiConfigs();
|
||||
expect(backend.loadAiConfigs).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("permits retry after a shared initialization failure", async () => {
|
||||
backend.loadAiConfigs.mockRejectedValueOnce(new Error("unavailable")).mockResolvedValue([]);
|
||||
const store = useSettingsStore();
|
||||
const pending = [store.initAiConfigs(), store.initAiConfigs()];
|
||||
const results = await Promise.allSettled(pending);
|
||||
expect(results.every((result) => result.status === "rejected")).toBe(true);
|
||||
expect(store.isAiConfigLoaded).toBe(false);
|
||||
await store.initAiConfigs();
|
||||
expect(backend.loadAiConfigs).toHaveBeenCalledTimes(2);
|
||||
expect(store.isAiConfigLoaded).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -25,7 +25,7 @@ import { REDIS_DATABASE_DISPLAY_LIMIT_DEFAULT, REDIS_DATABASE_DISPLAY_LIMIT_MIN,
|
||||
import { normalizeSidebarHiddenTablePrefixes } from "@/lib/sidebar/sidebarTableNameDisplay";
|
||||
import { normalizeSidebarCopyTableNameSeparator } from "@/lib/sidebar/sidebarTableNameCopy";
|
||||
import type { SidebarActivation } from "@/lib/sidebar/treeNodeClick";
|
||||
import { DEFAULT_SQL_SNIPPETS } from "@/lib/sql/sqlCompletion";
|
||||
import { DEFAULT_SQL_SNIPPETS } from "@/lib/sql/sqlSnippetTemplates";
|
||||
import { DEFAULT_SQL_FORMATTER_SETTINGS, normalizeSqlFormatterSettings, type SqlFormatterSettings } from "@/lib/sql/sqlFormatterConfig";
|
||||
import { canonicalSqlShortcutSql, DEFAULT_SQL_SHORTCUTS, deriveSqlShortcutDatabaseTypes, mergeDefaultSqlShortcuts, normalizeSqlShortcutDatabaseTypes, normalizeSqlShortcutKind, normalizeSqlShortcutLimit, normalizeSqlShortcutSqlByDatabaseType } from "@/lib/sql/sqlShortcutActions";
|
||||
import { normalizeSqlVariableSyntaxOverrides, type SqlVariableSyntaxOverrides } from "@/lib/sql/sqlVariableSyntax";
|
||||
@@ -1963,6 +1963,7 @@ export const useSettingsStore = defineStore("settings", () => {
|
||||
const isMcpGlobalPolicyLoaded = ref(false);
|
||||
const isEditorSettingsLoaded = ref(false);
|
||||
let initEditorSettingsPromise: Promise<void> | null = null;
|
||||
let initAiConfigsPromise: Promise<void> | null = null;
|
||||
let pendingEditorSettingsPatches: Partial<EditorSettings>[] = [];
|
||||
let editorSettingsOperationQueue: Promise<void> | null = null;
|
||||
let editorSettingsPatchRevision = 0;
|
||||
@@ -2168,7 +2169,15 @@ export const useSettingsStore = defineStore("settings", () => {
|
||||
|
||||
async function initAiConfigs(): Promise<void> {
|
||||
if (isAiConfigLoaded.value) return;
|
||||
if (!initAiConfigsPromise) {
|
||||
initAiConfigsPromise = loadAiConfigurationState().finally(() => {
|
||||
initAiConfigsPromise = null;
|
||||
});
|
||||
}
|
||||
await initAiConfigsPromise;
|
||||
}
|
||||
|
||||
async function loadAiConfigurationState(): Promise<void> {
|
||||
// 尝试加载新格式
|
||||
const newConfigs = await api.loadAiConfigs();
|
||||
|
||||
|
||||
Reference in new issue
Block a user