Skip to content

Commit 1f0e7a6

Browse files
committed
Fix sync race: return in-flight promise from syncSnapshot so welcome handler waits for actual sync completion
When syncSnapshot() was called while a sync was already in-flight, it returned immediately (resolved promise) after setting pending=true. This meant the onServerWelcome handler's SET_PROJECT_EXPANDED dispatch ran against an empty store before projects were populated. Now syncSnapshot() returns the in-flight syncPromise when syncing=true, so callers actually wait for the sync to complete before proceeding.
1 parent 3b8c5cd commit 1f0e7a6

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

apps/web/src/routes/__root.tsx

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -149,20 +149,25 @@ function EventRouter() {
149149
}
150150
};
151151

152-
const syncSnapshot = async () => {
152+
let syncPromise: Promise<void> = Promise.resolve();
153+
154+
const syncSnapshot = (): Promise<void> => {
153155
if (syncing) {
154156
pending = true;
155-
return;
157+
return syncPromise;
156158
}
157159
syncing = true;
158-
try {
159-
pending = false;
160-
await flushSnapshotSync();
161-
} catch {
162-
// Keep prior state and wait for next domain event to trigger a resync.
163-
} finally {
164-
syncing = false;
165-
}
160+
syncPromise = (async () => {
161+
try {
162+
pending = false;
163+
await flushSnapshotSync();
164+
} catch {
165+
// Keep prior state and wait for next domain event to trigger a resync.
166+
} finally {
167+
syncing = false;
168+
}
169+
})();
170+
return syncPromise;
166171
};
167172

168173
void syncSnapshot().catch(() => undefined);

0 commit comments

Comments
 (0)