mirror of
https://github.com/bitwarden/browser.git
synced 2025-02-15 01:11:47 +01:00
Name parameters after direction of change
Serializers create writable representations, deserializers read those representations and build objects
This commit is contained in:
parent
f0b16d0baf
commit
21046fd856
@ -103,7 +103,7 @@ class DefaultActiveUserState<T> implements ActiveUserState<T> {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
const jsonData = await this.chosenStorageLocation.get<Jsonify<T>>(key);
|
const jsonData = await this.chosenStorageLocation.get<Jsonify<T>>(key);
|
||||||
const data = keyDefinition.serializer(jsonData);
|
const data = keyDefinition.deserializer(jsonData);
|
||||||
return data;
|
return data;
|
||||||
}),
|
}),
|
||||||
tap((data) => {
|
tap((data) => {
|
||||||
@ -149,7 +149,7 @@ class DefaultActiveUserState<T> implements ActiveUserState<T> {
|
|||||||
}
|
}
|
||||||
const key = userKeyBuilder(activeUser.id, this.keyDefinition);
|
const key = userKeyBuilder(activeUser.id, this.keyDefinition);
|
||||||
const data = (await this.chosenStorageLocation.get(key)) as Jsonify<T>;
|
const data = (await this.chosenStorageLocation.get(key)) as Jsonify<T>;
|
||||||
return this.keyDefinition.serializer(data);
|
return this.keyDefinition.deserializer(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
createDerived<TTo>(
|
createDerived<TTo>(
|
||||||
@ -169,7 +169,7 @@ class DefaultActiveUserState<T> implements ActiveUserState<T> {
|
|||||||
private async seedInitial(key: string): Promise<T> {
|
private async seedInitial(key: string): Promise<T> {
|
||||||
const data = await this.chosenStorageLocation.get<Jsonify<T>>(key);
|
const data = await this.chosenStorageLocation.get<Jsonify<T>>(key);
|
||||||
this.seededInitial = true;
|
this.seededInitial = true;
|
||||||
return this.keyDefinition.serializer(data);
|
return this.keyDefinition.deserializer(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
private chooseStorage(storageLocation: StorageLocation): AbstractStorageService {
|
private chooseStorage(storageLocation: StorageLocation): AbstractStorageService {
|
||||||
|
@ -28,7 +28,7 @@ class GlobalStateImplementation<T> implements GlobalState<T> {
|
|||||||
this.storageKey = globalKeyBuilder(this.keyDefinition);
|
this.storageKey = globalKeyBuilder(this.keyDefinition);
|
||||||
|
|
||||||
this.seededPromise = this.chosenLocation.get<Jsonify<T>>(this.storageKey).then((data) => {
|
this.seededPromise = this.chosenLocation.get<Jsonify<T>>(this.storageKey).then((data) => {
|
||||||
const serializedData = this.keyDefinition.serializer(data);
|
const serializedData = this.keyDefinition.deserializer(data);
|
||||||
this.stateSubject.next(serializedData);
|
this.stateSubject.next(serializedData);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -45,7 +45,7 @@ class GlobalStateImplementation<T> implements GlobalState<T> {
|
|||||||
|
|
||||||
async getFromState(): Promise<T> {
|
async getFromState(): Promise<T> {
|
||||||
const data = await this.chosenLocation.get<Jsonify<T>>(this.storageKey);
|
const data = await this.chosenLocation.get<Jsonify<T>>(this.storageKey);
|
||||||
return this.keyDefinition.serializer(data);
|
return this.keyDefinition.deserializer(data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,34 +12,34 @@ export class KeyDefinition<T> {
|
|||||||
* Creates a new instance of a KeyDefinition
|
* Creates a new instance of a KeyDefinition
|
||||||
* @param stateDefinition The state definition for which this key belongs to.
|
* @param stateDefinition The state definition for which this key belongs to.
|
||||||
* @param key The name of the key, this should be unique per domain
|
* @param key The name of the key, this should be unique per domain
|
||||||
* @param serializer A function to use to safely convert your type from json to your expected type.
|
* @param deserializer A function to use to safely convert your type from json to your expected type.
|
||||||
*/
|
*/
|
||||||
constructor(
|
constructor(
|
||||||
readonly stateDefinition: StateDefinition,
|
readonly stateDefinition: StateDefinition,
|
||||||
readonly key: string,
|
readonly key: string,
|
||||||
readonly serializer: (jsonValue: Jsonify<T>) => T
|
readonly deserializer: (jsonValue: Jsonify<T>) => T
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
static array<T>(
|
static array<T>(
|
||||||
stateDefinition: StateDefinition,
|
stateDefinition: StateDefinition,
|
||||||
key: string,
|
key: string,
|
||||||
serializer: (jsonValue: Jsonify<T>) => T
|
deserializer: (jsonValue: Jsonify<T>) => T
|
||||||
) {
|
) {
|
||||||
return new KeyDefinition<T[]>(stateDefinition, key, (jsonValue) => {
|
return new KeyDefinition<T[]>(stateDefinition, key, (jsonValue) => {
|
||||||
// TODO: Should we handle null for them, I feel like we should discourage null for an array?
|
// TODO: Should we handle null for them, I feel like we should discourage null for an array?
|
||||||
return jsonValue.map((v) => serializer(v));
|
return jsonValue.map((v) => deserializer(v));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
static record<T>(
|
static record<T>(
|
||||||
stateDefinition: StateDefinition,
|
stateDefinition: StateDefinition,
|
||||||
key: string,
|
key: string,
|
||||||
serializer: (jsonValue: Jsonify<T>) => T
|
deserializer: (jsonValue: Jsonify<T>) => T
|
||||||
) {
|
) {
|
||||||
return new KeyDefinition<Record<string, T>>(stateDefinition, key, (jsonValue) => {
|
return new KeyDefinition<Record<string, T>>(stateDefinition, key, (jsonValue) => {
|
||||||
const output: Record<string, T> = {};
|
const output: Record<string, T> = {};
|
||||||
for (const key in jsonValue) {
|
for (const key in jsonValue) {
|
||||||
output[key] = serializer((jsonValue as Record<string, Jsonify<T>>)[key]);
|
output[key] = deserializer((jsonValue as Record<string, Jsonify<T>>)[key]);
|
||||||
}
|
}
|
||||||
return output;
|
return output;
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user