gjendje 0.3.5 → 0.4.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/README.md +56 -11
- package/dist/index.cjs +363 -117
- package/dist/index.d.cts +83 -1
- package/dist/index.d.ts +83 -1
- package/dist/index.js +361 -118
- package/package.json +81 -83
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
# gjendje
|
|
4
4
|
|
|
5
|
-
**gjendje**
|
|
5
|
+
Every app juggles `localStorage`, `sessionStorage`, URL params, and in-memory state — each with its own API. **gjendje** replaces it all with a single primitive. Choose where state lives. The rest is handled.
|
|
6
6
|
|
|
7
7
|
## Install
|
|
8
8
|
|
|
@@ -10,6 +10,37 @@
|
|
|
10
10
|
npm install gjendje
|
|
11
11
|
```
|
|
12
12
|
|
|
13
|
+
## Quick start
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { state } from 'gjendje'
|
|
17
|
+
|
|
18
|
+
// 1. Create state
|
|
19
|
+
const counter = state('counter', 0)
|
|
20
|
+
|
|
21
|
+
// 2. Read
|
|
22
|
+
counter.get() // 0
|
|
23
|
+
|
|
24
|
+
// 3. Write
|
|
25
|
+
counter.set(1)
|
|
26
|
+
counter.set((prev) => prev + 1)
|
|
27
|
+
|
|
28
|
+
// 4. Subscribe
|
|
29
|
+
counter.subscribe((value) => console.log(value))
|
|
30
|
+
|
|
31
|
+
// 5. Reset
|
|
32
|
+
counter.reset() // back to 0
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
To persist across page reloads, add a scope:
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
const theme = state('theme', { default: 'light', scope: 'local' })
|
|
39
|
+
|
|
40
|
+
theme.set('dark')
|
|
41
|
+
// Survives refresh — stored in localStorage
|
|
42
|
+
```
|
|
43
|
+
|
|
13
44
|
## Configure
|
|
14
45
|
|
|
15
46
|
Sets global defaults for all state instances.
|
|
@@ -22,18 +53,16 @@ import { configure } from 'gjendje'
|
|
|
22
53
|
configure({ scope: 'local' })
|
|
23
54
|
```
|
|
24
55
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
## Usage
|
|
56
|
+
Now every `state` call inherits that default:
|
|
28
57
|
|
|
29
58
|
```ts
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
const theme = state('theme', { default: 'light' })
|
|
59
|
+
const theme = state('theme', 'light')
|
|
33
60
|
|
|
34
61
|
theme.scope // 'local' — derived from configure
|
|
35
62
|
```
|
|
36
63
|
|
|
64
|
+
[Full configure reference](https://github.com/charliebeckstrand/gjendje/blob/main/docs/configure.md)
|
|
65
|
+
|
|
37
66
|
## Scopes
|
|
38
67
|
|
|
39
68
|
|Scope |Description |
|
|
@@ -45,6 +74,8 @@ theme.scope // 'local' — derived from configure
|
|
|
45
74
|
|`url` |`URLSearchParams` |
|
|
46
75
|
|`tab` |`sessionStorage` |
|
|
47
76
|
|
|
77
|
+
[Scope decision guide](https://github.com/charliebeckstrand/gjendje/blob/main/docs/scopes.md)
|
|
78
|
+
|
|
48
79
|
## API
|
|
49
80
|
|
|
50
81
|
#### `get()`
|
|
@@ -53,28 +84,42 @@ Returns the current value. Reactive — triggers tracking in `computed` and `eff
|
|
|
53
84
|
#### `set(value)` / `set(prev => next)`
|
|
54
85
|
Replaces the current value. Accepts a direct value or an updater function that receives the previous value.
|
|
55
86
|
|
|
56
|
-
#### `intercept(fn)`
|
|
57
|
-
Receives `(next, prev)` before each update. Return the value to store, or return `prev` to reject the change. Returns an `unsubscribe` function.
|
|
58
|
-
|
|
59
87
|
#### `subscribe(fn)`
|
|
60
88
|
Calls `fn` on every change. Returns an `unsubscribe` function.
|
|
61
89
|
|
|
62
90
|
#### `watch(key, fn)`
|
|
63
91
|
Like `subscribe`, but scoped to a single key within an object value. Only fires when that key changes.
|
|
64
92
|
|
|
65
|
-
|
|
93
|
+
#### `intercept(fn)`
|
|
94
|
+
Receives `(next, prev)` before each update. Return the value to store, or return `prev` to reject the change. Returns an `unsubscribe` function.
|
|
95
|
+
|
|
96
|
+
[Full API reference](https://github.com/charliebeckstrand/gjendje/blob/main/docs/api.md) · [Persistence reference](https://github.com/charliebeckstrand/gjendje/blob/main/docs/persistence.md)
|
|
66
97
|
|
|
67
98
|
## Derived state
|
|
68
99
|
|
|
69
100
|
#### `computed(deps, fn)`
|
|
70
101
|
Derives a reactive, read-only value from one or more state dependencies. Recomputes only when a dependency changes and caches the result between changes. Returns a `ReadonlyInstance` — no `set()` or `reset()`.
|
|
71
102
|
|
|
103
|
+
#### `select(source, fn)`
|
|
104
|
+
Lightweight single-dependency alternative to `computed`. No array allocation or dependency loop — just `source.get()` → `fn(value)`. Ideal for projecting a single field or transformation.
|
|
105
|
+
|
|
72
106
|
#### `collection(key, options)`
|
|
73
107
|
Reactive array with first-class mutation methods — `add`, `remove`, `update`, `find`, `findAll`, `has`, `clear`. Supports all the same scopes, persistence, validation, and migration as `state`.
|
|
74
108
|
|
|
75
109
|
#### `effect(deps, fn)`
|
|
76
110
|
Runs a side effect immediately and re-runs whenever any dependency changes. The callback can return a cleanup function that runs before the next execution and on `stop()`. Returns an `EffectHandle` with a `stop()` method.
|
|
77
111
|
|
|
112
|
+
#### `readonly(instance)`
|
|
113
|
+
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.
|
|
114
|
+
|
|
115
|
+
[Full derived state reference](https://github.com/charliebeckstrand/gjendje/blob/main/docs/derived.md)
|
|
116
|
+
|
|
117
|
+
## Examples
|
|
118
|
+
|
|
119
|
+
Real-world patterns and recipes — persistence with migration, cross-tab sync, derived state, collections, and more.
|
|
120
|
+
|
|
121
|
+
[Examples](https://github.com/charliebeckstrand/gjendje/blob/main/docs/examples.md)
|
|
122
|
+
|
|
78
123
|
## License
|
|
79
124
|
|
|
80
125
|
MIT
|