Fix infinite spinner in offline mode by serializing database operations

- Make onMount await loadLibraryData before starting loadFolders
- Add 5-second timeout to loadFolders using Promise.race
- Make folder loading non-critical to prevent UI blocking
- Prevents database lock deadlock when is_network_path blocks offline
This commit is contained in:
vicrodh committed 2026-01-20 02:38:32 -06:00
1 parent 2b4b59fc93
commit e13e5e70e9
1 file changed
+15 -5
@@ -266,9 +266,9 @@
previousOfflineState = isOffline; previousOfflineState = isOffline;
}); });
onMount(() => { onMount(async () => {
loadLibraryData(); await loadLibraryData();
loadFolders(); loadFolders(); // Load in background - doesn't block UI
checkDiscogsCredentials(); checkDiscogsCredentials();
// Subscribe to offline state changes // Subscribe to offline state changes
@@ -416,7 +416,16 @@
async function loadFolders() { async function loadFolders() {
try { try {
folders = await invoke<LibraryFolder[]>('library_get_folders_with_metadata'); console.log('[LocalLibrary] loadFolders START');
// Add timeout to prevent infinite hang when offline
const timeoutPromise = new Promise<LibraryFolder[]>((_, reject) =>
setTimeout(() => reject(new Error('Folder loading timeout')), 5000)
);
const foldersPromise = invoke<LibraryFolder[]>('library_get_folders_with_metadata');
folders = await Promise.race([foldersPromise, timeoutPromise]);
console.log('[LocalLibrary] Received folders:', folders.length);
// Check accessibility for network folders (skip when offline to avoid hanging) // Check accessibility for network folders (skip when offline to avoid hanging)
for (const folder of folders) { for (const folder of folders) {
if (folder.isNetwork) { if (folder.isNetwork) {
@@ -433,7 +442,8 @@
} }
folderAccessibility = new Map(folderAccessibility); folderAccessibility = new Map(folderAccessibility);
} catch (err) { } catch (err) {
console.error('Failed to load folders:', err); console.error('[LocalLibrary] Failed to load folders (timeout or error):', err);
// Continue anyway - folders are not critical for basic library functionality
} }
} }