Browse Source

share disk cache between contexts on browser

pull/5884/head
Jacob Fink 3 years ago
parent
commit
56a590c491
No known key found for this signature in database
GPG Key ID: C2F7ACF05859D008
  1. 6
      apps/browser/src/platform/services/browser-state.service.ts
  2. 2
      apps/browser/src/popup/services/services.module.ts
  3. 5
      libs/common/src/platform/abstractions/state.service.ts
  4. 14
      libs/common/src/platform/services/state.service.ts

6
apps/browser/src/platform/services/browser-state.service.ts

@ -1,4 +1,4 @@ @@ -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 @@ -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 @@ -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;
}
}

2
apps/browser/src/popup/services/services.module.ts

@ -444,7 +444,7 @@ function getBgService<T>(service: keyof MainBackground) { @@ -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,

5
libs/common/src/platform/abstractions/state.service.ts

@ -1,4 +1,4 @@ @@ -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> { @@ -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>;

14
libs/common/src/platform/services/state.service.ts

@ -95,8 +95,8 @@ export class StateService< @@ -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< @@ -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< @@ -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);
}
}
}

Loading…
Cancel
Save