-
Notifications
You must be signed in to change notification settings - Fork 145
Description
I'm trying to make use of the offline-transactions module together with a tanstack db query collection and think I found a bug.
When using @tanstack/offline-transactions, the loadAndReplayTransactions() method is called twice during initialization, causing persisted offline transactions to be executed multiple times (duplicating data).
To reproduce
- Create an offline executor with a mutation function
- Go offline
- Create a transaction (e.g., insert a record)
- Close the tab while still offline
- Reopen the tab (now online)
- The mutation executes 2+ times, creating duplicate records
Note, if I don't refresh the tab, and only switch from offline to online, it works! No duplicated data.
Expected behavior
loadAndReplayTransactions() should only be called once during initialization.
OR, there is some deduping going on?
Possible fix
Not being overly confident with how this all plays together, I believe the issue is that loadAndReplayTransactions() is called in both initialize() and in setupEventListeners() inside OfflineExecutor.ts.
My AI robot overlord suggests to add a guard, but not sure that's the cleanest way to deal with this.
// In initialize():
this._initialLoadDone = false;
this.setupEventListeners();
if (isLeader) {
await this.loadAndReplayTransactions();
this._initialLoadDone = true;
}
// In setupEventListeners():
if (isLeader && this._initialLoadDone) {
this.loadAndReplayTransactions();
}Desktop:
- OS: OSX
- Browser Chrome
- Version 142
"@tanstack/offline-transactions": "^1.0.0",
"@tanstack/query-db-collection": "^1.0.5",
"@tanstack/query-persist-client-core": "^5.91.10",
"@tanstack/svelte-db": "^0.1.49",
"@tanstack/svelte-query": "^6.0.9",