1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-06 00:13:28 +00:00

share disk cache between contexts on browser

This commit is contained in:
Jacob Fink
2023-06-21 09:31:04 -04:00
parent d16f76524c
commit 56a590c491
4 changed files with 14 additions and 13 deletions

View File

@@ -1,4 +1,4 @@
import { BehaviorSubject, Observable } from "rxjs";
import { BehaviorSubject } from "rxjs";
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
import { StateMigrationService } from "@bitwarden/common/platform/abstractions/state-migration.service";
@@ -44,7 +44,7 @@ export class BrowserStateService
stateMigrationService: StateMigrationService,
stateFactory: StateFactory<GlobalState, Account>,
useAccountCache = true,
accountCache: Observable<Record<string, Account>> = null
accountCache: BehaviorSubject<Record<string, Account>> = null
) {
super(
storageService,
@@ -59,7 +59,7 @@ export class BrowserStateService
// Hack to allow shared disk cache between contexts on browser
// TODO: Remove when services are consolidated to a single context
if (useAccountCache && accountCache) {
accountCache.subscribe(this.accountDiskCacheSubject);
this.accountDiskCache = accountCache;
}
}

View File

@@ -444,7 +444,7 @@ function getBgService<T>(service: keyof MainBackground) {
// TODO: we need to figure out a better way of sharing/syncing
// the disk cache
const bgStateService = getBgService<StateServiceAbstraction>("stateService");
const bgDiskCache = bgStateService().accountDiskCache$;
const bgDiskCache = bgStateService().accountDiskCache;
return new BrowserStateService(
storageService,
secureStorageService,

View File

@@ -1,4 +1,4 @@
import { Observable } from "rxjs";
import { BehaviorSubject, Observable } from "rxjs";
import { EncryptedOrganizationKeyData } from "../../admin-console/models/data/encrypted-organization-key.data";
import { OrganizationData } from "../../admin-console/models/data/organization.data";
@@ -37,7 +37,8 @@ export abstract class StateService<T extends Account = Account> {
accounts$: Observable<{ [userId: string]: T }>;
activeAccount$: Observable<string>;
activeAccountUnlocked$: Observable<boolean>;
accountDiskCache$: Observable<Record<string, T>>;
// eslint-disable-next-line rxjs/no-exposed-subjects
accountDiskCache: BehaviorSubject<Record<string, T>>;
addAccount: (account: T) => Promise<void>;
setActiveUser: (userId: string) => Promise<void>;

View File

@@ -95,8 +95,8 @@ export class StateService<
private hasBeenInited = false;
private isRecoveredSession = false;
protected accountDiskCacheSubject = new BehaviorSubject<Record<string, TAccount>>({});
accountDiskCache$ = this.accountDiskCacheSubject.asObservable();
// eslint-disable-next-line rxjs/no-exposed-subjects
accountDiskCache = new BehaviorSubject<Record<string, TAccount>>({});
// default account serializer, must be overridden by child class
protected accountDeserializer = Account.fromJSON as (json: Jsonify<TAccount>) => TAccount;
@@ -2769,7 +2769,7 @@ export class StateService<
}
if (this.useAccountCache) {
const cachedAccount = this.accountDiskCacheSubject.value[options.userId];
const cachedAccount = this.accountDiskCache.value[options.userId];
if (cachedAccount != null) {
return cachedAccount;
}
@@ -3165,15 +3165,15 @@ export class StateService<
private setDiskCache(key: string, value: TAccount, options?: StorageOptions) {
if (this.useAccountCache) {
this.accountDiskCacheSubject.value[key] = value;
this.accountDiskCacheSubject.next(this.accountDiskCacheSubject.value);
this.accountDiskCache.value[key] = value;
this.accountDiskCache.next(this.accountDiskCache.value);
}
}
private deleteDiskCache(key: string) {
if (this.useAccountCache) {
delete this.accountDiskCacheSubject.value[key];
this.accountDiskCacheSubject.next(this.accountDiskCacheSubject.value);
delete this.accountDiskCache.value[key];
this.accountDiskCache.next(this.accountDiskCache.value);
}
}
}