@naverpay/vanilla-store
Version:
  
35 lines (34 loc) • 887 B
JavaScript
;
const type = require("./type.js");
class LocalStoragePersist extends type.Persistent {
get serialized() {
return window.localStorage.getItem(this.key);
}
get value() {
const storageValue = window.localStorage.getItem(this.key);
if (!type.isSerializeValue(storageValue)) {
return this._value;
}
const parsedValue = storageValue ? JSON.parse(storageValue) : null;
if (!this.typeAssertion(parsedValue)) {
return this._value;
}
if (this._value !== parsedValue) {
this._value = parsedValue;
}
return this._value;
}
set value(value) {
if (this.typeAssertion(value)) {
this._value = value;
window.localStorage.setItem(this.key, JSON.stringify(value));
}
}
clear() {
try {
window.localStorage.removeItem(this.key);
} catch {
}
}
}
module.exports = LocalStoragePersist;