[bug] Allow for toggling the account cache (#674)

* [bug] Allow for toggling the account cache

* Add missing conditional

* Ran prettier
This commit is contained in:
Addison Beck 2022-02-14 11:47:01 -05:00 committed by GitHub
parent 609baece05
commit 240fc154ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 7 deletions

View File

@ -76,7 +76,8 @@ export class StateService<
protected secureStorageService: StorageService, protected secureStorageService: StorageService,
protected logService: LogService, protected logService: LogService,
protected stateMigrationService: StateMigrationService, protected stateMigrationService: StateMigrationService,
protected stateFactory: StateFactory<TGlobalState, TAccount> protected stateFactory: StateFactory<TGlobalState, TAccount>,
protected useAccountCache: boolean = true
) { ) {
this.accountDiskCache = new Map<string, TAccount>(); this.accountDiskCache = new Map<string, TAccount>();
} }
@ -2163,10 +2164,12 @@ export class StateService<
return null; return null;
} }
if (this.useAccountCache) {
const cachedAccount = this.accountDiskCache.get(options.userId); const cachedAccount = this.accountDiskCache.get(options.userId);
if (cachedAccount != null) { if (cachedAccount != null) {
return cachedAccount; return cachedAccount;
} }
}
const account = options?.useSecureStorage const account = options?.useSecureStorage
? (await this.secureStorageService.get<TAccount>(options.userId, options)) ?? ? (await this.secureStorageService.get<TAccount>(options.userId, options)) ??
@ -2176,7 +2179,9 @@ export class StateService<
)) ))
: await this.storageService.get<TAccount>(options.userId, options); : await this.storageService.get<TAccount>(options.userId, options);
if (this.useAccountCache) {
this.accountDiskCache.set(options.userId, account); this.accountDiskCache.set(options.userId, account);
}
return account; return account;
} }
@ -2206,8 +2211,11 @@ export class StateService<
: this.storageService; : this.storageService;
await storageLocation.save(`${options.userId}`, account, options); await storageLocation.save(`${options.userId}`, account, options);
if (this.useAccountCache) {
this.accountDiskCache.delete(options.userId); this.accountDiskCache.delete(options.userId);
} }
}
protected async saveAccountToMemory(account: TAccount): Promise<void> { protected async saveAccountToMemory(account: TAccount): Promise<void> {
if (this.getAccountFromMemory({ userId: account.profile.userId }) !== null) { if (this.getAccountFromMemory({ userId: account.profile.userId }) !== null) {
@ -2407,8 +2415,10 @@ export class StateService<
protected removeAccountFromMemory(userId: string = this.state.activeUserId): void { protected removeAccountFromMemory(userId: string = this.state.activeUserId): void {
delete this.state.accounts[userId]; delete this.state.accounts[userId];
if (this.useAccountCache) {
this.accountDiskCache.delete(userId); this.accountDiskCache.delete(userId);
} }
}
protected async pruneInMemoryAccounts() { protected async pruneInMemoryAccounts() {
// We preserve settings for logged out accounts, but we don't want to consider them when thinking about active account state // We preserve settings for logged out accounts, but we don't want to consider them when thinking about active account state