template/src/main/js/Storage.ts

202 lines
5.0 KiB
TypeScript

/**
* Stores key-value pairs.
*/
export interface Storage {
/**
* Removes the data from storage.
*/
clear(): void;
/**
* Retrieves an array from storage.
*
* @param name the name of the array to retrieve
* @param def the value to return if no array is stored with the given name
*/
getArray(name: string, def: any[]): any[];
/**
* Stores an array.
*
* @param name the name of the array to store
* @param value the array to store under the given name
*/
setArray(name: string, value: any[]): void;
/**
* Retrieves a boolean from storage.
*
* @param name the name of the boolean to retrieve
* @param def the value to return if no boolean is stored with the given name
*/
getBoolean(name: string, def: boolean): boolean;
/**
* Stores a boolean.
*
* @param name the name of the boolean to store
* @param value the boolean to store under the given name
*/
setBoolean(name: string, value: boolean): void;
/**
* Retrieves a number from storage.
*
* @param name the name of the number to retrieve
* @param def the value to return if no number is stored with the given name
*/
getNumber(name: string, def: number): number;
/**
* Stores a number.
*
* @param name the name of the number to store
* @param value the number to store under the given name
*/
setNumber(name: string, value: number): void;
/**
* Retrieves a string from storage.
*
* @param name the name of the string to retrieve
* @param def the value to return if no string is stored with the given name
*/
getString(name: string, def: string): string;
/**
* Stores a string.
*
* @param name the name of the string to store
* @param value the number to store under the given name
*/
setString(name: string, value: string): void;
}
/**
* Stores key-value pairs in a single entry in `localStorage`.
*/
export class LocalStorage implements Storage {
private readonly key: string;
private cache: { [key: string]: string } | null = null;
/**
* Constructs a new persistent storage item under the given key.
*
* @param key the unique identifier to store the data under
*/
constructor(key: string) {
this.key = key;
}
/**
* Reads the object stored in local storage.
*
* @return the object stored in local storage, or an empty object if there is nothing in the local storage
* @private
*/
private read(): { [key: string]: string } {
if (this.cache === null)
this.cache = JSON.parse(localStorage.getItem(this.key) ?? "{}");
return this.cache!;
}
/**
* Writes the given object to local storage.
*
* @param item the object to write to local storage
* @private
*/
private write(item: { [key: string]: string }): void {
this.cache = item;
localStorage.setItem(this.key, JSON.stringify(item));
}
clear(): void {
this.cache = null;
localStorage.removeItem(this.key);
}
getArray(name: string, def: any[] = []): any[] {
const array = this.read()[name];
return array === undefined ? def : JSON.parse(array);
}
setArray(name: string, value: any[]): void {
this.setString(name, JSON.stringify(value));
}
getBoolean(name: string, def: boolean = false): boolean {
return this.getString(name, def ? "true" : "false") === "true";
}
setBoolean(name: string, value: boolean): void {
this.setString(name, "" + value);
}
getNumber(name: string, def: number = 0): number {
return +this.getString(name, "" + def);
}
setNumber(name: string, value: number): void {
this.setString(name, "" + value);
}
getString(name: string, def: string = ""): string {
return this.read()[name] ?? def;
}
setString(name: string, value: string): void {
const item = this.read();
item[name] = value;
this.write(item);
}
}
/**
* Stores key-value pairs in an object.
*/
export class MemoryStorage implements Storage {
private storage: { [key: string]: any } = {};
clear(): void {
this.storage = {};
}
setArray(name: string, value: any[] = []): void {
this.storage[name] = value;
}
getArray(name: string, def: any[]): any[] {
return this.storage[name] ?? def;
}
setBoolean(name: string, value: boolean): void {
this.storage[name] = value;
}
getBoolean(name: string, def: boolean): boolean {
return this.storage[name] ?? def;
}
setNumber(name: string, value: number): void {
this.storage[name] = value;
}
getNumber(name: string, def: number): number {
return this.storage[name] ?? def;
}
setString(name: string, value: string): void {
this.storage[name] = value;
}
getString(name: string, def: string): string {
return this.storage[name] ?? def;
}
}