Skip to content

Commit cb8fbe5

Browse files
committed
fix(desktop): baseline welcome env-id from the atom to avoid immediate-fire false resolve
onWelcome subscribes with `immediate: true`, so the listener fires synchronously with whatever welcome payload is already in the atom. The previous code compared against `previousPrimaryEnvId` (descriptor-derived); if the descriptor hadn't loaded yet, that was null and any non-null current welcome would resolve the promise instantly, completing the "syncing" stage before the new backend's welcome actually arrived. Capture the current welcome's env-id from the atom as the baseline instead so the immediate fire never matches the "new welcome arrived" predicate.
1 parent e15edd9 commit cb8fbe5

2 files changed

Lines changed: 14 additions & 3 deletions

File tree

apps/web/src/components/settings/ConnectionsSettings.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ import {
8686
type ServerClientSessionRecord,
8787
type ServerPairingLinkRecord,
8888
} from "~/environments/primary";
89-
import { onWelcome } from "~/rpc/serverState";
89+
import { getWelcomePayload, onWelcome } from "~/rpc/serverState";
9090
import { suppressReconnect } from "~/rpc/wsConnectionState";
9191
import { useStore } from "~/store";
9292
import type { WsRpcClient } from "~/rpc/wsRpcClient";
@@ -2403,13 +2403,20 @@ export function ConnectionsSettings() {
24032403
const previousPrimaryEnvId = readPrimaryEnvironmentDescriptor()?.environmentId ?? null;
24042404

24052405
// Subscribe to welcomes BEFORE the swap so we can wait for the new
2406-
// backend's welcome (env-id !== previous) before declaring the swap done.
2406+
// backend's welcome before declaring the swap done. Capture the current
2407+
// welcome's env-id as the baseline instead of comparing against
2408+
// `previousPrimaryEnvId`: `onWelcome` is `immediate: true` and fires
2409+
// synchronously with the atom's current value, so if the descriptor
2410+
// hasn't loaded (`previousPrimaryEnvId === null`) any non-null payload
2411+
// would otherwise resolve the promise instantly. Using the live welcome
2412+
// atom as the baseline closes that race.
2413+
const baselineWelcomeEnvId = getWelcomePayload()?.environment.environmentId ?? null;
24072414
let resolveNewWelcome: (() => void) | null = null;
24082415
const newWelcomePromise = new Promise<void>((resolve) => {
24092416
resolveNewWelcome = resolve;
24102417
});
24112418
const unsubscribeWelcome = onWelcome((payload) => {
2412-
if (payload.environment.environmentId !== previousPrimaryEnvId) {
2419+
if (payload.environment.environmentId !== baselineWelcomeEnvId) {
24132420
resolveNewWelcome?.();
24142421
}
24152422
});

apps/web/src/rpc/serverState.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ export function getServerConfig(): ServerConfig | null {
7575
return appAtomRegistry.get(serverConfigAtom);
7676
}
7777

78+
export function getWelcomePayload(): ServerLifecycleWelcomePayload | null {
79+
return appAtomRegistry.get(welcomeAtom);
80+
}
81+
7882
export function getServerKeybindings(): ServerConfig["keybindings"] {
7983
return selectKeybindings(getServerConfig());
8084
}

0 commit comments

Comments
 (0)