memorio 4.8.0 → 4.9.7
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 +232 -527
- package/SUMMARY.md +1 -5
- package/index.cjs +4401 -148
- package/index.js +4358 -102
- package/llms.txt +1 -1
- package/package.json +7 -8
- package/types/env.d.ts +9 -0
- package/types/exports.d.ts +50 -1
- package/types/memorio.d.ts +15 -11
- package/types/memory.d.ts +40 -2
- package/types/schema.d.ts +53 -53
- package/markdown/CACHE.md +0 -90
- package/markdown/CHANGELOG.md +0 -161
- package/markdown/DEVTOOLS.md +0 -122
- package/markdown/DISPATCH.md +0 -168
- package/markdown/HISTORY.md +0 -192
- package/markdown/IDB.md +0 -169
- package/markdown/IMPORT.md +0 -139
- package/markdown/INSPECT.md +0 -116
- package/markdown/LOGGER.md +0 -147
- package/markdown/MEMORY-ATTACHMENT.md +0 -95
- package/markdown/MEMORY.md +0 -155
- package/markdown/OBSERVER.md +0 -200
- package/markdown/PLATFORM.md +0 -265
- package/markdown/SCHEMA.md +0 -169
- package/markdown/SECURITY.md +0 -323
- package/markdown/SESSION.md +0 -154
- package/markdown/SQLITE.md +0 -181
- package/markdown/STATE.md +0 -153
- package/markdown/STORE.md +0 -164
- package/markdown/SYNC.md +0 -170
- package/markdown/TYPED.md +0 -158
- package/markdown/USEOBSERVER.md +0 -259
package/markdown/USEOBSERVER.md
DELETED
|
@@ -1,259 +0,0 @@
|
|
|
1
|
-
# useObserver - Memorio
|
|
2
|
-
|
|
3
|
-
> ⚛️ **React Only**: This is a React hook and only works within React components
|
|
4
|
-
|
|
5
|
-
useObserver is a React hook for observing state changes. It automatically subscribes to state changes and includes powerful auto-discovery features.
|
|
6
|
-
|
|
7
|
-
## Installation
|
|
8
|
-
|
|
9
|
-
```bash
|
|
10
|
-
npm install memorio
|
|
11
|
-
```
|
|
12
|
-
|
|
13
|
-
```javascript
|
|
14
|
-
import 'memorio';
|
|
15
|
-
```
|
|
16
|
-
|
|
17
|
-
---
|
|
18
|
-
|
|
19
|
-
## Quick Examples
|
|
20
|
-
|
|
21
|
-
### Example 1: Basic Usage
|
|
22
|
-
|
|
23
|
-
```javascript
|
|
24
|
-
import 'memorio';
|
|
25
|
-
|
|
26
|
-
function Counter() {
|
|
27
|
-
// Direct values work! ✅
|
|
28
|
-
useObserver(() => {
|
|
29
|
-
console.debug('Counter changed:', state.counter);
|
|
30
|
-
}, [state.counter]);
|
|
31
|
-
|
|
32
|
-
return <div>{state.counter}</div>;
|
|
33
|
-
}
|
|
34
|
-
```
|
|
35
|
-
|
|
36
|
-
### Auto-Discovery (Magic Mode)
|
|
37
|
-
|
|
38
|
-
```javascript
|
|
39
|
-
// Pass your callback WITHOUT dependencies - it auto-discovers!
|
|
40
|
-
function MyComponent() {
|
|
41
|
-
useObserver(() => {
|
|
42
|
-
// This will automatically track ALL state properties used inside
|
|
43
|
-
console.debug('Something changed:', state.user.name, state.items.length);
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
return <div>{state.user.name}</div>;
|
|
47
|
-
}
|
|
48
|
-
```
|
|
49
|
-
|
|
50
|
-
### Example 2: Intermediate
|
|
51
|
-
|
|
52
|
-
```javascript
|
|
53
|
-
function UserProfile() {
|
|
54
|
-
const [localState, setLocalState] = useState(null);
|
|
55
|
-
|
|
56
|
-
useObserver(() => {
|
|
57
|
-
setLocalState(state.user);
|
|
58
|
-
}, [state.user]);
|
|
59
|
-
|
|
60
|
-
return <div>{localState?.name}</div>;
|
|
61
|
-
}
|
|
62
|
-
```
|
|
63
|
-
|
|
64
|
-
### Example 3: Advanced
|
|
65
|
-
|
|
66
|
-
```javascript
|
|
67
|
-
// Multiple watchers with array - works with direct values
|
|
68
|
-
function MultiWatch() {
|
|
69
|
-
useObserver(() => {
|
|
70
|
-
console.debug('A or B changed:', state.a, state.b);
|
|
71
|
-
}, [state.a, state.b]); // Direct values work!
|
|
72
|
-
|
|
73
|
-
return <div>{state.a} - {state.b}</div>;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
// With string path (for store)
|
|
77
|
-
function StoreWatcher() {
|
|
78
|
-
useObserver(() => {
|
|
79
|
-
console.debug('Store changed');
|
|
80
|
-
}, 'store.userPreferences');
|
|
81
|
-
|
|
82
|
-
return <div />;
|
|
83
|
-
}
|
|
84
|
-
```
|
|
85
|
-
// With string path (for store)
|
|
86
|
-
function StoreWatcher() {
|
|
87
|
-
useObserver(() => {
|
|
88
|
-
console.debug('Store changed');
|
|
89
|
-
}, 'store.userPreferences');
|
|
90
|
-
|
|
91
|
-
return <div />;
|
|
92
|
-
}
|
|
93
|
-
```
|
|
94
|
-
|
|
95
|
-
---
|
|
96
|
-
|
|
97
|
-
## API Reference
|
|
98
|
-
|
|
99
|
-
### useObserver(callback, deps)
|
|
100
|
-
|
|
101
|
-
| Parameter | Type | Description |
|
|
102
|
-
| --------- | ---- | ----------- |
|
|
103
|
-
| `callback` | `function` | Function to run on change |
|
|
104
|
-
| `deps` | `function \| string \| array \| proxy` | State path(s) to watch. Supports: |
|
|
105
|
-
| | | - Direct values: `state.counter` |
|
|
106
|
-
| | | - String paths: `'state.counter'` |
|
|
107
|
-
| | | - Arrow functions: `() => state.counter` |
|
|
108
|
-
| | | - Arrays: `[state.a, state.b]` or `['state.a', 'state.b']` |
|
|
109
|
-
| | | - Optional chaining: `[state?.one]` |
|
|
110
|
-
|
|
111
|
-
### Primitive Values
|
|
112
|
-
|
|
113
|
-
Direct primitive values are now fully supported:
|
|
114
|
-
|
|
115
|
-
```javascript
|
|
116
|
-
// Direct values work with primitives ✅
|
|
117
|
-
useObserver(() => { console.debug('changed') }, [state.counter])
|
|
118
|
-
|
|
119
|
-
// Arrays of primitives work ✅
|
|
120
|
-
useObserver(() => { console.log(state.a, state.b) }, [state.a, state.b])
|
|
121
|
-
|
|
122
|
-
// Optional chaining works ✅
|
|
123
|
-
useObserver(() => { console.log(state?.one) }, [state?.one])
|
|
124
|
-
|
|
125
|
-
// Strings still work ✅
|
|
126
|
-
useObserver(() => { console.debug('changed') }, ['state.counter'])
|
|
127
|
-
|
|
128
|
-
// Functions still work ✅
|
|
129
|
-
useObserver(() => { console.debug('changed') }, [() => state.counter])
|
|
130
|
-
```
|
|
131
|
-
|
|
132
|
-
### Callback Parameters
|
|
133
|
-
|
|
134
|
-
```javascript
|
|
135
|
-
// Single value (no array needed)
|
|
136
|
-
useObserver(
|
|
137
|
-
() => {
|
|
138
|
-
console.debug('Changed:', state.key);
|
|
139
|
-
}, state.key // Single value works!
|
|
140
|
-
);
|
|
141
|
-
|
|
142
|
-
// Array of values
|
|
143
|
-
useObserver(
|
|
144
|
-
() => {
|
|
145
|
-
console.debug('Changed:', state.key);
|
|
146
|
-
}, [state.key] // Array also works!
|
|
147
|
-
);
|
|
148
|
-
```
|
|
149
|
-
|
|
150
|
-
### Auto-Discovery Mode
|
|
151
|
-
|
|
152
|
-
When `deps` is omitted, useObserver automatically discovers all state properties accessed inside the callback:
|
|
153
|
-
|
|
154
|
-
```javascript
|
|
155
|
-
// No deps needed - magic auto-discovery!
|
|
156
|
-
useObserver(() => {
|
|
157
|
-
// Automatically tracks state.user, state.items, state.counter
|
|
158
|
-
console.debug(state.user.name, state.items.length, state.counter);
|
|
159
|
-
},[]);
|
|
160
|
-
```
|
|
161
|
-
|
|
162
|
-
Returns a cleanup function:
|
|
163
|
-
|
|
164
|
-
## useObserver vs observer
|
|
165
|
-
|
|
166
|
-
| Feature | observer | useObserver |
|
|
167
|
-
| ------- | -------- | ----------- |
|
|
168
|
-
| Framework | Vanilla JS | React |
|
|
169
|
-
| Auto-cleanup | Manual | Auto |
|
|
170
|
-
| React lifecycle | No | Yes |
|
|
171
|
-
|
|
172
|
-
---
|
|
173
|
-
|
|
174
|
-
## Common Patterns
|
|
175
|
-
|
|
176
|
-
### Sync with useState (Recommended for Primitives)
|
|
177
|
-
|
|
178
|
-
```javascript
|
|
179
|
-
function CounterComponent() {
|
|
180
|
-
// Sync memorio state with React state
|
|
181
|
-
const [counter, setCounter] = useState(state.counter)
|
|
182
|
-
|
|
183
|
-
// React useEffect works correctly with primitive values
|
|
184
|
-
useEffect(() => {
|
|
185
|
-
console.log('Counter changed:', counter)
|
|
186
|
-
}, [counter])
|
|
187
|
-
|
|
188
|
-
// Direct values now work with primitives!
|
|
189
|
-
useObserver(() => {
|
|
190
|
-
setCounter(state.counter)
|
|
191
|
-
}, [state.counter]) // ✅ Works now!
|
|
192
|
-
|
|
193
|
-
return (
|
|
194
|
-
<div>
|
|
195
|
-
<button onClick={() => { state.counter++ }}>Increment</button>
|
|
196
|
-
<span>{counter}</span>
|
|
197
|
-
</div>
|
|
198
|
-
)
|
|
199
|
-
}
|
|
200
|
-
```
|
|
201
|
-
|
|
202
|
-
### Safe Access with Optional Chaining (Protection)
|
|
203
|
-
|
|
204
|
-
```javascript
|
|
205
|
-
// Optional chaining is supported - protects against errors
|
|
206
|
-
useObserver(() => {
|
|
207
|
-
if (test?.one) {
|
|
208
|
-
console.log(test.one)
|
|
209
|
-
}
|
|
210
|
-
}, [test?.one])
|
|
211
|
-
|
|
212
|
-
// Works with objects
|
|
213
|
-
function SafeComponent() {
|
|
214
|
-
const [test, setTest] = useState(state.test)
|
|
215
|
-
|
|
216
|
-
useEffect(() => {
|
|
217
|
-
if (test?.one) {
|
|
218
|
-
console.log('test.one:', test.one)
|
|
219
|
-
}
|
|
220
|
-
}, [test?.one])
|
|
221
|
-
|
|
222
|
-
useObserver(() => {
|
|
223
|
-
if (state.test?.one) {
|
|
224
|
-
setTest(state.test)
|
|
225
|
-
}
|
|
226
|
-
}, ['state.test.one'])
|
|
227
|
-
|
|
228
|
-
return <div>{test?.one || 'Loading...'}</div>
|
|
229
|
-
}
|
|
230
|
-
```
|
|
231
|
-
|
|
232
|
-
### Multiple Watchers
|
|
233
|
-
|
|
234
|
-
```javascript
|
|
235
|
-
// Watch multiple properties with strings
|
|
236
|
-
useObserver(() => {
|
|
237
|
-
console.log(state.a, state.b)
|
|
238
|
-
}, ['state.a', 'state.b'])
|
|
239
|
-
|
|
240
|
-
// Watch multiple properties with functions
|
|
241
|
-
useObserver(() => {
|
|
242
|
-
console.log(state.a, state.b)
|
|
243
|
-
}, [() => state.a, () => state.b])
|
|
244
|
-
|
|
245
|
-
// Watch with auto-discovery
|
|
246
|
-
useObserver(() => {
|
|
247
|
-
console.log(state.a, state.b) // Automatically tracks both
|
|
248
|
-
}, [])
|
|
249
|
-
```
|
|
250
|
-
|
|
251
|
-
---
|
|
252
|
-
|
|
253
|
-
## Best Practices
|
|
254
|
-
|
|
255
|
-
1. Always use in React components
|
|
256
|
-
2. Use auto-discovery for simpler code: `useObserver(() => { ... })`
|
|
257
|
-
3. No manual cleanup needed - returns cleanup function automatically
|
|
258
|
-
4. Use with state for reactive UI
|
|
259
|
-
5. Check console for auto-discovery logs
|