Skip to content

fix(sync): wait for persist load before an explicit sync() (#659) - #673

Open
LinusOssmann wants to merge 1 commit into
LegendApp:mainfrom
LinusOssmann:fix/sync-wait-persist-load-659
Open

LinusOssmann wants to merge 1 commit into
LegendApp:mainfrom
LinusOssmann:fix/sync-wait-persist-load-659

Conversation

@LinusOssmann

Copy link
Copy Markdown

Fixes #659.

Problem

loadLocal() is async and not awaited, but syncState(obs$).sync is assigned synchronously. An explicit sync() that runs before isPersistLoaded reads localState.pendingChanges (still undefined) and metadata.lastSync (still undefined), and consumes the one-shot isSynced replay with that empty snapshot. When the load finishes a moment later, the persisted pending changes are never applied, on this launch or any later one if the app syncs early again. The implicit activation path is already gated with when(onAllPersistLoaded); the explicit path is not.

Fix

Wait for isPersistLoaded at the top of sync() when the observable is persisted. This is a no-op for the implicit path (already loaded) and for observables without persist. Side effect: an early explicit sync() now uses the persisted lastSync (delta fetch) instead of undefined (full fetch).

Test

Adds a regression test in tests/persist.test.ts with a persist plugin whose loadTable resolves on demand. It fails on main (create is never called) and passes with the fix. The rest of the suite is unchanged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

retrySync silently loses pending changes when sync() is called before the async persist load finishes

1 participant