svas 1.2.0 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/collection.d.ts +1 -0
- package/dist/collection.js +21 -3
- package/dist/combined.js +1 -1
- package/dist/value.js +9 -1
- package/dist/values.js +12 -3
- package/package.json +1 -1
package/dist/collection.d.ts
CHANGED
package/dist/collection.js
CHANGED
|
@@ -105,9 +105,8 @@ export class Collection {
|
|
|
105
105
|
persist(key) {
|
|
106
106
|
if (typeof window === 'undefined')
|
|
107
107
|
return;
|
|
108
|
-
const
|
|
109
|
-
if (
|
|
110
|
-
const items = JSON.parse(stored);
|
|
108
|
+
const items = this.load(key);
|
|
109
|
+
if (items !== null) {
|
|
111
110
|
this.store.set(items);
|
|
112
111
|
if (this.values?.persistent === false)
|
|
113
112
|
for (const item of items)
|
|
@@ -122,6 +121,25 @@ export class Collection {
|
|
|
122
121
|
localStorage.setItem(key, JSON.stringify(items));
|
|
123
122
|
});
|
|
124
123
|
}
|
|
124
|
+
load(key) {
|
|
125
|
+
if (typeof window === 'undefined')
|
|
126
|
+
return null;
|
|
127
|
+
const stored = localStorage.getItem(key);
|
|
128
|
+
if (stored === null)
|
|
129
|
+
return null;
|
|
130
|
+
try {
|
|
131
|
+
const items = JSON.parse(stored);
|
|
132
|
+
if (!Array.isArray(items)) {
|
|
133
|
+
console.error(`Storage value for ${key} is not an array:`, items);
|
|
134
|
+
return null;
|
|
135
|
+
}
|
|
136
|
+
return items;
|
|
137
|
+
}
|
|
138
|
+
catch (error) {
|
|
139
|
+
console.error(`Invalid storage value for ${key}`, error);
|
|
140
|
+
return null;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
125
143
|
bind(store) {
|
|
126
144
|
store.subscribe((value) => {
|
|
127
145
|
if (value === null)
|
package/dist/combined.js
CHANGED
package/dist/value.js
CHANGED
|
@@ -48,5 +48,13 @@ function persistent(key, def, session) {
|
|
|
48
48
|
}
|
|
49
49
|
function load(key) {
|
|
50
50
|
const json = window.localStorage.getItem(key);
|
|
51
|
-
|
|
51
|
+
if (json === null)
|
|
52
|
+
return null;
|
|
53
|
+
try {
|
|
54
|
+
return JSON.parse(json);
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
console.error(`Invalid storage value for ${key}`, error);
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
52
60
|
}
|
package/dist/values.js
CHANGED
|
@@ -120,9 +120,18 @@ class Values {
|
|
|
120
120
|
const data = localStorage.getItem(this.persist);
|
|
121
121
|
if (data === null)
|
|
122
122
|
return;
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
123
|
+
try {
|
|
124
|
+
const values = JSON.parse(data);
|
|
125
|
+
if (values === null || typeof values !== 'object') {
|
|
126
|
+
console.error(`Storage value for ${this.persist} is not an object:`, values);
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
for (const [key, value] of Object.entries(values))
|
|
130
|
+
this.set(key, value);
|
|
131
|
+
}
|
|
132
|
+
catch (error) {
|
|
133
|
+
console.error(`Invalid storage value for ${this.persist}`, error);
|
|
134
|
+
}
|
|
126
135
|
}
|
|
127
136
|
bind(store) {
|
|
128
137
|
store.subscribe((value) => {
|