perf: overhaul session loading, caching, and runtime isolation (#2360)
Improve OpenChamber responsiveness under large session workloads while fixing cache, synchronization, and persistence correctness across runtimes, projects, directories, and worktrees. - prioritize selected and visible sessions during bootstrap and defer non-critical enrichment work - reduce redundant message loading, event processing, store publication, and hidden sidebar work - prevent stale session and message requests from overwriting newer authoritative state - preserve existing data when authoritative fetches fail instead of treating failures as successful empty responses - scope session materialization, messages, drafts, queues, todos, pins, permissions, folders, tabs, Git state, and pull request data by runtime and directory identity - harden runtime switching, reconnect, cleanup, mutation reconciliation, and persisted-state ordering - preserve live subagent Task linkage when metadata arrives after an older message request or while streaming parts are suspended - coalesce overlapping tail refreshes without losing newer refresh demand - improve cold-session loading by moving deferrable work out of the critical bootstrap path - isolate URL authentication, mobile credentials, native secrets, and other runtime-owned state across endpoint changes - bound long-lived caches and remove avoidable allocations from event and rendering hot paths - limit virtualization to archive collections where it improves rendering without disrupting active sidebar layout - stabilize session folders, pin ordering, expanded state, and persisted sidebar behavior - open skill files through the same secure editor and outside-workspace grant flow used by file navigation, including worktree sessions - expand regression coverage for stale completions, runtime collisions, reconnect behavior, persistence races, authoritative empty results, and subagent refresh ordering - document the updated synchronization, cache ownership, performance, and runtime-isolation invariants
This commit is contained in:
committed by
GitHub
parent
485efc7117
commit
85400459e9
@@ -1,8 +1,9 @@
|
||||
import { create } from 'zustand';
|
||||
import { devtools } from 'zustand/middleware';
|
||||
import { getDeferredSafeStorage } from './utils/safeStorage';
|
||||
import { getDeferredSafeStorage, getSafeStorage } from './utils/safeStorage';
|
||||
import { isVSCodeRuntime } from '@/lib/desktop';
|
||||
import { runtimeFetch } from '@/lib/runtime-fetch';
|
||||
import { getRuntimeKey } from '@/lib/runtime-switch';
|
||||
|
||||
// --- Types ---
|
||||
|
||||
@@ -30,10 +31,11 @@ interface SessionFoldersActions {
|
||||
addSessionToFolder: (scopeKey: string, folderId: string, sessionId: string) => void;
|
||||
addSessionsToFolder: (scopeKey: string, folderId: string, sessionIds: string[]) => void;
|
||||
removeSessionFromFolder: (scopeKey: string, sessionId: string) => void;
|
||||
removeSessionEverywhere: (runtimeKey: string, sessionId: string) => void;
|
||||
removeSessionsFromFolders: (scopeKey: string, sessionIds: string[]) => void;
|
||||
toggleFolderCollapse: (folderId: string) => void;
|
||||
cleanupSessions: (scopeKey: string, existingSessionIds: Set<string>) => void;
|
||||
getSessionFolderId: (scopeKey: string, sessionId: string) => string | null;
|
||||
resetForRuntimeSwitch: (runtimeKey: string) => void;
|
||||
}
|
||||
|
||||
type SessionFoldersStore = SessionFoldersState & SessionFoldersActions;
|
||||
@@ -42,11 +44,12 @@ type SessionFoldersStore = SessionFoldersState & SessionFoldersActions;
|
||||
|
||||
const FOLDERS_STORAGE_KEY = 'oc.sessions.folders';
|
||||
const COLLAPSED_STORAGE_KEY = 'oc.sessions.folderCollapse';
|
||||
const STORAGE_INDEX_KEY = 'oc.sessions.folders.v2.index';
|
||||
const SESSION_FOLDERS_API_PATH = '/api/session-folders';
|
||||
const DISK_WRITE_DEBOUNCE_MS = 250;
|
||||
const ARCHIVED_SCOPE_PREFIX = '__archived__:';
|
||||
|
||||
const safeStorage = getDeferredSafeStorage();
|
||||
const immediateSafeStorage = getSafeStorage();
|
||||
let diskWriteTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
let diskHydrated = false;
|
||||
let diskHydrationInFlight = false;
|
||||
@@ -54,6 +57,54 @@ let persistFoldersTimer: ReturnType<typeof setTimeout> | undefined;
|
||||
let persistCollapsedTimer: ReturnType<typeof setTimeout> | undefined;
|
||||
let pendingFoldersMap: SessionFoldersMap | null = null;
|
||||
let pendingCollapsedIds: Set<string> | null = null;
|
||||
let pendingBrowserRuntimeKey: string | null = null;
|
||||
let activeFolderRuntimeKey = getRuntimeKey();
|
||||
let folderRuntimeGeneration = 0;
|
||||
let folderMutationRevision = 0;
|
||||
const lastDiskUpdatedAtByRuntime = new Map<string, number>();
|
||||
|
||||
type FolderStorageIndex = {
|
||||
version: 2;
|
||||
legacyClaimed: boolean;
|
||||
runtimes: Array<{ runtimeKey: string; updatedAt: number }>;
|
||||
};
|
||||
|
||||
const runtimeStorageKey = (base: string, runtimeKey: string) => `${base}.v2:${encodeURIComponent(runtimeKey)}`;
|
||||
const readStorageIndex = (): FolderStorageIndex => {
|
||||
try {
|
||||
const parsed = JSON.parse(safeStorage.getItem(STORAGE_INDEX_KEY) ?? '') as Partial<FolderStorageIndex>;
|
||||
return parsed.version === 2 && Array.isArray(parsed.runtimes)
|
||||
? { version: 2, legacyClaimed: Boolean(parsed.legacyClaimed), runtimes: parsed.runtimes }
|
||||
: { version: 2, legacyClaimed: false, runtimes: [] };
|
||||
} catch {
|
||||
return { version: 2, legacyClaimed: false, runtimes: [] };
|
||||
}
|
||||
};
|
||||
|
||||
const touchRuntimeStorage = (runtimeKey: string, updatedAt = Date.now(), targetStorage: Storage = safeStorage): void => {
|
||||
const index = readStorageIndex();
|
||||
const runtimes = [
|
||||
{ runtimeKey, updatedAt },
|
||||
...index.runtimes.filter((entry) => entry.runtimeKey !== runtimeKey),
|
||||
];
|
||||
targetStorage.setItem(STORAGE_INDEX_KEY, JSON.stringify({ version: 2, legacyClaimed: index.legacyClaimed, runtimes }));
|
||||
};
|
||||
|
||||
const claimLegacyStorage = (runtimeKey: string): void => {
|
||||
const index = readStorageIndex();
|
||||
if (index.legacyClaimed) return;
|
||||
const legacyFolders = safeStorage.getItem(FOLDERS_STORAGE_KEY);
|
||||
const legacyCollapsed = safeStorage.getItem(COLLAPSED_STORAGE_KEY);
|
||||
if (legacyFolders) safeStorage.setItem(runtimeStorageKey(FOLDERS_STORAGE_KEY, runtimeKey), legacyFolders);
|
||||
if (legacyCollapsed) safeStorage.setItem(runtimeStorageKey(COLLAPSED_STORAGE_KEY, runtimeKey), legacyCollapsed);
|
||||
const next = { ...index, legacyClaimed: true };
|
||||
safeStorage.setItem(STORAGE_INDEX_KEY, JSON.stringify(next));
|
||||
if (safeStorage.getItem(STORAGE_INDEX_KEY) === JSON.stringify(next)) {
|
||||
safeStorage.removeItem(FOLDERS_STORAGE_KEY);
|
||||
safeStorage.removeItem(COLLAPSED_STORAGE_KEY);
|
||||
}
|
||||
touchRuntimeStorage(runtimeKey, 0);
|
||||
};
|
||||
|
||||
const isVSCodeWebview = (): boolean => {
|
||||
if (typeof window === 'undefined') {
|
||||
@@ -82,14 +133,19 @@ const schedulePersistToDisk = (foldersMap: SessionFoldersMap, collapsedFolderIds
|
||||
|
||||
const foldersSnapshot = JSON.parse(JSON.stringify(foldersMap)) as SessionFoldersMap;
|
||||
const collapsedSnapshot = Array.from(collapsedFolderIds);
|
||||
const runtimeKey = activeFolderRuntimeKey;
|
||||
const generation = folderRuntimeGeneration;
|
||||
|
||||
diskWriteTimer = setTimeout(() => {
|
||||
diskWriteTimer = null;
|
||||
if (runtimeKey !== getRuntimeKey() || generation !== folderRuntimeGeneration) return;
|
||||
const updatedAt = Math.max(Date.now(), (lastDiskUpdatedAtByRuntime.get(runtimeKey) ?? 0) + 1);
|
||||
lastDiskUpdatedAtByRuntime.set(runtimeKey, updatedAt);
|
||||
const payload = {
|
||||
version: 1,
|
||||
foldersMap: foldersSnapshot,
|
||||
collapsedFolderIds: collapsedSnapshot,
|
||||
updatedAt: Date.now(),
|
||||
updatedAt,
|
||||
};
|
||||
void runtimeFetch(SESSION_FOLDERS_API_PATH, {
|
||||
method: 'POST',
|
||||
@@ -99,9 +155,10 @@ const schedulePersistToDisk = (foldersMap: SessionFoldersMap, collapsedFolderIds
|
||||
}, DISK_WRITE_DEBOUNCE_MS);
|
||||
};
|
||||
|
||||
const readPersistedFolders = (): SessionFoldersMap => {
|
||||
const readPersistedFolders = (runtimeKey = activeFolderRuntimeKey): SessionFoldersMap => {
|
||||
try {
|
||||
const raw = safeStorage.getItem(FOLDERS_STORAGE_KEY);
|
||||
claimLegacyStorage(runtimeKey);
|
||||
const raw = safeStorage.getItem(runtimeStorageKey(FOLDERS_STORAGE_KEY, runtimeKey));
|
||||
if (!raw) {
|
||||
return {};
|
||||
}
|
||||
@@ -138,9 +195,10 @@ const readPersistedFolders = (): SessionFoldersMap => {
|
||||
}
|
||||
};
|
||||
|
||||
const readPersistedCollapsed = (): Set<string> => {
|
||||
const readPersistedCollapsed = (runtimeKey = activeFolderRuntimeKey): Set<string> => {
|
||||
try {
|
||||
const raw = safeStorage.getItem(COLLAPSED_STORAGE_KEY);
|
||||
claimLegacyStorage(runtimeKey);
|
||||
const raw = safeStorage.getItem(runtimeStorageKey(COLLAPSED_STORAGE_KEY, runtimeKey));
|
||||
if (!raw) {
|
||||
return new Set();
|
||||
}
|
||||
@@ -156,10 +214,13 @@ const readPersistedCollapsed = (): Set<string> => {
|
||||
|
||||
const persistFolders = (foldersMap: SessionFoldersMap): void => {
|
||||
pendingFoldersMap = foldersMap;
|
||||
pendingBrowserRuntimeKey = activeFolderRuntimeKey;
|
||||
clearTimeout(persistFoldersTimer);
|
||||
persistFoldersTimer = setTimeout(() => {
|
||||
try {
|
||||
safeStorage.setItem(FOLDERS_STORAGE_KEY, JSON.stringify(foldersMap));
|
||||
const runtimeKey = pendingBrowserRuntimeKey ?? activeFolderRuntimeKey;
|
||||
safeStorage.setItem(runtimeStorageKey(FOLDERS_STORAGE_KEY, runtimeKey), JSON.stringify(foldersMap));
|
||||
touchRuntimeStorage(runtimeKey);
|
||||
pendingFoldersMap = null;
|
||||
} catch {
|
||||
// ignored
|
||||
@@ -169,10 +230,13 @@ const persistFolders = (foldersMap: SessionFoldersMap): void => {
|
||||
|
||||
const persistCollapsed = (collapsedFolderIds: Set<string>): void => {
|
||||
pendingCollapsedIds = collapsedFolderIds;
|
||||
pendingBrowserRuntimeKey = activeFolderRuntimeKey;
|
||||
clearTimeout(persistCollapsedTimer);
|
||||
persistCollapsedTimer = setTimeout(() => {
|
||||
try {
|
||||
safeStorage.setItem(COLLAPSED_STORAGE_KEY, JSON.stringify(Array.from(collapsedFolderIds)));
|
||||
const runtimeKey = pendingBrowserRuntimeKey ?? activeFolderRuntimeKey;
|
||||
safeStorage.setItem(runtimeStorageKey(COLLAPSED_STORAGE_KEY, runtimeKey), JSON.stringify(Array.from(collapsedFolderIds)));
|
||||
touchRuntimeStorage(runtimeKey);
|
||||
pendingCollapsedIds = null;
|
||||
} catch {
|
||||
// ignored
|
||||
@@ -180,26 +244,54 @@ const persistCollapsed = (collapsedFolderIds: Set<string>): void => {
|
||||
}, 300);
|
||||
};
|
||||
|
||||
const flushPendingBrowserPersistence = (): void => {
|
||||
if (persistFoldersTimer) clearTimeout(persistFoldersTimer);
|
||||
if (persistCollapsedTimer) clearTimeout(persistCollapsedTimer);
|
||||
persistFoldersTimer = undefined;
|
||||
persistCollapsedTimer = undefined;
|
||||
|
||||
const runtimeKey = pendingBrowserRuntimeKey ?? activeFolderRuntimeKey;
|
||||
let wrote = false;
|
||||
if (pendingFoldersMap !== null) {
|
||||
const key = runtimeStorageKey(FOLDERS_STORAGE_KEY, runtimeKey);
|
||||
const value = JSON.stringify(pendingFoldersMap);
|
||||
safeStorage.setItem(key, value);
|
||||
immediateSafeStorage.setItem(key, value);
|
||||
pendingFoldersMap = null;
|
||||
wrote = true;
|
||||
}
|
||||
if (pendingCollapsedIds !== null) {
|
||||
const key = runtimeStorageKey(COLLAPSED_STORAGE_KEY, runtimeKey);
|
||||
const value = JSON.stringify(Array.from(pendingCollapsedIds));
|
||||
safeStorage.setItem(key, value);
|
||||
immediateSafeStorage.setItem(key, value);
|
||||
pendingCollapsedIds = null;
|
||||
wrote = true;
|
||||
}
|
||||
if (wrote) {
|
||||
const updatedAt = Date.now();
|
||||
touchRuntimeStorage(runtimeKey, updatedAt);
|
||||
touchRuntimeStorage(runtimeKey, updatedAt, immediateSafeStorage);
|
||||
}
|
||||
pendingBrowserRuntimeKey = null;
|
||||
};
|
||||
|
||||
if (typeof window !== 'undefined') {
|
||||
window.addEventListener('beforeunload', () => {
|
||||
if (pendingFoldersMap !== null) {
|
||||
clearTimeout(persistFoldersTimer);
|
||||
try {
|
||||
safeStorage.setItem(FOLDERS_STORAGE_KEY, JSON.stringify(pendingFoldersMap));
|
||||
} catch { /* ignored */ }
|
||||
pendingFoldersMap = null;
|
||||
}
|
||||
if (pendingCollapsedIds !== null) {
|
||||
clearTimeout(persistCollapsedTimer);
|
||||
try {
|
||||
safeStorage.setItem(COLLAPSED_STORAGE_KEY, JSON.stringify(Array.from(pendingCollapsedIds)));
|
||||
} catch { /* ignored */ }
|
||||
pendingCollapsedIds = null;
|
||||
}
|
||||
});
|
||||
const flushPending = () => {
|
||||
try { flushPendingBrowserPersistence(); } catch { /* ignored */ }
|
||||
};
|
||||
window.addEventListener('pagehide', flushPending, { capture: true });
|
||||
window.addEventListener('beforeunload', flushPending, { capture: true });
|
||||
if (typeof document !== 'undefined') {
|
||||
document.addEventListener('visibilitychange', () => {
|
||||
if (document.visibilityState === 'hidden') flushPending();
|
||||
});
|
||||
document.addEventListener('freeze', flushPending);
|
||||
}
|
||||
}
|
||||
|
||||
const persistState = (foldersMap: SessionFoldersMap, collapsedFolderIds: Set<string>): void => {
|
||||
folderMutationRevision += 1;
|
||||
persistFolders(foldersMap);
|
||||
persistCollapsed(collapsedFolderIds);
|
||||
schedulePersistToDisk(foldersMap, collapsedFolderIds);
|
||||
@@ -232,14 +324,6 @@ const syncCollapsedAfterFolderCleanup = (
|
||||
return nextCollapsed;
|
||||
};
|
||||
|
||||
const pruneEmptyArchivedFolders = (scopeKey: string, folders: SessionFolder[]): SessionFolder[] => {
|
||||
if (!scopeKey.startsWith(ARCHIVED_SCOPE_PREFIX)) {
|
||||
return folders;
|
||||
}
|
||||
|
||||
return folders.filter((folder) => folder.sessionIds.length > 0);
|
||||
};
|
||||
|
||||
// --- Store ---
|
||||
|
||||
export const useSessionFoldersStore = create<SessionFoldersStore>()(
|
||||
@@ -248,6 +332,22 @@ export const useSessionFoldersStore = create<SessionFoldersStore>()(
|
||||
foldersMap: readPersistedFolders(),
|
||||
collapsedFolderIds: readPersistedCollapsed(),
|
||||
|
||||
resetForRuntimeSwitch: (runtimeKey: string): void => {
|
||||
try { flushPendingBrowserPersistence(); } catch { /* deferred storage retains failed writes */ }
|
||||
activeFolderRuntimeKey = runtimeKey;
|
||||
folderRuntimeGeneration += 1;
|
||||
folderMutationRevision = 0;
|
||||
diskHydrated = false;
|
||||
diskHydrationInFlight = false;
|
||||
if (diskWriteTimer) clearTimeout(diskWriteTimer);
|
||||
diskWriteTimer = null;
|
||||
set({
|
||||
foldersMap: readPersistedFolders(runtimeKey),
|
||||
collapsedFolderIds: readPersistedCollapsed(runtimeKey),
|
||||
});
|
||||
queueMicrotask(() => void hydrateSessionFoldersFromDisk());
|
||||
},
|
||||
|
||||
getFoldersForScope: (scopeKey: string): SessionFolder[] => {
|
||||
if (!scopeKey) return [];
|
||||
return get().foldersMap[scopeKey] ?? [];
|
||||
@@ -306,18 +406,12 @@ export const useSessionFoldersStore = create<SessionFoldersStore>()(
|
||||
}
|
||||
const nextFolders = scopeFolders.filter((folder) => !idsToDelete.has(folder.id));
|
||||
const nextMap: SessionFoldersMap = { ...current, [scopeKey]: nextFolders };
|
||||
set({ foldersMap: nextMap });
|
||||
persistState(nextMap, get().collapsedFolderIds);
|
||||
|
||||
// Clean up collapsed state for all deleted folders
|
||||
const collapsed = get().collapsedFolderIds;
|
||||
const hasStale = Array.from(idsToDelete).some((id) => collapsed.has(id));
|
||||
if (hasStale) {
|
||||
const nextCollapsed = new Set(collapsed);
|
||||
idsToDelete.forEach((id) => nextCollapsed.delete(id));
|
||||
set({ collapsedFolderIds: nextCollapsed });
|
||||
persistState(nextMap, nextCollapsed);
|
||||
}
|
||||
const nextCollapsed = syncCollapsedAfterFolderCleanup(scopeFolders, nextFolders, collapsed);
|
||||
set(nextCollapsed
|
||||
? { foldersMap: nextMap, collapsedFolderIds: nextCollapsed }
|
||||
: { foldersMap: nextMap });
|
||||
persistState(nextMap, nextCollapsed ?? collapsed);
|
||||
},
|
||||
|
||||
addSessionToFolder: (scopeKey: string, folderId: string, sessionId: string): void => {
|
||||
@@ -466,6 +560,29 @@ export const useSessionFoldersStore = create<SessionFoldersStore>()(
|
||||
persistState(nextMap, nextCollapsed ?? get().collapsedFolderIds);
|
||||
},
|
||||
|
||||
removeSessionEverywhere: (runtimeKey: string, sessionId: string): void => {
|
||||
if (!runtimeKey || runtimeKey !== activeFolderRuntimeKey || runtimeKey !== getRuntimeKey() || !sessionId) return;
|
||||
const current = get().foldersMap;
|
||||
let nextMap: SessionFoldersMap | null = null;
|
||||
|
||||
for (const [scopeKey, scopeFolders] of Object.entries(current)) {
|
||||
let scopeChanged = false;
|
||||
const nextFolders = scopeFolders.map((folder) => {
|
||||
const sessionIds = folder.sessionIds.filter((id) => id !== sessionId);
|
||||
if (sessionIds.length === folder.sessionIds.length) return folder;
|
||||
scopeChanged = true;
|
||||
return { ...folder, sessionIds };
|
||||
});
|
||||
if (!scopeChanged) continue;
|
||||
nextMap ??= { ...current };
|
||||
nextMap[scopeKey] = nextFolders;
|
||||
}
|
||||
|
||||
if (!nextMap) return;
|
||||
set({ foldersMap: nextMap });
|
||||
persistState(nextMap, get().collapsedFolderIds);
|
||||
},
|
||||
|
||||
toggleFolderCollapse: (folderId: string): void => {
|
||||
const collapsed = get().collapsedFolderIds;
|
||||
const next = new Set(collapsed);
|
||||
@@ -478,37 +595,6 @@ export const useSessionFoldersStore = create<SessionFoldersStore>()(
|
||||
persistState(get().foldersMap, next);
|
||||
},
|
||||
|
||||
cleanupSessions: (scopeKey: string, existingSessionIds: Set<string>): void => {
|
||||
if (!scopeKey) return;
|
||||
const current = get().foldersMap;
|
||||
const scopeFolders = current[scopeKey];
|
||||
if (!scopeFolders || scopeFolders.length === 0) return;
|
||||
|
||||
let changed = false;
|
||||
const filteredFolders = scopeFolders.map((folder) => {
|
||||
const filtered = folder.sessionIds.filter((id) => existingSessionIds.has(id));
|
||||
if (filtered.length !== folder.sessionIds.length) {
|
||||
changed = true;
|
||||
return { ...folder, sessionIds: filtered };
|
||||
}
|
||||
return folder;
|
||||
});
|
||||
|
||||
const nextFolders = pruneEmptyArchivedFolders(scopeKey, filteredFolders);
|
||||
if (nextFolders.length !== filteredFolders.length) {
|
||||
changed = true;
|
||||
}
|
||||
|
||||
if (!changed) return;
|
||||
const nextMap: SessionFoldersMap = { ...current, [scopeKey]: nextFolders };
|
||||
const nextCollapsed = syncCollapsedAfterFolderCleanup(scopeFolders, nextFolders, get().collapsedFolderIds);
|
||||
|
||||
set(nextCollapsed
|
||||
? { foldersMap: nextMap, collapsedFolderIds: nextCollapsed }
|
||||
: { foldersMap: nextMap });
|
||||
persistState(nextMap, nextCollapsed ?? get().collapsedFolderIds);
|
||||
},
|
||||
|
||||
getSessionFolderId: (scopeKey: string, sessionId: string): string | null => {
|
||||
if (!scopeKey || !sessionId) return null;
|
||||
const scopeFolders = get().foldersMap[scopeKey];
|
||||
@@ -536,6 +622,10 @@ const hydrateSessionFoldersFromDisk = async (): Promise<void> => {
|
||||
}
|
||||
|
||||
diskHydrationInFlight = true;
|
||||
const runtimeKey = activeFolderRuntimeKey;
|
||||
const generation = folderRuntimeGeneration;
|
||||
const baselineMutationRevision = folderMutationRevision;
|
||||
let completed = false;
|
||||
|
||||
try {
|
||||
const response = await runtimeFetch(SESSION_FOLDERS_API_PATH).catch(() => null);
|
||||
@@ -544,14 +634,21 @@ const hydrateSessionFoldersFromDisk = async (): Promise<void> => {
|
||||
}
|
||||
|
||||
const parsed = await response.json().catch(() => null) as {
|
||||
exists?: boolean;
|
||||
foldersMap?: SessionFoldersMap;
|
||||
collapsedFolderIds?: string[];
|
||||
updatedAt?: number;
|
||||
} | null;
|
||||
|
||||
if (!parsed) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (parsed.exists === false) {
|
||||
completed = true;
|
||||
return;
|
||||
}
|
||||
|
||||
const diskFolders = parsed.foldersMap && typeof parsed.foldersMap === 'object'
|
||||
? parsed.foldersMap
|
||||
: {};
|
||||
@@ -559,23 +656,26 @@ const hydrateSessionFoldersFromDisk = async (): Promise<void> => {
|
||||
? new Set(parsed.collapsedFolderIds.filter((value): value is string => typeof value === 'string'))
|
||||
: new Set<string>();
|
||||
|
||||
const hasDiskData = Object.keys(diskFolders).length > 0 || diskCollapsed.size > 0;
|
||||
if (!hasDiskData) {
|
||||
return;
|
||||
if (generation !== folderRuntimeGeneration || runtimeKey !== getRuntimeKey()) return;
|
||||
const browserUpdatedAt = readStorageIndex().runtimes.find((entry) => entry.runtimeKey === runtimeKey)?.updatedAt ?? 0;
|
||||
const diskUpdatedAt = typeof parsed.updatedAt === 'number' && Number.isFinite(parsed.updatedAt) ? parsed.updatedAt : 0;
|
||||
if (diskUpdatedAt > 0) {
|
||||
lastDiskUpdatedAtByRuntime.set(runtimeKey, Math.max(lastDiskUpdatedAtByRuntime.get(runtimeKey) ?? 0, diskUpdatedAt));
|
||||
}
|
||||
|
||||
useSessionFoldersStore.setState({
|
||||
foldersMap: diskFolders,
|
||||
collapsedFolderIds: diskCollapsed,
|
||||
});
|
||||
|
||||
persistFolders(diskFolders);
|
||||
persistCollapsed(diskCollapsed);
|
||||
const hasDiskAuthority = parsed.exists === true || diskUpdatedAt > 0;
|
||||
if (hasDiskAuthority && folderMutationRevision === baselineMutationRevision && diskUpdatedAt >= browserUpdatedAt) {
|
||||
useSessionFoldersStore.setState({ foldersMap: diskFolders, collapsedFolderIds: diskCollapsed });
|
||||
persistFolders(diskFolders);
|
||||
persistCollapsed(diskCollapsed);
|
||||
}
|
||||
completed = true;
|
||||
} catch {
|
||||
// ignored
|
||||
} finally {
|
||||
diskHydrationInFlight = false;
|
||||
diskHydrated = true;
|
||||
if (generation === folderRuntimeGeneration && runtimeKey === getRuntimeKey()) {
|
||||
diskHydrationInFlight = false;
|
||||
if (completed) diskHydrated = true;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user