1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-21 02:33:46 +00:00

[PM-16262] Make getEnvironment observable and use it in SdkService (#12501)

* feat: re-implement getEnvironment as an observable

* feat: deprecate `getEnvironment`

* fix: use correct environment function in SdkService

* fix: test
This commit is contained in:
Andreas Coroiu
2024-12-20 15:20:23 +01:00
committed by GitHub
parent 2041799174
commit 1d335bb164
5 changed files with 35 additions and 20 deletions

View File

@@ -271,23 +271,30 @@ export class DefaultEnvironmentService implements EnvironmentService {
}
}
async getEnvironment(userId?: UserId): Promise<Environment | undefined> {
getEnvironment$(userId?: UserId): Observable<Environment | undefined> {
if (userId == null) {
return await firstValueFrom(this.environment$);
return this.environment$;
}
const state = await this.getEnvironmentState(userId);
return this.buildEnvironment(state.region, state.urls);
return this.activeAccountId$.pipe(
switchMap((activeUserId) => {
// Previous rules dictated that we only get from user scoped state if there is an active user.
if (activeUserId == null) {
return this.globalState.state$;
}
return this.stateProvider.getUser(userId ?? activeUserId, USER_ENVIRONMENT_KEY).state$;
}),
map((state) => {
return this.buildEnvironment(state?.region, state?.urls);
}),
);
}
private async getEnvironmentState(userId: UserId | null) {
// Previous rules dictated that we only get from user scoped state if there is an active user.
const activeUserId = await firstValueFrom(this.activeAccountId$);
return activeUserId == null
? await firstValueFrom(this.globalState.state$)
: await firstValueFrom(
this.stateProvider.getUser(userId ?? activeUserId, USER_ENVIRONMENT_KEY).state$,
);
/**
* @deprecated Use getEnvironment$ instead.
*/
async getEnvironment(userId?: UserId): Promise<Environment | undefined> {
return firstValueFrom(this.getEnvironment$(userId));
}
async seedUserEnvironment(userId: UserId) {