1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-18 01:03:35 +00:00

Resolve state <-> state-test-utils circular dependency (#16093)

* Resolve state <-> state-test-utils circular dependency

* Fix type errors
This commit is contained in:
Justin Baur
2025-08-25 12:38:28 -04:00
committed by GitHub
parent 777b92660a
commit 5f7f1d1924
90 changed files with 543 additions and 500 deletions

View File

@@ -0,0 +1,38 @@
import { Jsonify } from "type-fest";
import { KeyDefinition, StateUpdateOptions, StorageKey } from "@bitwarden/state";
import { AbstractStorageService } from "@bitwarden/storage-core";
export async function getStoredValue<T>(
key: string,
storage: AbstractStorageService,
deserializer: (jsonValue: Jsonify<T>) => T | null,
) {
if (storage.valuesRequireDeserialization) {
const jsonValue = await storage.get<Jsonify<T>>(key);
return deserializer(jsonValue);
} else {
const value = await storage.get<T>(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<unknown>): StorageKey {
return `global_${keyDefinition.stateDefinition.name}_${keyDefinition.key}` as StorageKey;
}
export function populateOptionsWithDefault<T, TCombine>(
options: Partial<StateUpdateOptions<T, TCombine>>,
): StateUpdateOptions<T, TCombine> {
const { combineLatestWith = null, shouldUpdate = () => true, msTimeout = 1000 } = options;
return {
combineLatestWith: combineLatestWith,
shouldUpdate: shouldUpdate,
msTimeout: msTimeout,
};
}