UNPKG

@themineway/smart-storage-js

Version:

A TS/JS library that provides a smart and easy way to store data

52 lines 1.43 kB
export class AConnector { constructor(name, allowsObjectStorage = true) { this.name = name; this.allowsObjectStorage = allowsObjectStorage; /* Events */ this.onChangeEvents = new Map(); } /* Schema */ parse(schema, value) { if (!schema) return value; return schema.parse(value); } /* Accessors */ get(key, schema) { const value = this.rawGet(key); if (value === null) return null; try { return schema ? this.parse(schema, value) : value; } catch { this.remove(key); return null; } } set(key, value, schema) { const parsed = schema ? this.parse(schema, value) : value; this.rawSet(key, parsed); this.triggerOnChange(key); } addOnChangeListener(callback) { const newKey = new Date().getTime() + "_" + Math.random() + "_" + this.onChangeEvents.size; this.onChangeEvents.set(newKey, callback); return () => { this.removeOnChangeListener(newKey); }; } removeOnChangeListener(key) { this.onChangeEvents.delete(key); } triggerOnChange(key) { for (const [, callback] of this.onChangeEvents.entries()) { callback(key); } } } //# sourceMappingURL=connector.abstract.js.map