- 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.
59 lines
1.4 KiB
TypeScript
59 lines
1.4 KiB
TypeScript
/**
|
|
* 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 [];
|
|
}
|
|
}
|