Skip to content

Commit ba27f7f

Browse files
charlesvienjonathanlab
authored andcommitted
avoid redundant init and client allocation on auth sync
1 parent 4bea42b commit ba27f7f

2 files changed

Lines changed: 33 additions & 27 deletions

File tree

apps/code/src/main/services/auth/service.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,7 @@ export class AuthService extends TypedEventEmitter<AuthServiceEvents> {
7878
return this.initializePromise;
7979
}
8080

81-
this.initializePromise = this.doInitialize().finally(() => {
82-
this.initializePromise = null;
83-
});
84-
81+
this.initializePromise = this.doInitialize();
8582
return this.initializePromise;
8683
}
8784

apps/code/src/renderer/features/auth/stores/authStore.ts

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -102,29 +102,38 @@ function createClient(
102102
async function syncAuthState(): Promise<void> {
103103
const authState = await trpcClient.auth.getState.query();
104104
const isAuthenticated = authState.status === "authenticated";
105-
const client =
106-
isAuthenticated && authState.cloudRegion
107-
? createClient(authState.cloudRegion, authState.projectId)
108-
: null;
109-
110-
useAuthStore.setState((state) => ({
111-
...state,
112-
isAuthenticated,
113-
cloudRegion: authState.cloudRegion,
114-
staleCloudRegion: isAuthenticated
115-
? null
116-
: (authState.cloudRegion ?? state.staleCloudRegion),
117-
client,
118-
projectId: authState.projectId,
119-
availableProjectIds: authState.availableProjectIds,
120-
availableOrgIds: authState.availableOrgIds,
121-
needsProjectSelection:
122-
isAuthenticated &&
123-
authState.availableProjectIds.length > 1 &&
124-
authState.projectId === null,
125-
needsScopeReauth: authState.needsScopeReauth,
126-
hasCodeAccess: authState.hasCodeAccess,
127-
}));
105+
106+
useAuthStore.setState((state) => {
107+
const regionChanged = authState.cloudRegion !== state.cloudRegion;
108+
const projectChanged = authState.projectId !== state.projectId;
109+
const client =
110+
isAuthenticated && authState.cloudRegion
111+
? regionChanged || projectChanged || !state.client
112+
? createClient(authState.cloudRegion, authState.projectId)
113+
: state.client
114+
: null;
115+
116+
return {
117+
...state,
118+
isAuthenticated,
119+
cloudRegion: authState.cloudRegion,
120+
staleCloudRegion: isAuthenticated
121+
? null
122+
: (authState.cloudRegion ?? state.staleCloudRegion),
123+
client,
124+
projectId: authState.projectId,
125+
availableProjectIds: authState.availableProjectIds,
126+
availableOrgIds: authState.availableOrgIds,
127+
needsProjectSelection:
128+
isAuthenticated &&
129+
authState.availableProjectIds.length > 1 &&
130+
authState.projectId === null,
131+
needsScopeReauth: authState.needsScopeReauth,
132+
hasCodeAccess: authState.hasCodeAccess,
133+
};
134+
});
135+
136+
const client = useAuthStore.getState().client;
128137

129138
if (!isAuthenticated || !authState.cloudRegion || !client) {
130139
inFlightAuthSync = null;

0 commit comments

Comments
 (0)