1
0
mirror of https://github.com/bitwarden/directory-connector synced 2025-12-16 16:23:41 +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 { KdfType } from "../enums/kdfType";
import { ThemeType } from "../enums/themeType"; import { ThemeType } from "../enums/themeType";
@@ -25,8 +25,8 @@ import { FolderView } from "../models/view/folderView";
import { SendView } from "../models/view/sendView"; import { SendView } from "../models/view/sendView";
export abstract class StateService<T extends Account = Account> { export abstract class StateService<T extends Account = Account> {
accounts: BehaviorSubject<{ [userId: string]: T }>; accounts$: Observable<{ [userId: string]: T }>;
activeAccount: BehaviorSubject<string>; activeAccount$: Observable<string>;
addAccount: (account: T) => Promise<void>; addAccount: (account: T) => Promise<void>;
setActiveUser: (userId: string) => 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 { import {
EnvironmentService as EnvironmentServiceAbstraction, EnvironmentService as EnvironmentServiceAbstraction,
@@ -21,9 +21,15 @@ export class EnvironmentService implements EnvironmentServiceAbstraction {
private keyConnectorUrl: string; private keyConnectorUrl: string;
constructor(private stateService: StateService) { constructor(private stateService: StateService) {
this.stateService.activeAccount.subscribe(async () => { 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(); await this.setUrlsFromStorage();
}); }),
)
.subscribe();
} }
hasBaseUrl() { hasBaseUrl() {

View File

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

View File

@@ -1,3 +1,5 @@
import { firstValueFrom } from "rxjs";
import { CipherService } from "../abstractions/cipher.service"; import { CipherService } from "../abstractions/cipher.service";
import { CollectionService } from "../abstractions/collection.service"; import { CollectionService } from "../abstractions/collection.service";
import { CryptoService } from "../abstractions/crypto.service"; import { CryptoService } from "../abstractions/crypto.service";
@@ -67,7 +69,8 @@ export class VaultTimeoutService implements VaultTimeoutServiceAbstraction {
return; 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))) { if (userId != null && (await this.shouldLock(userId))) {
await this.executeTimeoutAction(userId); await this.executeTimeoutAction(userId);
} }

View File

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