1
0
mirror of https://github.com/bitwarden/jslib synced 2025-12-06 00:03:29 +00:00

Expose Observables over behavior subjects

This commit is contained in:
Matt Gibson
2022-05-19 10:58:02 -04:00
parent 9a933640dc
commit c7e549a2c1
2 changed files with 10 additions and 7 deletions

View File

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

View File

@@ -1,4 +1,4 @@
import { BehaviorSubject } from "rxjs";
import { BehaviorSubject, Observable } from "rxjs";
import { LogService } from "../abstractions/log.service";
import { StateService as StateServiceAbstraction } from "../abstractions/state.service";
@@ -53,7 +53,10 @@ export class StateService<
> implements StateServiceAbstraction<TAccount>
{
accounts = new BehaviorSubject<{ [userId: string]: TAccount }>({});
activeAccount = new BehaviorSubject<string>(null);
protected innerActiveAccount = new BehaviorSubject<string>(null);
get activeAccount(): Observable<string> {
return this.innerActiveAccount.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.innerActiveAccount.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.innerActiveAccount.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.innerActiveAccount.next(this.state.activeUserId);
await this.pushAccounts();
}