import BaseStorage, { KEY_PREFIX } from './BaseStorage'; export default class BrowserNativeStorage extends BaseStorage { public static get works() { return typeof localStorage !== 'undefined' && localStorage.getItem; } protected async load(name: string, defaultValue: T): Promise { const result = localStorage.getItem(name); if (result === null) { return defaultValue; } try { return JSON.parse(result); } catch { return defaultValue; } } protected async save(name: string, value: T): Promise { localStorage.setItem(name, JSON.stringify(value)); } public async getAll(): Promise> { const result = {}; for (const [key, value] of Object.entries(localStorage)) { if (key.startsWith(KEY_PREFIX)) { try { Object.assign(result, { [key]: JSON.parse(value) }); } catch { // ignored } } } return result; } public async setAll(obj: Record): Promise { for (const [key, value] of Object.entries(obj)) { await this.save(key, value); } } }