1
0
mirror of https://github.com/bitwarden/directory-connector synced 2025-12-05 23:53:21 +00:00

Fix eslint issues related to state

This commit is contained in:
Addison Beck
2023-12-06 17:52:45 +00:00
parent f71f6db0e5
commit 5bd63c6d90
5 changed files with 29 additions and 17 deletions

View File

@@ -1,4 +1,4 @@
import { BehaviorSubject } from "rxjs";
import { Observable } from "rxjs";
import { KdfType } from "../enums/kdfType";
import { ThemeType } from "../enums/themeType";
@@ -25,8 +25,8 @@ import { FolderView } from "../models/view/folderView";
import { SendView } from "../models/view/sendView";
export abstract class StateService<T extends Account = Account> {
accounts: BehaviorSubject<{ [userId: string]: T }>;
activeAccount: BehaviorSubject<string>;
accounts$: Observable<{ [userId: string]: T }>;
activeAccount$: Observable<string>;
addAccount: (account: T) => Promise<void>;
setActiveUser: (userId: string) => Promise<void>;

View File

@@ -1,4 +1,4 @@
import { Observable, Subject } from "rxjs";
import { concatMap, distinctUntilChanged, Observable, Subject } from "rxjs";
import {
EnvironmentService as EnvironmentServiceAbstraction,
@@ -21,9 +21,15 @@ export class EnvironmentService implements EnvironmentServiceAbstraction {
private keyConnectorUrl: string;
constructor(private stateService: StateService) {
this.stateService.activeAccount.subscribe(async () => {
await this.setUrlsFromStorage();
});
this.stateService.activeAccount$
.pipe(
// Use == here to not trigger on undefined -> null transition
distinctUntilChanged((oldUserId: string, newUserId: string) => oldUserId == newUserId),
concatMap(async () => {
await this.setUrlsFromStorage();
}),
)
.subscribe();
}
hasBaseUrl() {

View File

@@ -52,8 +52,11 @@ export class StateService<
TAccount extends Account = Account
> implements StateServiceAbstraction<TAccount>
{
accounts = new BehaviorSubject<{ [userId: string]: TAccount }>({});
activeAccount = new BehaviorSubject<string>(null);
protected accountsSubject = new BehaviorSubject<{ [userId: string]: TAccount }>({});
accounts$ = this.accountsSubject.asObservable();
protected activeAccountSubject = new BehaviorSubject<string | null>(null);
activeAccount$ = this.activeAccountSubject.asObservable();
protected state: State<TGlobalState, TAccount> = new State<TGlobalState, TAccount>(
this.createGlobals()
@@ -100,7 +103,7 @@ export class StateService<
this.state.activeUserId = storedActiveUser;
}
await this.pushAccounts();
this.activeAccount.next(this.state.activeUserId);
this.activeAccountSubject.next(this.state.activeUserId);
}
async syncAccountFromDisk(userId: string) {
@@ -120,14 +123,14 @@ export class StateService<
await this.scaffoldNewAccountStorage(account);
await this.setLastActive(new Date().getTime(), { userId: account.profile.userId });
await this.setActiveUser(account.profile.userId);
this.activeAccount.next(account.profile.userId);
this.activeAccountSubject.next(account.profile.userId);
}
async setActiveUser(userId: string): Promise<void> {
this.clearDecryptedDataForActiveUser();
this.state.activeUserId = userId;
await this.storageService.save(keys.activeUserId, userId);
this.activeAccount.next(this.state.activeUserId);
this.activeAccountSubject.next(this.state.activeUserId);
await this.pushAccounts();
}
@@ -2338,11 +2341,11 @@ export class StateService<
protected async pushAccounts(): Promise<void> {
await this.pruneInMemoryAccounts();
if (this.state?.accounts == null || Object.keys(this.state.accounts).length < 1) {
this.accounts.next(null);
this.accountsSubject.next(null);
return;
}
this.accounts.next(this.state.accounts);
this.accountsSubject.next(this.state.accounts);
}
protected reconcileOptions(

View File

@@ -1,3 +1,5 @@
import { firstValueFrom } from "rxjs";
import { CipherService } from "../abstractions/cipher.service";
import { CollectionService } from "../abstractions/collection.service";
import { CryptoService } from "../abstractions/crypto.service";
@@ -67,7 +69,8 @@ export class VaultTimeoutService implements VaultTimeoutServiceAbstraction {
return;
}
for (const userId in this.stateService.accounts.getValue()) {
const accounts = await firstValueFrom(this.stateService.accounts$);
for (const userId in accounts) {
if (userId != null && (await this.shouldLock(userId))) {
await this.executeTimeoutAction(userId);
}

View File

@@ -560,10 +560,10 @@ export class StateService
protected async pushAccounts(): Promise<void> {
if (this.state?.accounts == null || Object.keys(this.state.accounts).length < 1) {
this.accounts.next(null);
this.accountsSubject.next(null);
return;
}
this.accounts.next(this.state.accounts);
this.accountsSubject.next(this.state.accounts);
}
protected async hasTemporaryStorage(): Promise<boolean> {