The "back online" toast fires on the real reconnect path: the pull that runs first cleared the offline flag before the owed push looked at it
This commit is contained in:
1 parent
826432a3a2
commit
14c454f5c1
2 files changed
+9
-5
No files matched your search
@@ -92,9 +92,12 @@ describe('offline and unsynced flags', () => {
|
||||
await useStore.getState().pushState()
|
||||
expect(useStore.getState().sync).toMatchObject({ offline: true, pending: true })
|
||||
expect(localStorage.getItem('gym_dirty')).toBe('1')
|
||||
// back online: the push lands, the flags clear, and the user hears about it once
|
||||
// back online: the `online` event checks with the server first (the pull clears the offline
|
||||
// flag), then the owed push lands, the flags clear, and the user hears about it once
|
||||
api.mockResolvedValueOnce({ state: { ...clone(DEF), _ts: 100, workouts: [workout('w1')], _rev: 1 }, rev: 1 })
|
||||
api.mockResolvedValueOnce({ ok: true, rev: 2 })
|
||||
await useStore.getState().pushState()
|
||||
window.dispatchEvent(new Event('online'))
|
||||
await new Promise(r => setTimeout(r, 20))
|
||||
expect(useStore.getState().sync).toMatchObject({ offline: false, pending: false })
|
||||
expect(localStorage.getItem('gym_dirty')).toBeNull()
|
||||
await new Promise(r => setTimeout(r, 0)) // the toast goes through a lazy import of useUI
|
||||
|
||||
@@ -109,6 +109,7 @@ export const useStore = create((set, get) => {
|
||||
let forceNext = false // the next push replaces the server copy outright (import, reset)
|
||||
let lastCheck = 0
|
||||
let pollTm = null
|
||||
let offlineChanges = false // a push failed for lack of network — the next one that lands says so
|
||||
|
||||
const readSync = () => { try { return JSON.parse(localStorage.getItem(SYNC_KEY)) || null } catch { return null } }
|
||||
const writeSync = (rev, ts) => localStorage.setItem(SYNC_KEY, JSON.stringify({ rev, ts: ts || 0 }))
|
||||
@@ -223,15 +224,15 @@ export const useStore = create((set, get) => {
|
||||
toldTooLarge = false
|
||||
// Back from offline with changes that were waiting: say so once — the banner that promised
|
||||
// "syncs when you're back online" has just kept its word.
|
||||
const was = get().sync
|
||||
setSync({ offline: false, pending: false, lastSynced: Date.now() })
|
||||
if (was.offline && was.pending) {
|
||||
if (offlineChanges) {
|
||||
offlineChanges = false
|
||||
import('./useUI.js').then(({ useUI }) => useUI.getState().toast(t('Back online — synced with the server.'))).catch(() => {})
|
||||
}
|
||||
} catch (e) {
|
||||
// A session that is gone is boot's business (/api/me); the copy stays owed to the server.
|
||||
if (e.status === 401) { localStorage.setItem('gym_dirty', '1'); return }
|
||||
if (isNetworkError(e)) { localStorage.setItem('gym_dirty', '1'); setSync({ offline: true, pending: true }); return }
|
||||
if (isNetworkError(e)) { localStorage.setItem('gym_dirty', '1'); offlineChanges = true; setSync({ offline: true, pending: true }); return }
|
||||
if (e.status === 409 && e.data && attempt < 2) {
|
||||
// Another device wrote since this one last read. The server sent its document along;
|
||||
// merge and push once more against that revision. A second refusal in a row leaves the
|
||||
|
||||
Reference in new issue
Block a user