gjendje 0.8.0 → 0.9.2
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/README.md +19 -26
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
# gjendje
|
|
4
4
|
|
|
5
|
-
Every app juggles localStorage,
|
|
5
|
+
Every app juggles localStorage, URL params, sessionStorage, and in-memory state. **gjendje** replaces storage backends with a unified API. Choose where state lives. The rest is handled.
|
|
6
6
|
|
|
7
7
|
## Install
|
|
8
8
|
|
|
@@ -12,24 +12,27 @@ npm install gjendje
|
|
|
12
12
|
|
|
13
13
|
## Quick start
|
|
14
14
|
|
|
15
|
-
You can pass the scope as an option:
|
|
16
|
-
|
|
17
15
|
```ts
|
|
18
16
|
import { state } from 'gjendje'
|
|
19
17
|
|
|
18
|
+
// Pass scope as an option
|
|
20
19
|
const theme = state({ theme: 'light' }, { scope: 'local' })
|
|
20
|
+
|
|
21
|
+
// Or use dot notation
|
|
22
|
+
const theme = state.local({ theme: 'light' })
|
|
21
23
|
```
|
|
22
24
|
|
|
23
|
-
|
|
25
|
+
For in-memory state that doesn't persist, use `state` without a scope:
|
|
24
26
|
|
|
25
27
|
```ts
|
|
26
|
-
const
|
|
28
|
+
const filters = state({ role: 'all', status: 'active', tags: [] })
|
|
27
29
|
```
|
|
28
30
|
|
|
29
|
-
|
|
31
|
+
### Getting values
|
|
30
32
|
|
|
31
33
|
```ts
|
|
32
|
-
const
|
|
34
|
+
const filters = store.get() // Returns the full state object
|
|
35
|
+
const { role, status } = store.get() // Get values by destructuring
|
|
33
36
|
```
|
|
34
37
|
|
|
35
38
|
### Updating values
|
|
@@ -46,18 +49,17 @@ For object stores, `patch` lets you update specific properties without spreading
|
|
|
46
49
|
```ts
|
|
47
50
|
const form = state({ name: '', email: '', age: 0 })
|
|
48
51
|
|
|
49
|
-
form.patch({ name: 'Alice' }) //
|
|
50
|
-
form.patch({ name: 'Bob', age: 30 }) //
|
|
52
|
+
form.patch({ name: 'Alice' }) // Only updates name
|
|
53
|
+
form.patch({ name: 'Bob', age: 30 }) // Update multiple properties at once
|
|
51
54
|
```
|
|
52
55
|
|
|
53
|
-
[
|
|
56
|
+
[More examples](https://github.com/charliebeckstrand/gjendje/blob/main/docs/examples.md)
|
|
54
57
|
|
|
55
58
|
## Configure
|
|
56
59
|
|
|
57
|
-
Sets global defaults for all state instances.
|
|
60
|
+
Call once at app initialization. Sets global defaults for all state instances.
|
|
58
61
|
|
|
59
62
|
```ts
|
|
60
|
-
// app.ts
|
|
61
63
|
import { configure } from 'gjendje'
|
|
62
64
|
|
|
63
65
|
configure({ scope: 'local' })
|
|
@@ -75,16 +77,13 @@ You can also configure global events:
|
|
|
75
77
|
|
|
76
78
|
```ts
|
|
77
79
|
configure({
|
|
78
|
-
onError: ({ key, scope, error }) => {
|
|
79
|
-
console.error(`[${key}] (${scope}) error:`, error)
|
|
80
|
-
},
|
|
81
80
|
onChange: ({ key, scope, value, previousValue }) => {
|
|
82
81
|
console.log(`[${key}] (${scope}) changed:`, previousValue, '→', value)
|
|
83
82
|
},
|
|
84
83
|
})
|
|
85
84
|
```
|
|
86
85
|
|
|
87
|
-
[
|
|
86
|
+
[Configure reference guide](https://github.com/charliebeckstrand/gjendje/blob/main/docs/configure.md)
|
|
88
87
|
|
|
89
88
|
## Scopes
|
|
90
89
|
|
|
@@ -97,13 +96,13 @@ configure({
|
|
|
97
96
|
| `bucket` | Storage Buckets API | `state.bucket()` |
|
|
98
97
|
| `server` | `AsyncLocalStorage` | `state.server()` |
|
|
99
98
|
|
|
100
|
-
[Scope decision guide](https://github.com/charliebeckstrand/gjendje/blob/main/docs/scopes.md)
|
|
99
|
+
[Scope decision guide](https://github.com/charliebeckstrand/gjendje/blob/main/docs/scopes.md) · [Persistence reference guide](https://github.com/charliebeckstrand/gjendje/blob/main/docs/persistence.md)
|
|
101
100
|
|
|
102
101
|
## API
|
|
103
102
|
|
|
104
|
-
Every scope
|
|
103
|
+
Every scope shares the same core API: `get`, `set`, `patch`, `reset`, `subscribe`, `watch`, `intercept`, `use`, `destroy`
|
|
105
104
|
|
|
106
|
-
[
|
|
105
|
+
[API reference guide](https://github.com/charliebeckstrand/gjendje/blob/main/docs/api.md)
|
|
107
106
|
|
|
108
107
|
## Derived state
|
|
109
108
|
|
|
@@ -122,13 +121,7 @@ Runs a side effect immediately and re-runs whenever any dependency changes. The
|
|
|
122
121
|
#### `readonly(instance)`
|
|
123
122
|
Creates a read-only view of any state or computed instance. Exposes `get`, `peek`, `subscribe`, and lifecycle — but no `set`, `reset`, `intercept`, or `use`. Zero runtime cost.
|
|
124
123
|
|
|
125
|
-
[
|
|
126
|
-
|
|
127
|
-
## Examples
|
|
128
|
-
|
|
129
|
-
Real-world patterns and recipes — persistence with migration, cross-tab sync, derived state, collections, and more.
|
|
130
|
-
|
|
131
|
-
[Examples](https://github.com/charliebeckstrand/gjendje/blob/main/docs/examples.md)
|
|
124
|
+
[Derived state reference guide](https://github.com/charliebeckstrand/gjendje/blob/main/docs/derived.md)
|
|
132
125
|
|
|
133
126
|
## License
|
|
134
127
|
|
package/package.json
CHANGED