mirror of
https://github.com/bitwarden/browser.git
synced 2025-02-08 00:01:28 +01:00
Establish account service observables and update methods
This commit is contained in:
parent
0a9c8a416a
commit
4f97c7e283
@ -1,4 +1,11 @@
|
||||
import { BehaviorSubject, Subject } from "rxjs";
|
||||
import {
|
||||
BehaviorSubject,
|
||||
Subject,
|
||||
combineLatestWith,
|
||||
map,
|
||||
distinctUntilChanged,
|
||||
share,
|
||||
} from "rxjs";
|
||||
|
||||
import { InternalAccountService } from "../../auth/abstractions/account.service";
|
||||
import { LogService } from "../../platform/abstractions/log.service";
|
||||
@ -8,20 +15,27 @@ import { AuthenticationStatus } from "../enums/authentication-status";
|
||||
|
||||
export class AccountServiceImplementation implements InternalAccountService {
|
||||
private accounts = new BehaviorSubject<Record<UserId, AuthenticationStatus>>({});
|
||||
private activeAccount = new BehaviorSubject<{
|
||||
id: UserId | undefined;
|
||||
status: AuthenticationStatus | undefined;
|
||||
}>({ id: undefined, status: undefined });
|
||||
private activeAccountId = new BehaviorSubject<UserId | undefined>(undefined);
|
||||
private lock = new Subject<UserId>();
|
||||
private logout = new Subject<UserId>();
|
||||
|
||||
accounts$ = this.accounts.asObservable();
|
||||
activeAccount$ = this.activeAccount.asObservable();
|
||||
activeAccount$ = this.activeAccountId.pipe(
|
||||
combineLatestWith(this.accounts$),
|
||||
map(([id, accounts]) => (id ? { id, status: accounts[id] } : undefined)),
|
||||
distinctUntilChanged((a, b) => a.id === b.id && a.status === b.status),
|
||||
share()
|
||||
);
|
||||
accountLock$ = this.lock.asObservable();
|
||||
accountLogout$ = this.logout.asObservable();
|
||||
constructor(private messagingService: MessagingService, private logService: LogService) {}
|
||||
|
||||
setAccountStatus(userId: UserId, status: AuthenticationStatus): void {
|
||||
if (this.accounts.value[userId] === status) {
|
||||
// Do not emit on no change
|
||||
return;
|
||||
}
|
||||
|
||||
this.accounts.value[userId] = status;
|
||||
this.accounts.next(this.accounts.value);
|
||||
if (status === AuthenticationStatus.LoggedOut) {
|
||||
@ -32,9 +46,15 @@ export class AccountServiceImplementation implements InternalAccountService {
|
||||
}
|
||||
|
||||
switchAccount(userId: UserId) {
|
||||
if (this.accounts.value[userId] != null) {
|
||||
if (userId == null) {
|
||||
// indicates no account is active
|
||||
this.activeAccountId.next(undefined);
|
||||
}
|
||||
|
||||
if (this.accounts.value[userId] == null) {
|
||||
throw new Error("Account does not exist");
|
||||
}
|
||||
this.activeAccountId.next(userId);
|
||||
}
|
||||
|
||||
// TODO: update to use our own account status settings.
|
||||
|
Loading…
Reference in New Issue
Block a user