mirror of
https://github.com/bitwarden/browser
synced 2025-12-14 07:13:32 +00:00
[ps-2003] Ps/ps 1854 fix pin (#4193)
* Await in `has` calls. * Add disk cache to browser synced items Note: `Map` doesn't serialize nicely so it's easier to swap over to a `Record` object for out cache * Mock and await init promises in tests * Remove redundant settings checks
This commit is contained in:
@@ -18,7 +18,7 @@ export class MemoryStorageService
|
||||
}
|
||||
|
||||
async has(key: string): Promise<boolean> {
|
||||
return this.get(key) != null;
|
||||
return (await this.get(key)) != null;
|
||||
}
|
||||
|
||||
save(key: string, obj: any): Promise<any> {
|
||||
|
||||
@@ -79,7 +79,7 @@ export class StateService<
|
||||
private hasBeenInited = false;
|
||||
private isRecoveredSession = false;
|
||||
|
||||
private accountDiskCache = new Map<string, TAccount>();
|
||||
protected accountDiskCache = new BehaviorSubject<Record<string, TAccount>>({});
|
||||
|
||||
// default account serializer, must be overridden by child class
|
||||
protected accountDeserializer = Account.fromJSON as (json: Jsonify<TAccount>) => TAccount;
|
||||
@@ -2383,7 +2383,7 @@ export class StateService<
|
||||
}
|
||||
|
||||
if (this.useAccountCache) {
|
||||
const cachedAccount = this.accountDiskCache.get(options.userId);
|
||||
const cachedAccount = this.accountDiskCache.value[options.userId];
|
||||
if (cachedAccount != null) {
|
||||
return cachedAccount;
|
||||
}
|
||||
@@ -2397,9 +2397,7 @@ export class StateService<
|
||||
))
|
||||
: await this.storageService.get<TAccount>(options.userId, options);
|
||||
|
||||
if (this.useAccountCache) {
|
||||
this.accountDiskCache.set(options.userId, account);
|
||||
}
|
||||
this.setDiskCache(options.userId, account);
|
||||
return account;
|
||||
}
|
||||
|
||||
@@ -2430,9 +2428,7 @@ export class StateService<
|
||||
|
||||
await storageLocation.save(`${options.userId}`, account, options);
|
||||
|
||||
if (this.useAccountCache) {
|
||||
this.accountDiskCache.delete(options.userId);
|
||||
}
|
||||
this.deleteDiskCache(options.userId);
|
||||
}
|
||||
|
||||
protected async saveAccountToMemory(account: TAccount): Promise<void> {
|
||||
@@ -2643,9 +2639,7 @@ export class StateService<
|
||||
userId = userId ?? state.activeUserId;
|
||||
delete state.accounts[userId];
|
||||
|
||||
if (this.useAccountCache) {
|
||||
this.accountDiskCache.delete(userId);
|
||||
}
|
||||
this.deleteDiskCache(userId);
|
||||
|
||||
return state;
|
||||
});
|
||||
@@ -2770,6 +2764,20 @@ export class StateService<
|
||||
await this.setState(updatedState);
|
||||
});
|
||||
}
|
||||
|
||||
private setDiskCache(key: string, value: TAccount, options?: StorageOptions) {
|
||||
if (this.useAccountCache) {
|
||||
this.accountDiskCache.value[key] = value;
|
||||
this.accountDiskCache.next(this.accountDiskCache.value);
|
||||
}
|
||||
}
|
||||
|
||||
private deleteDiskCache(key: string) {
|
||||
if (this.useAccountCache) {
|
||||
delete this.accountDiskCache.value[key];
|
||||
this.accountDiskCache.next(this.accountDiskCache.value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function withPrototypeForArrayMembers<T>(
|
||||
|
||||
Reference in New Issue
Block a user