fix(sql-library): load saved files earlier at startup

This commit is contained in:
t8y2 committed 2026-09-25 14:06:37 +08:00
1 parent cb16b0f8be
commit b99c60aff6
5 files changed
+63 -16

No files matched your search

+9 -9
View File
@@ -3870,6 +3870,15 @@ function onLoginSuccess() {
async function initApp() { async function initApp() {
const t0 = performance.now(); const t0 = performance.now();
console.log("[STARTUP] initApp begin"); console.log("[STARTUP] initApp begin");
void Promise.all([initSavedSqlEditorPositions(), savedSqlStore.initFromStorage()])
.then(() => {
console.log(`[STARTUP] savedSqlStore.initFromStorage: ${(performance.now() - t0).toFixed(0)}ms`);
void queryStore.hydrateSavedSqlTabs();
})
.catch((e: any) => {
toast(t("connection.loadFailed", { message: e?.message || String(e) }), 5000);
});
const restoreOpenTabs = async () => { const restoreOpenTabs = async () => {
await settingsStore.initEditorSettings(); await settingsStore.initEditorSettings();
markStartupPhase("settings-ready"); markStartupPhase("settings-ready");
@@ -3916,15 +3925,6 @@ async function initApp() {
void promptTemplateStore.init(); void promptTemplateStore.init();
void Promise.all([initSavedSqlEditorPositions(), savedSqlStore.initFromStorage()])
.then(() => {
console.log(`[STARTUP] savedSqlStore.initFromStorage: ${(performance.now() - t0).toFixed(0)}ms`);
void queryStore.hydrateSavedSqlTabs();
})
.catch((e: any) => {
toast(t("connection.loadFailed", { message: e?.message || String(e) }), 5000);
});
restoreActiveConnectionContext(); restoreActiveConnectionContext();
} catch (e: any) { } catch (e: any) {
toast(t("connection.loadFailed", { message: e?.message || String(e) }), 5000); toast(t("connection.loadFailed", { message: e?.message || String(e) }), 5000);
@@ -2,7 +2,7 @@
import { computed, nextTick, onBeforeUnmount, reactive, ref, watch } from "vue"; import { computed, nextTick, onBeforeUnmount, reactive, ref, watch } from "vue";
import type { CSSProperties } from "vue"; import type { CSSProperties } from "vue";
import { useI18n } from "vue-i18n"; import { useI18n } from "vue-i18n";
import { ArrowDownWideNarrow, ChevronsDownUp, Download, FilePlus, FileText, FolderCog, FolderClosed, FolderOpen, FolderPlus, Library, LocateFixed, Pencil, Play, Search, Trash2, Upload, X } from "@lucide/vue"; import { ArrowDownWideNarrow, ChevronsDownUp, Download, FilePlus, FileText, FolderCog, FolderClosed, FolderOpen, FolderPlus, Library, Loader2, LocateFixed, Pencil, Play, Search, Trash2, Upload, X } from "@lucide/vue";
import { Button } from "@/components/ui/button"; import { Button } from "@/components/ui/button";
import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from "@/components/ui/dialog"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from "@/components/ui/dialog";
import CustomContextMenu, { type ContextMenuItem as CtxMenuItem } from "@/components/ui/CustomContextMenu.vue"; import CustomContextMenu, { type ContextMenuItem as CtxMenuItem } from "@/components/ui/CustomContextMenu.vue";
@@ -1446,7 +1446,12 @@ function showDropInside(targetId: string) {
</div> </div>
<!-- End tree structure --> <!-- End tree structure -->
<div v-if="!hasAnyVisibleItem" class="flex h-full flex-col items-center justify-center gap-2 text-muted-foreground"> <div v-if="savedSqlStore.loadState === 'idle' || savedSqlStore.loadState === 'loading'" class="flex h-full flex-col items-center justify-center gap-2 text-muted-foreground" role="status" aria-live="polite">
<Loader2 class="h-6 w-6 animate-spin motion-reduce:animate-none" aria-hidden="true" />
<p class="text-[13px]">{{ t("common.loading") }}</p>
</div>
<div v-else-if="!hasAnyVisibleItem" class="flex h-full flex-col items-center justify-center gap-2 text-muted-foreground">
<Library class="h-8 w-8 opacity-30" /> <Library class="h-8 w-8 opacity-30" />
<p class="text-[13px]">{{ t("sqlLibrary.empty") }}</p> <p class="text-[13px]">{{ t("sqlLibrary.empty") }}</p>
</div> </div>
+12 -3
View File
@@ -148,6 +148,7 @@ export const useSavedSqlStore = defineStore("savedSql", () => {
const folders = ref<SavedSqlFolder[]>([]); const folders = ref<SavedSqlFolder[]>([]);
const files = ref<SavedSqlFile[]>([]); const files = ref<SavedSqlFile[]>([]);
const isLoaded = ref(false); const isLoaded = ref(false);
const loadState = ref<"idle" | "loading" | "loaded" | "failed">("idle");
let pendingSync: Promise<void> | null = null; let pendingSync: Promise<void> | null = null;
let initFromStoragePromise: Promise<void> | null = null; let initFromStoragePromise: Promise<void> | null = null;
const pendingFolderCreates = new Map<string, Promise<SavedSqlFolder>>(); const pendingFolderCreates = new Map<string, Promise<SavedSqlFolder>>();
@@ -186,13 +187,20 @@ export const useSavedSqlStore = defineStore("savedSql", () => {
async function initFromStorage() { async function initFromStorage() {
if (isLoaded.value) return; if (isLoaded.value) return;
if (!initFromStoragePromise) { if (!initFromStoragePromise) {
loadState.value = "loading";
initFromStoragePromise = (async () => { initFromStoragePromise = (async () => {
await migrateLegacyLocalStorage(); await migrateLegacyLocalStorage();
applyLibrary(await api.loadSavedSqlLibrary()); applyLibrary(await api.loadSavedSqlLibrary());
isLoaded.value = true; isLoaded.value = true;
})().finally(() => { loadState.value = "loaded";
initFromStoragePromise = null; })()
}); .catch((error) => {
loadState.value = "failed";
throw error;
})
.finally(() => {
initFromStoragePromise = null;
});
} }
await initFromStoragePromise; await initFromStoragePromise;
} }
@@ -821,6 +829,7 @@ export const useSavedSqlStore = defineStore("savedSql", () => {
folders, folders,
files, files,
isLoaded, isLoaded,
loadState,
version, version,
treeVersion, treeVersion,
initFromStorage, initFromStorage,
+3 -2
View File
@@ -61,14 +61,15 @@ test("web startup uses the guarded initializer behind the existing authenticatio
assert.ok(initAppStart >= 0 && initAppEnd > initAppStart); assert.ok(initAppStart >= 0 && initAppEnd > initAppStart);
const initAppSource = appSource.slice(initAppStart, initAppEnd); const initAppSource = appSource.slice(initAppStart, initAppEnd);
const savedSqlInitialization = initAppSource.indexOf("savedSqlStore.initFromStorage()");
const webInitialization = initAppSource.indexOf("await initializeOpenTabs({"); const webInitialization = initAppSource.indexOf("await initializeOpenTabs({");
const optionalInitialization = initAppSource.indexOf("initializeOptionalState: () => settingsStore.initAiConfigs()", webInitialization); const optionalInitialization = initAppSource.indexOf("initializeOptionalState: () => settingsStore.initAiConfigs()", webInitialization);
const requiredRestoration = initAppSource.indexOf("restoreOpenTabs,", optionalInitialization); const requiredRestoration = initAppSource.indexOf("restoreOpenTabs,", optionalInitialization);
const savedSqlInitialization = initAppSource.indexOf("savedSqlStore.initFromStorage()", requiredRestoration); assert.ok(savedSqlInitialization >= 0);
assert.ok(webInitialization >= 0); assert.ok(webInitialization >= 0);
assert.ok(savedSqlInitialization < webInitialization);
assert.ok(optionalInitialization > webInitialization); assert.ok(optionalInitialization > webInitialization);
assert.ok(requiredRestoration > optionalInitialization); assert.ok(requiredRestoration > optionalInitialization);
assert.ok(savedSqlInitialization > requiredRestoration);
assert.equal(initAppSource.includes("if (!desktopOpenTabsRestorationBarrier) await settingsStore.initAiConfigs()"), false); assert.equal(initAppSource.includes("if (!desktopOpenTabsRestorationBarrier) await settingsStore.initAiConfigs()"), false);
const mountedStart = appSource.indexOf("onMounted(async () =>"); const mountedStart = appSource.indexOf("onMounted(async () =>");
+32
View File
@@ -28,6 +28,38 @@ beforeEach(() => {
vi.clearAllMocks(); vi.clearAllMocks();
}); });
test("tracks saved SQL library loading and supports retry after failure", async () => {
let resolveLoad: ((library: SavedSqlLibrary) => void) | undefined;
apiMock.loadSavedSqlLibrary.mockImplementationOnce(
() =>
new Promise<SavedSqlLibrary>((resolve) => {
resolveLoad = resolve;
}),
);
const store = useSavedSqlStore();
assert.equal(store.loadState, "idle");
const loading = store.initFromStorage();
assert.equal(store.loadState, "loading");
await Promise.resolve();
resolveLoad?.({ folders: [], files: [] });
await loading;
assert.equal(store.loadState, "loaded");
assert.equal(store.isLoaded, true);
setActivePinia(createPinia());
const retryingStore = useSavedSqlStore();
apiMock.loadSavedSqlLibrary.mockRejectedValueOnce(new Error("storage unavailable"));
await assert.rejects(retryingStore.initFromStorage(), /storage unavailable/);
assert.equal(retryingStore.loadState, "failed");
apiMock.loadSavedSqlLibrary.mockResolvedValueOnce({ folders: [], files: [] });
await retryingStore.initFromStorage();
assert.equal(retryingStore.loadState, "loaded");
});
test("concurrent saved SQL folder creates reuse the same pending folder", async () => { test("concurrent saved SQL folder creates reuse the same pending folder", async () => {
let resolveSave: ((folder: SavedSqlFolder) => void) | undefined; let resolveSave: ((folder: SavedSqlFolder) => void) | undefined;
apiMock.saveSavedSqlFolder.mockImplementation( apiMock.saveSavedSqlFolder.mockImplementation(