Fix for offline mode
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
// Favorites service - Handles toggling favorite status with optimistic updates
|
||||
// TRACES: UR-017 | DR-021
|
||||
|
||||
import { get } from "svelte/store";
|
||||
import { commands } from "$lib/api/bindings";
|
||||
import { auth } from "$lib/stores/auth";
|
||||
import { isConnected } from "$lib/stores/connectivity";
|
||||
|
||||
/**
|
||||
* Toggle the favorite status of an item.
|
||||
@@ -31,21 +33,31 @@ export async function toggleFavorite(
|
||||
// 1. Update local database first (optimistic update)
|
||||
await commands.storageToggleFavorite(userId, itemId, newIsFavorite);
|
||||
|
||||
// 2. Sync to Jellyfin server
|
||||
try {
|
||||
const repo = auth.getRepository();
|
||||
if (newIsFavorite) {
|
||||
await repo.markFavorite(itemId);
|
||||
} else {
|
||||
await repo.unmarkFavorite(itemId);
|
||||
}
|
||||
// 2. Sync to Jellyfin server.
|
||||
//
|
||||
// Only attempt this when we're actually connected. When offline, the server
|
||||
// call can hang on a long network timeout rather than failing fast — which
|
||||
// blocks the caller (and leaves the favorite button greyed out with a wait
|
||||
// cursor) until the request finally gives up, effectively only recovering
|
||||
// once we're back online. The local DB write above keeps the pending_sync
|
||||
// flag set, so the change still syncs later; we just don't block the UI on
|
||||
// an unreachable server here.
|
||||
if (get(isConnected)) {
|
||||
try {
|
||||
const repo = auth.getRepository();
|
||||
if (newIsFavorite) {
|
||||
await repo.markFavorite(itemId);
|
||||
} else {
|
||||
await repo.unmarkFavorite(itemId);
|
||||
}
|
||||
|
||||
// 3. Mark as synced
|
||||
await commands.storageMarkSynced(userId, itemId);
|
||||
} catch (error) {
|
||||
console.error("Failed to sync favorite to server:", error);
|
||||
// Favorite is stored locally and will be synced later
|
||||
// via sync queue (when implemented)
|
||||
// 3. Mark as synced
|
||||
await commands.storageMarkSynced(userId, itemId);
|
||||
} catch (error) {
|
||||
console.error("Failed to sync favorite to server:", error);
|
||||
// Favorite is stored locally and will be synced later
|
||||
// via sync queue (when implemented)
|
||||
}
|
||||
}
|
||||
|
||||
return newIsFavorite;
|
||||
|
||||
Reference in New Issue
Block a user