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:
38
libs/state-internal/src/util.ts
Normal file
38
libs/state-internal/src/util.ts
Normal 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,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user