Implement synchronization, offline data handling, and intelligent request routing

- Add services for offline data management, including `offline-data.service.ts`, ensuring data is saved to a local database and synced with the server when online.
- Introduce bidirectional `SyncService` for managing data synchronization with conflict resolution and retry mechanisms.
- Create `data.service.ts` to handle smart routing between local database and server API based on connectivity status.
- Update models and logic to support comprehensive synchronization for books, chapters, characters, and conversations.
- Implement event listeners for online/offline detection and automatic sync scheduling.
This commit is contained in:
natreex
2025-11-17 07:47:15 -05:00
parent 71067c6fa8
commit 09768aafcf
4 changed files with 1048 additions and 0 deletions

58
lib/db/sync.service.ts Normal file
View File

@@ -0,0 +1,58 @@
/**
* Sync progress interface
*/
export interface SyncProgress {
isSyncing: boolean;
pendingChanges: number;
isOnline: boolean;
lastError?: string;
}
/**
* Get sync status from local database
*/
export async function getSyncStatus(): Promise<SyncProgress> {
if (!window.electron) {
return {
isSyncing: false,
pendingChanges: 0,
isOnline: navigator.onLine
};
}
try {
const result = await window.electron.dbGetSyncStatus();
if (!result.success) {
throw new Error(result.error);
}
return result.data;
} catch (error) {
console.error('Failed to get sync status:', error);
return {
isSyncing: false,
pendingChanges: 0,
isOnline: navigator.onLine,
lastError: error instanceof Error ? error.message : 'Unknown error'
};
}
}
/**
* Get pending changes to sync
*/
export async function getPendingChanges(limit: number = 100) {
if (!window.electron) {
return [];
}
try {
const result = await window.electron.dbGetPendingChanges(limit);
if (!result.success) {
throw new Error(result.error);
}
return result.data || [];
} catch (error) {
console.error('Failed to get pending changes:', error);
return [];
}
}