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