1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-31 15:43:28 +00:00
Files
browser/libs/common/src/platform/services/memory-storage.service.ts
Matt Gibson a682f2a0ef [PM-5468] Ensure prototypes available on memory stored objects (#7399)
* Hide account switcher in addEdit generator

* Handle AddEditCipher deserialization

* Opaque types are not serializable

* Better handle jsonification of login uris

* Ensure we don't overwrite original with clone

* Ensure cipherView prototype is always restored if it exists
2024-01-02 10:46:45 -05:00

50 lines
1.4 KiB
TypeScript

import { Subject } from "rxjs";
import { AbstractMemoryStorageService, StorageUpdate } from "../abstractions/storage.service";
export class MemoryStorageService extends AbstractMemoryStorageService {
protected store = new Map<string, unknown>();
private updatesSubject = new Subject<StorageUpdate>();
get valuesRequireDeserialization(): boolean {
return false;
}
get updates$() {
return this.updatesSubject.asObservable();
}
get<T>(key: string): Promise<T> {
if (this.store.has(key)) {
const obj = this.store.get(key);
return Promise.resolve(obj as T);
}
return Promise.resolve(null);
}
async has(key: string): Promise<boolean> {
return (await this.get(key)) != null;
}
save<T>(key: string, obj: T): Promise<void> {
if (obj == null) {
return this.remove(key);
}
// TODO: Remove once foreground/background contexts are separated in browser
// Needed to ensure ownership of all memory by the context running the storage service
const toStore = structuredClone(obj);
this.store.set(key, toStore);
this.updatesSubject.next({ key, updateType: "save" });
return Promise.resolve();
}
remove(key: string): Promise<void> {
this.store.delete(key);
this.updatesSubject.next({ key, updateType: "remove" });
return Promise.resolve();
}
getBypassCache<T>(key: string): Promise<T> {
return this.get<T>(key);
}
}