fix: clean up sync state tracking in offline sync service

- Remove duplicate state tracking from service
- State transition detection now fully handled by hook
- Fix remaining syncState reference bug
- Simplify sync lock mechanism
This commit is contained in:
Gigi
2025-10-09 13:38:23 +01:00
parent 450776f9d0
commit 3330f22f82
2 changed files with 10 additions and 39 deletions

View File

@@ -48,7 +48,7 @@ export function useOfflineSync({
if (wasLocalOnly && isNowOnline) { if (wasLocalOnly && isNowOnline) {
console.log('✈️ Detected transition: Flight Mode → Online') console.log('✈️ Detected transition: Flight Mode → Online')
syncLocalEventsToRemote(relayPool, account, true) syncLocalEventsToRemote(relayPool, account)
} }
previousStateRef.current.hasRemoteRelays = hasRemoteRelays previousStateRef.current.hasRemoteRelays = hasRemoteRelays

View File

@@ -4,36 +4,22 @@ import { IAccount } from 'applesauce-core/helpers'
import { RELAYS } from '../config/relays' import { RELAYS } from '../config/relays'
import { isLocalRelay } from '../utils/helpers' import { isLocalRelay } from '../utils/helpers'
interface SyncState { let isSyncing = false
lastRemoteConnectionTime: number
wasLocalOnly: boolean
isSyncing: boolean
}
const syncState: SyncState = {
lastRemoteConnectionTime: 0,
wasLocalOnly: false,
isSyncing: false
}
/** /**
* Syncs local-only events to remote relays when coming back online * Syncs local-only events to remote relays when coming back online
*/ */
export async function syncLocalEventsToRemote( export async function syncLocalEventsToRemote(
relayPool: RelayPool, relayPool: RelayPool,
account: IAccount | null, account: IAccount
hasRemoteRelays: boolean
): Promise<void> { ): Promise<void> {
if (!account || syncState.isSyncing) return if (isSyncing) {
console.log('⏳ Sync already in progress, skipping...')
// Detect transition from local-only to having remote relays return
const justCameOnline = !syncState.wasLocalOnly && hasRemoteRelays }
syncState.wasLocalOnly = !hasRemoteRelays
if (!justCameOnline) return
console.log('🔄 Coming back online - syncing local events to remote relays...') console.log('🔄 Coming back online - syncing local events to remote relays...')
syncState.isSyncing = true isSyncing = true
try { try {
const localRelays = RELAYS.filter(isLocalRelay) const localRelays = RELAYS.filter(isLocalRelay)
@@ -79,7 +65,7 @@ export async function syncLocalEventsToRemote(
if (eventsToSync.length === 0) { if (eventsToSync.length === 0) {
console.log('✅ No local events to sync') console.log('✅ No local events to sync')
syncState.isSyncing = false isSyncing = false
return return
} }
@@ -102,25 +88,10 @@ export async function syncLocalEventsToRemote(
} }
console.log(`✅ Synced ${successCount}/${uniqueEvents.length} events to remote relays`) console.log(`✅ Synced ${successCount}/${uniqueEvents.length} events to remote relays`)
syncState.lastRemoteConnectionTime = Date.now()
} catch (error) { } catch (error) {
console.error('❌ Error during offline sync:', error) console.error('❌ Error during offline sync:', error)
} finally { } finally {
syncState.isSyncing = false isSyncing = false
} }
} }
/**
* Checks if we should sync based on current relay state
*/
export function shouldSync(
hasRemoteRelays: boolean,
account: IAccount | null
): boolean {
if (!account || syncState.isSyncing) return false
// Only sync if we just came online (transition from local-only to having remote relays)
const justCameOnline = syncState.wasLocalOnly && hasRemoteRelays
return justCameOnline
}