@friendofsvelte/state 0.0.6 → 0.2.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Friend Of Svelte
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -25,40 +25,45 @@ npm install @friendofsvelte/state
25
25
  // new.svelte.ts / js
26
26
  import { PersistentState } from '@friendofsvelte/state';
27
27
 
28
- export const box = new PersistentState('box', {
29
- color: '#ff3e00',
30
- dimensions: [100, 100]
31
- }, 'sessionStorage');
28
+ export const box = new PersistentState(
29
+ 'box',
30
+ {
31
+ color: '#ff3e00',
32
+ dimensions: [100, 100]
33
+ },
34
+ 'sessionStorage'
35
+ );
32
36
  ```
33
37
 
34
38
  2. Use in your components:
35
39
 
36
40
  ```svelte
37
41
  <script lang="ts">
38
- import { box } from '$lib/new.svelte';
39
-
40
- const listColors = ['red', 'blue', 'green', 'yellow', 'purple', 'orange', 'pink', 'brown'];
41
-
42
- function switchNextColor() {
43
- const currentIndex = listColors.indexOf(box.current.color);
44
- const nextIndex = currentIndex + 1;
45
- if (nextIndex >= listColors.length) {
46
- box.current.color = listColors[0];
47
- } else {
48
- box.current.color = listColors[nextIndex];
49
- }
50
- }
42
+ import { box } from '$lib/new.svelte';
43
+
44
+ const listColors = ['red', 'blue', 'green', 'yellow', 'purple', 'orange', 'pink', 'brown'];
45
+
46
+ function switchNextColor() {
47
+ const currentIndex = listColors.indexOf(box.current.color);
48
+ const nextIndex = currentIndex + 1;
49
+ if (nextIndex >= listColors.length) {
50
+ box.current.color = listColors[0];
51
+ } else {
52
+ box.current.color = listColors[nextIndex];
53
+ }
54
+ }
51
55
  </script>
52
56
 
53
57
  <div
54
- style="background-color: {box.current.color}; width: 100px; height: 100px; color: gray; text-align: center;"
55
- class="m-2 rounded-2xl"
58
+ style="background-color: {box.current
59
+ .color}; width: 100px; height: 100px; color: gray; text-align: center;"
60
+ class="m-2 rounded-2xl"
56
61
  >
57
- {box.current.color}
62
+ {box.current.color}
58
63
  </div>
59
64
 
60
- <button onclick={switchNextColor} class="bg-gray-700 m-2 px-3 rounded-2xl text-gray-200">
61
- Change color
65
+ <button onclick={switchNextColor} class="m-2 rounded-2xl bg-gray-700 px-3 text-gray-200">
66
+ Change color
62
67
  </button>
63
68
  ```
64
69
 
@@ -69,15 +74,16 @@ export const box = new PersistentState('box', {
69
74
  Creates or retrieves a persistent state container.
70
75
 
71
76
  Parameters:
77
+
72
78
  - `key`: Unique identifier for the state container
73
79
  - `initial`: (Optional) Initial state value
74
80
  - `storageType`: (Optional) Storage type - 'localStorage' or 'sessionStorage' (default: 'localStorage')
75
81
 
76
82
  Returns:
77
- - A reactive state object of type `T`
78
83
 
79
- > Inspired by: Rich-Harris' [local-storage-test](https://github.com/Rich-Harris/local-storage-test/blob/main/src/lib/storage.svelte.ts)
84
+ - A reactive state object of type `T`
80
85
 
86
+ > Based on, Rich-Harris' [local-storage-test](https://github.com/Rich-Harris/local-storage-test/blob/main/src/lib/storage.svelte.ts)
81
87
 
82
88
  ## Examples
83
89
 
@@ -85,26 +91,34 @@ Returns:
85
91
 
86
92
  ```svelte
87
93
  <script lang="ts">
88
- import { PersistentState } from '@friendofsvelte/state';
89
-
90
- const box = new PersistentState('box', {
91
- color: '#ff3e00',
92
- dimensions: [100, 100]
93
- }, 'sessionStorage');
94
-
95
- function switchNextColor() {
96
- const colors = ['red', 'blue', 'green', 'yellow', 'purple', 'orange', 'pink', 'brown'];
97
- const currentIndex = colors.indexOf(box.current.color);
98
- box.current.color = colors[(currentIndex + 1) % colors.length];
99
- }
94
+ import { PersistentState } from '@friendofsvelte/state';
95
+
96
+ const box = new PersistentState(
97
+ 'box',
98
+ {
99
+ color: '#ff3e00',
100
+ dimensions: [100, 100]
101
+ },
102
+ 'sessionStorage'
103
+ );
104
+
105
+ function switchNextColor() {
106
+ const colors = ['red', 'blue', 'green', 'yellow', 'purple', 'orange', 'pink', 'brown'];
107
+ const currentIndex = colors.indexOf(box.current.color);
108
+ box.current.color = colors[(currentIndex + 1) % colors.length];
109
+ }
100
110
  </script>
101
111
 
102
- <div style="background-color: {box.current.color}; width: 100px; height: 100px; color: gray; text-align: center;" class="m-2 rounded-2xl">
103
- {box.current.color}
112
+ <div
113
+ style="background-color: {box.current
114
+ .color}; width: 100px; height: 100px; color: gray; text-align: center;"
115
+ class="m-2 rounded-2xl"
116
+ >
117
+ {box.current.color}
104
118
  </div>
105
119
 
106
- <button onclick={switchNextColor} class="bg-gray-700 m-2 px-3 rounded-2xl text-gray-200">
107
- Change color
120
+ <button onclick={switchNextColor} class="m-2 rounded-2xl bg-gray-700 px-3 text-gray-200">
121
+ Change color
108
122
  </button>
109
123
  ```
110
124
 
@@ -1,4 +1,4 @@
1
- import { PersistentState } from "./storage.svelte.js";
1
+ import { PersistentState } from './storage.svelte.js';
2
2
  export declare const box: PersistentState<{
3
3
  color: string;
4
4
  dimensions: number[];
@@ -1,4 +1,4 @@
1
- import { PersistentState } from "./storage.svelte.js";
1
+ import { PersistentState } from './storage.svelte.js';
2
2
  export const box = new PersistentState('box', {
3
3
  color: '#ff3e00',
4
4
  dimensions: [100, 100]
@@ -2,6 +2,6 @@ export type StorageType = 'localStorage' | 'sessionStorage';
2
2
  export declare class PersistentState<T> {
3
3
  #private;
4
4
  constructor(key: string, initial?: T, storageType?: StorageType);
5
- get current(): any;
6
- set current(value: any);
5
+ get current(): T;
6
+ set current(value: T);
7
7
  }
@@ -1,4 +1,4 @@
1
- // See our inspiration: https://github.com/Rich-Harris/local-storage-test/blob/main/src/lib/storage.svelte.ts
1
+ // Based on: https://github.com/Rich-Harris/local-storage-test/blob/main/src/lib/storage.svelte.ts
2
2
  import { tick } from 'svelte';
3
3
  export class PersistentState {
4
4
  #key;
@@ -14,20 +14,43 @@ export class PersistentState {
14
14
  this.#version += 1;
15
15
  };
16
16
  constructor(key, initial, storageType = 'localStorage') {
17
+ if (!key) {
18
+ throw new Error('PersistentState: key must be a non-empty string');
19
+ }
17
20
  this.#key = key;
18
21
  this.#value = initial;
19
- this.#storage = storageType === 'localStorage' ? localStorage : sessionStorage;
22
+ if (storageType === 'localStorage' && typeof localStorage !== 'undefined') {
23
+ this.#storage = localStorage;
24
+ }
25
+ else if (storageType === 'sessionStorage' && typeof sessionStorage !== 'undefined') {
26
+ this.#storage = sessionStorage;
27
+ }
20
28
  if (typeof this.#storage !== 'undefined') {
21
29
  if (this.#storage.getItem(key) === null) {
22
- this.#storage.setItem(key, JSON.stringify(initial));
30
+ try {
31
+ this.#storage.setItem(key, JSON.stringify(initial));
32
+ }
33
+ catch {
34
+ // Storage full or unavailable — continue with in-memory fallback
35
+ }
23
36
  }
24
37
  }
25
38
  }
26
39
  get current() {
40
+ // eslint-disable-next-line @typescript-eslint/no-unused-expressions
27
41
  this.#version;
28
- const root = typeof this.#storage !== 'undefined'
29
- ? JSON.parse(this.#storage.getItem(this.#key))
30
- : this.#value;
42
+ let root;
43
+ if (typeof this.#storage !== 'undefined') {
44
+ try {
45
+ root = JSON.parse(this.#storage.getItem(this.#key));
46
+ }
47
+ catch {
48
+ root = this.#value;
49
+ }
50
+ }
51
+ else {
52
+ root = this.#value;
53
+ }
31
54
  const proxies = new WeakMap();
32
55
  const proxy = (value) => {
33
56
  if (typeof value !== 'object' || value === null) {
@@ -37,6 +60,7 @@ export class PersistentState {
37
60
  if (!p) {
38
61
  p = new Proxy(value, {
39
62
  get: (target, property) => {
63
+ // eslint-disable-next-line @typescript-eslint/no-unused-expressions
40
64
  this.#version;
41
65
  return proxy(Reflect.get(target, property));
42
66
  },
@@ -44,7 +68,12 @@ export class PersistentState {
44
68
  this.#version += 1;
45
69
  Reflect.set(target, property, value);
46
70
  if (typeof this.#storage !== 'undefined') {
47
- this.#storage.setItem(this.#key, JSON.stringify(root));
71
+ try {
72
+ this.#storage.setItem(this.#key, JSON.stringify(root));
73
+ }
74
+ catch {
75
+ // Storage full or unavailable
76
+ }
48
77
  }
49
78
  return true;
50
79
  }
@@ -73,7 +102,12 @@ export class PersistentState {
73
102
  }
74
103
  set current(value) {
75
104
  if (typeof this.#storage !== 'undefined') {
76
- this.#storage.setItem(this.#key, JSON.stringify(value));
105
+ try {
106
+ this.#storage.setItem(this.#key, JSON.stringify(value));
107
+ }
108
+ catch {
109
+ // Storage full or unavailable
110
+ }
77
111
  }
78
112
  this.#version += 1;
79
113
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@friendofsvelte/state",
3
3
  "description": "Persistent Svelte 5 State, localStorage & sessionStorage",
4
- "version": "0.0.6",
4
+ "version": "0.2.0",
5
5
  "scripts": {
6
6
  "dev": "vite dev",
7
7
  "build": "vite build && npm run prepack",
@@ -36,6 +36,7 @@
36
36
  "svelte": "^5.0.0"
37
37
  },
38
38
  "devDependencies": {
39
+ "@changesets/cli": "^2.29.8",
39
40
  "@eslint/compat": "^1.2.5",
40
41
  "@eslint/js": "^9.18.0",
41
42
  "@sveltejs/adapter-auto": "^4.0.0",
@@ -62,8 +63,9 @@
62
63
  "vite": "^6.0.0",
63
64
  "vitest": "^3.0.0"
64
65
  },
66
+ "license": "MIT",
65
67
  "repository": {
66
- "type": "git",
67
- "url": "https://github.com/friendofsvelte/state.git"
68
+ "type": "git",
69
+ "url": "git+https://github.com/friendofsvelte/state.git"
68
70
  }
69
71
  }