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.
@@ -21,6 +21,7 @@ export declare class Collection<T extends Identifiable, E extends Error = Error>
21
21
  replace(items: T[]): void;
22
22
  private refresh;
23
23
  private persist;
24
+ private load;
24
25
  private bind;
25
26
  private clear;
26
27
  }
@@ -105,9 +105,8 @@ export class Collection {
105
105
  persist(key) {
106
106
  if (typeof window === 'undefined')
107
107
  return;
108
- const stored = localStorage.getItem(key);
109
- if (stored !== null) {
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
@@ -7,6 +7,6 @@ export function combined(...stores) {
7
7
  else if (values.some((value) => value === null))
8
8
  set(null);
9
9
  else
10
- set(values);
10
+ set([...values]);
11
11
  });
12
12
  }
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
- return json === null ? null : JSON.parse(json);
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
- const values = JSON.parse(data);
124
- for (const [key, value] of Object.entries(values))
125
- this.set(key, value);
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) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svas",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "repository": "https://github.com/temich/svas",
5
5
  "scripts": {
6
6
  "dev": "vite dev",