2021-12-19 23:03:46 +00:00
|
|
|
import BaseStorage from './BaseStorage';
|
|
|
|
|
|
|
|
export default class BrowserNativeStorage extends BaseStorage {
|
2021-12-20 22:19:44 +00:00
|
|
|
public static get works() {
|
|
|
|
return typeof localStorage !== 'undefined' && localStorage.getItem;
|
|
|
|
}
|
|
|
|
|
2021-12-19 23:03:46 +00:00
|
|
|
protected async load<T>(name: string, defaultValue: T): Promise<T> {
|
|
|
|
const result = localStorage.getItem(name);
|
|
|
|
if (result === null) {
|
|
|
|
return defaultValue;
|
|
|
|
}
|
|
|
|
return JSON.parse(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected async save<T>(name: string, value: T): Promise<void> {
|
|
|
|
localStorage.setItem(name, JSON.stringify(value));
|
|
|
|
}
|
2021-12-20 22:19:44 +00:00
|
|
|
|
|
|
|
public async getAll(): Promise<Record<string, any>> {
|
|
|
|
const result = {};
|
|
|
|
for (const [key, value] of Object.entries(localStorage)) {
|
|
|
|
Object.assign(result, { [key]: JSON.parse(value) });
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async setAll(obj: Record<string, any>): Promise<void> {
|
|
|
|
for (const [key, value] of Object.entries(obj)) {
|
|
|
|
localStorage.setItem(key, JSON.stringify(value));
|
|
|
|
}
|
|
|
|
}
|
2021-12-19 23:03:46 +00:00
|
|
|
}
|