1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/dashboard/client/utils/createStorage.ts
Jari Kolehmainen 1d0815abd2
Lens app source code (#119)
Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>
2020-03-15 09:52:02 +02:00

68 lines
1.6 KiB
TypeScript
Executable File

// Helper to work with browser's local/session storage api
export interface IStorageHelperOptions {
addKeyPrefix?: boolean;
useSession?: boolean; // use `sessionStorage` instead of `localStorage`
}
export function createStorage<T>(key: string, defaultValue?: T, options?: IStorageHelperOptions) {
return new StorageHelper(key, defaultValue, options);
}
export class StorageHelper<T> {
static keyPrefix = "lens_";
static defaultOptions: IStorageHelperOptions = {
addKeyPrefix: true,
useSession: false,
}
constructor(protected key: string, protected defaultValue?: T, protected options?: IStorageHelperOptions) {
this.options = Object.assign({}, StorageHelper.defaultOptions, options);
if (this.options.addKeyPrefix) {
this.key = StorageHelper.keyPrefix + key;
}
}
protected get storage() {
if (this.options.useSession) return window.sessionStorage;
return window.localStorage;
}
get(): T {
const strValue = this.storage.getItem(this.key);
if (strValue != null) {
try {
return JSON.parse(strValue);
} catch (e) {
console.error(`Parsing json failed for pair: ${this.key}=${strValue}`)
}
}
return this.defaultValue;
}
set(value: T) {
this.storage.setItem(this.key, JSON.stringify(value));
return this;
}
merge(value: Partial<T>) {
const currentValue = this.get();
return this.set(Object.assign(currentValue, value));
}
clear() {
this.storage.removeItem(this.key);
return this;
}
getDefaultValue() {
return this.defaultValue;
}
restoreDefaultValue() {
return this.set(this.defaultValue);
}
}