import { Jsonify } from "type-fest"; import { KeyDefinition, StateUpdateOptions, StorageKey } from "@bitwarden/state"; import { AbstractStorageService } from "@bitwarden/storage-core"; export async function getStoredValue( key: string, storage: AbstractStorageService, deserializer: (jsonValue: Jsonify) => T | null, ) { if (storage.valuesRequireDeserialization) { const jsonValue = await storage.get>(key); return deserializer(jsonValue); } else { const value = await storage.get(key); return value ?? null; } } /** * Creates a {@link StorageKey} * @param keyDefinition The key definition of which data the key should point to. * @returns A key that is ready to be used in a storage service to get data. */ export function globalKeyBuilder(keyDefinition: KeyDefinition): StorageKey { return `global_${keyDefinition.stateDefinition.name}_${keyDefinition.key}` as StorageKey; } export function populateOptionsWithDefault( options: Partial>, ): StateUpdateOptions { const { combineLatestWith = null, shouldUpdate = () => true, msTimeout = 1000 } = options; return { combineLatestWith: combineLatestWith, shouldUpdate: shouldUpdate, msTimeout: msTimeout, }; }