From 56a590c4919f119cb1465eb7091a4384f5d90699 Mon Sep 17 00:00:00 2001 From: Jacob Fink Date: Wed, 21 Jun 2023 09:31:04 -0400 Subject: [PATCH] share disk cache between contexts on browser --- .../src/platform/services/browser-state.service.ts | 6 +++--- apps/browser/src/popup/services/services.module.ts | 2 +- .../src/platform/abstractions/state.service.ts | 5 +++-- libs/common/src/platform/services/state.service.ts | 14 +++++++------- 4 files changed, 14 insertions(+), 13 deletions(-) diff --git a/apps/browser/src/platform/services/browser-state.service.ts b/apps/browser/src/platform/services/browser-state.service.ts index 9d6b770457..9d7e8ec0c4 100644 --- a/apps/browser/src/platform/services/browser-state.service.ts +++ b/apps/browser/src/platform/services/browser-state.service.ts @@ -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, useAccountCache = true, - accountCache: Observable> = null + accountCache: BehaviorSubject> = 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; } } diff --git a/apps/browser/src/popup/services/services.module.ts b/apps/browser/src/popup/services/services.module.ts index e71bdd1c08..b49de68337 100644 --- a/apps/browser/src/popup/services/services.module.ts +++ b/apps/browser/src/popup/services/services.module.ts @@ -444,7 +444,7 @@ function getBgService(service: keyof MainBackground) { // TODO: we need to figure out a better way of sharing/syncing // the disk cache const bgStateService = getBgService("stateService"); - const bgDiskCache = bgStateService().accountDiskCache$; + const bgDiskCache = bgStateService().accountDiskCache; return new BrowserStateService( storageService, secureStorageService, diff --git a/libs/common/src/platform/abstractions/state.service.ts b/libs/common/src/platform/abstractions/state.service.ts index a7b1d8928d..9335d505a8 100644 --- a/libs/common/src/platform/abstractions/state.service.ts +++ b/libs/common/src/platform/abstractions/state.service.ts @@ -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 { accounts$: Observable<{ [userId: string]: T }>; activeAccount$: Observable; activeAccountUnlocked$: Observable; - accountDiskCache$: Observable>; + // eslint-disable-next-line rxjs/no-exposed-subjects + accountDiskCache: BehaviorSubject>; addAccount: (account: T) => Promise; setActiveUser: (userId: string) => Promise; diff --git a/libs/common/src/platform/services/state.service.ts b/libs/common/src/platform/services/state.service.ts index 43fd5587f0..b7b587bd45 100644 --- a/libs/common/src/platform/services/state.service.ts +++ b/libs/common/src/platform/services/state.service.ts @@ -95,8 +95,8 @@ export class StateService< private hasBeenInited = false; private isRecoveredSession = false; - protected accountDiskCacheSubject = new BehaviorSubject>({}); - accountDiskCache$ = this.accountDiskCacheSubject.asObservable(); + // eslint-disable-next-line rxjs/no-exposed-subjects + accountDiskCache = new BehaviorSubject>({}); // default account serializer, must be overridden by child class protected accountDeserializer = Account.fromJSON as (json: Jsonify) => 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); } } }