Fixed db resync.
This commit is contained in:
parent
1eb6a99ff6
commit
214c63de20
2 changed files with 45 additions and 5 deletions
|
|
@ -51,7 +51,16 @@ db.version(5).stores({
|
||||||
participants: 'id, email, preferred_name, email_confirmed, updated_at, deleted_at',
|
participants: 'id, email, preferred_name, email_confirmed, updated_at, deleted_at',
|
||||||
})
|
})
|
||||||
|
|
||||||
db.version(6).stores({}).upgrade(tx => tx.table('session').clear())
|
db.version(6).stores({}).upgrade(async tx => {
|
||||||
|
await tx.table('session').clear()
|
||||||
|
await tx.table('meta').clear()
|
||||||
|
await tx.table('participants').clear()
|
||||||
|
await tx.table('tickets').clear()
|
||||||
|
await tx.table('departments').clear()
|
||||||
|
await tx.table('volunteers').clear()
|
||||||
|
await tx.table('shifts').clear()
|
||||||
|
await tx.table('volunteer_shifts').clear()
|
||||||
|
})
|
||||||
|
|
||||||
export async function getLastSync() {
|
export async function getLastSync() {
|
||||||
const m = await db.meta.get('last_sync')
|
const m = await db.meta.get('last_sync')
|
||||||
|
|
|
||||||
|
|
@ -4,10 +4,36 @@ import { api } from './api.js'
|
||||||
let syncing = false
|
let syncing = false
|
||||||
let sseSource = null
|
let sseSource = null
|
||||||
|
|
||||||
|
async function checkBuildChanged() {
|
||||||
|
try {
|
||||||
|
const res = await fetch('/api/version')
|
||||||
|
const { build } = await res.json()
|
||||||
|
if (!build) return
|
||||||
|
const stored = await db.meta.get('build')
|
||||||
|
if (stored && stored.value !== build) {
|
||||||
|
await db.transaction('rw',
|
||||||
|
[db.meta, db.event, db.participants, db.tickets, db.departments, db.volunteers, db.shifts, db.volunteer_shifts],
|
||||||
|
async () => {
|
||||||
|
await db.meta.clear()
|
||||||
|
await db.event.clear()
|
||||||
|
await db.participants.clear()
|
||||||
|
await db.tickets.clear()
|
||||||
|
await db.departments.clear()
|
||||||
|
await db.volunteers.clear()
|
||||||
|
await db.shifts.clear()
|
||||||
|
await db.volunteer_shifts.clear()
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
await db.meta.put({ key: 'build', value: build })
|
||||||
|
} catch {}
|
||||||
|
}
|
||||||
|
|
||||||
export async function syncPull() {
|
export async function syncPull() {
|
||||||
if (syncing) return
|
if (syncing) return
|
||||||
syncing = true
|
syncing = true
|
||||||
try {
|
try {
|
||||||
|
await checkBuildChanged()
|
||||||
const since = await getLastSync()
|
const since = await getLastSync()
|
||||||
const data = await api.sync.pull(since)
|
const data = await api.sync.pull(since)
|
||||||
|
|
||||||
|
|
@ -51,7 +77,7 @@ export async function syncPull() {
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
await setLastSync(data.server_time)
|
if (data.server_time) await setLastSync(data.server_time)
|
||||||
return true
|
return true
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.warn('Sync pull failed:', err.message)
|
console.warn('Sync pull failed:', err.message)
|
||||||
|
|
@ -97,7 +123,7 @@ export function startSSE(onEvent) {
|
||||||
syncPull()
|
syncPull()
|
||||||
}, 5000)
|
}, 5000)
|
||||||
}
|
}
|
||||||
})
|
}).catch(() => {})
|
||||||
}
|
}
|
||||||
|
|
||||||
connect()
|
connect()
|
||||||
|
|
@ -108,18 +134,23 @@ export function stopSSE() {
|
||||||
sseSource = null
|
sseSource = null
|
||||||
}
|
}
|
||||||
|
|
||||||
// Poll for sync when online, with exponential backoff on failure
|
|
||||||
let syncInterval = null
|
let syncInterval = null
|
||||||
|
let onlineHandler = null
|
||||||
|
|
||||||
export function startSyncLoop(intervalMs = 30000) {
|
export function startSyncLoop(intervalMs = 30000) {
|
||||||
if (syncInterval) return
|
if (syncInterval) return
|
||||||
syncInterval = setInterval(() => {
|
syncInterval = setInterval(() => {
|
||||||
if (navigator.onLine) syncPull()
|
if (navigator.onLine) syncPull()
|
||||||
}, intervalMs)
|
}, intervalMs)
|
||||||
window.addEventListener('online', () => syncPull())
|
onlineHandler = () => syncPull()
|
||||||
|
window.addEventListener('online', onlineHandler)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function stopSyncLoop() {
|
export function stopSyncLoop() {
|
||||||
clearInterval(syncInterval)
|
clearInterval(syncInterval)
|
||||||
syncInterval = null
|
syncInterval = null
|
||||||
|
if (onlineHandler) {
|
||||||
|
window.removeEventListener('online', onlineHandler)
|
||||||
|
onlineHandler = null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue