55 lines
2.0 KiB
TypeScript
55 lines
2.0 KiB
TypeScript
/**
|
|
* Helper for managing sync progress
|
|
*/
|
|
|
|
export interface SyncProgress {
|
|
currentStep: number
|
|
totalSteps: number
|
|
completed: boolean
|
|
currentRelay?: string // URL of the relay currently being used
|
|
}
|
|
|
|
export async function initializeSyncProgress(
|
|
onProgress?: (progress: SyncProgress) => void
|
|
): Promise<{ updateProgress: (step: number, completed?: boolean) => void }> {
|
|
const TOTAL_STEPS = 7
|
|
const { relaySessionManager } = await import('../relaySessionManager')
|
|
const { syncProgressManager } = await import('../syncProgressManager')
|
|
const activeRelays = await relaySessionManager.getActiveRelays()
|
|
const initialRelay = activeRelays[0] ?? 'Connecting...'
|
|
|
|
if (onProgress) {
|
|
onProgress({ currentStep: 0, totalSteps: TOTAL_STEPS, completed: false, currentRelay: initialRelay })
|
|
}
|
|
syncProgressManager.setProgress({ currentStep: 0, totalSteps: TOTAL_STEPS, completed: false, currentRelay: initialRelay })
|
|
|
|
const updateProgress = (step: number, completed: boolean = false): void => {
|
|
const currentRelay = syncProgressManager.getProgress()?.currentRelay ?? initialRelay
|
|
const progressUpdate = { currentStep: step, totalSteps: TOTAL_STEPS, completed, currentRelay }
|
|
if (onProgress) {
|
|
onProgress(progressUpdate)
|
|
}
|
|
syncProgressManager.setProgress(progressUpdate)
|
|
}
|
|
|
|
return { updateProgress }
|
|
}
|
|
|
|
export async function finalizeSync(currentTimestamp: number): Promise<void> {
|
|
const { setLastSyncDate } = await import('../syncStorage')
|
|
await setLastSyncDate(currentTimestamp)
|
|
|
|
const { syncProgressManager } = await import('../syncProgressManager')
|
|
const { configStorage } = await import('../configStorage')
|
|
const config = await configStorage.getConfig()
|
|
const currentRelay = syncProgressManager.getProgress()?.currentRelay
|
|
if (currentRelay) {
|
|
const relayConfig = config.relays.find((r) => r.url === currentRelay)
|
|
if (relayConfig) {
|
|
await configStorage.updateRelay(relayConfig.id, { lastSyncDate: Date.now() })
|
|
}
|
|
}
|
|
|
|
console.warn('[Sync] Synchronization completed successfully')
|
|
}
|