import { Maybe, Stringified, stringify, parse } from '@dotstats/common'; export class Persistent { private readonly onChange: (value: Data) => void; private readonly key: string; private value: Data; constructor(key: string, initial: Data, onChange: (value: Data) => void) { this.key = key; this.onChange = onChange; const stored = window.localStorage.getItem(key) as Maybe>; if (stored) { this.value = parse(stored); } else { this.value = initial; } window.addEventListener('storage', (event) => { if (event.key === this.key) { this.value = parse(event.newValue as any as Stringified); this.onChange(this.value); } }); } public get(): Data { return this.value; } public set(changes: Pick | Data) { this.value = Object.assign({}, this.value, changes); window.localStorage.setItem(this.key, stringify(this.value) as any as string); this.onChange(this.value); } }