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,176 @@
1
+ > **Status:** Published
2
+ > **Date:** 2026-09-12
3
+ > **Scope**: API Reference
4
+ > **Standard**: Memorio API Specification v5
5
+ >
6
+ ---
7
+ # Dispatch - Memorio
8
+
9
+ > ⚛️ **Vanilla JS**: This is for non-React applications. For React, use [`useObserver`](USEOBSERVER.md).
10
+
11
+ `memorio.dispatch` is an event system for vanilla JavaScript applications. It enables pub/sub patterns without React hooks.
12
+
13
+ ## Installation
14
+
15
+ ```bash
16
+ npm install memorio
17
+ ```
18
+
19
+ ```javascript
20
+ import { memorio, state, store } from 'memorio';
21
+ ```
22
+
23
+ > **Global API**: `import 'memorio/global'` is the opt-in entrypoint that exposes `state`, `store`, `session`, `cache`, `idb`, `sqlite`, `observer`, `useObserver`, and more on `globalThis`.
24
+
25
+ ---
26
+
27
+ ## Quick Examples
28
+
29
+ ### Example 1: Basic Event Listening
30
+
31
+ ```javascript
32
+ // Listen for an event
33
+ memorio.dispatch.listen('my:event', (event) => {
34
+ console.debug('Event triggered:', event.detail);
35
+ });
36
+
37
+ // Trigger the event
38
+ memorio.dispatch.set('my:event', { detail: { data: 'Hello World' } });
39
+ // Output: "Event triggered: { data: 'Hello World' }"
40
+ ```
41
+
42
+ ### Example 2: State Reactivity (Vanilla JS)
43
+
44
+ ```javascript
45
+ // React to state changes without React
46
+ memorio.dispatch.listen('state.counter', (event) => {
47
+ console.debug('Counter is now:', event.detail);
48
+ });
49
+
50
+ // Update state
51
+ state.counter = 1;
52
+ // Output: "Counter is now: 1"
53
+
54
+ state.counter = 5;
55
+ // Output: "Counter is now: 5"
56
+ ```
57
+
58
+ ### Example 3: Remove Listener
59
+
60
+ ```javascript
61
+ // Remove a specific event listener
62
+ memorio.dispatch.remove('my:event');
63
+
64
+ // Or remove all listeners for state changes
65
+ memorio.dispatch.remove('state.user');
66
+ ```
67
+
68
+ ---
69
+
70
+ ## API Reference
71
+
72
+ ### memorio.dispatch.set(name, value)
73
+
74
+ Dispatches a custom event with the specified name and value.
75
+
76
+ | Parameter | Type | Description |
77
+ |-----------|------|-------------|
78
+ | `name` | `string` | Event name (e.g., `'my:event'`, `'state.counter'`) |
79
+ | `value` | `object` | Object with `detail` property (default: `{}`) |
80
+
81
+ ```javascript
82
+ memorio.dispatch.set('custom:event', { detail: { data: 'value' } });
83
+ ```
84
+
85
+ ### memorio.dispatch.listen(name, callback)
86
+
87
+ Listens for the specified event and executes the callback when triggered.
88
+
89
+ | Parameter | Type | Description |
90
+ |-----------|------|-------------|
91
+ | `name` | `string` | Event name to listen for |
92
+ | `callback` | `function` | Function called with the event object |
93
+
94
+ ```javascript
95
+ memorio.dispatch.listen('state.user', (event) => {
96
+ console.debug('User changed:', event.detail);
97
+ });
98
+ ```
99
+
100
+ ### memorio.dispatch.remove(name)
101
+
102
+ Removes the event listener for the specified event name.
103
+
104
+ | Parameter | Type | Description |
105
+ |-----------|------|-------------|
106
+ | `name` | `string` | Event name to stop listening |
107
+
108
+ ```javascript
109
+ memorio.dispatch.remove('state.counter');
110
+ ```
111
+
112
+ ---
113
+
114
+ ## Common Patterns
115
+
116
+ ### Form Validation
117
+
118
+ ```javascript
119
+ memorio.dispatch.listen('state.form.email', (event) => {
120
+ const email = event.detail;
121
+ const isValid = email.includes('@');
122
+ state.form.isValid = isValid;
123
+ });
124
+ ```
125
+
126
+ ### Analytics Tracking
127
+
128
+ ```javascript
129
+ memorio.dispatch.listen('state.page', (event) => {
130
+ const page = event.detail;
131
+ analytics.track('page_view', { page });
132
+ });
133
+ ```
134
+
135
+ ### Auto-save
136
+
137
+ ```javascript
138
+ memorio.dispatch.listen('state.draft', (event) => {
139
+ const content = event.detail;
140
+ store.set('autosave', content);
141
+ });
142
+ ```
143
+
144
+ ### Multiple Listeners
145
+
146
+ ```javascript
147
+ // Listen for multiple state changes
148
+ memorio.dispatch.listen('state.user', (e) => console.log('User:', e.detail));
149
+ memorio.dispatch.listen('state.settings', (e) => console.log('Settings:', e.detail));
150
+ ```
151
+
152
+ ---
153
+
154
+ ## Migration from observer()
155
+
156
+ The `observer()` Replace it with `memorio.dispatch.listen()`:
157
+
158
+ ```javascript
159
+ observer('state.counter', (newValue) => {
160
+ console.debug('Counter:', newValue);
161
+ });
162
+
163
+ // NEW (recommended for vanilla JS)
164
+ memorio.dispatch.listen('state.counter', (event) => {
165
+ console.debug('Counter:', event.detail);
166
+ });
167
+ ```
168
+
169
+ ---
170
+
171
+ ## Best Practices
172
+
173
+ 1. Use specific event names: `'state.user.name'` not `'state'`
174
+ 2. Clean up listeners when no longer needed with `memorio.dispatch.remove()`
175
+ 3. Use `event.detail` to access the value
176
+ 4. For React applications, use [`useObserver`](USEOBSERVER.md) instead
@@ -0,0 +1,198 @@
1
+ > **Status:** Published
2
+ > **Date:** 2026-09-12
3
+ > **Scope**: API Reference
4
+ > **Standard**: Memorio API Specification v5
5
+ >
6
+ ---
7
+ # History, Undo / Redo, Snapshot, Diff, Trace - Memorio
8
+
9
+ > ✅ **Universal**: Works in Browser, Node.js, Deno, and Edge Workers
10
+
11
+ Memorio provides a lightweight time-travel system for `state` mutations: snapshots, diffs, undo/redo, and a full mutation trace log.
12
+
13
+ History tracking is **opt-in** - it is disabled by default to avoid overhead. Enable it when you need undo/redo or trace capabilities.
14
+
15
+ ---
16
+
17
+ ## Enable History
18
+
19
+ ```javascript
20
+ import { memorio, state } from 'memorio'
21
+
22
+ memorio.enableHistory() // enable tracking
23
+ // ... mutate state ...
24
+ state.user = { name: 'Sara' }
25
+ state.counter = 42
26
+ ```
27
+
28
+ > Without `enableHistory()`, mutations are not recorded and `undo()`/`redo()`/`trace()` return empty results.
29
+
30
+ ---
31
+
32
+ ## Snapshot & Diff
33
+
34
+ Snapshot captures the entire `state` tree at a point in time. Diff compares a snapshot against current state to see what changed.
35
+
36
+ ```javascript
37
+ // Enable history (snapshots work regardless, but trace/undo need it)
38
+ memorio.enableHistory()
39
+
40
+ // Take a snapshot
41
+ state.user = { name: 'Sara', age: 30 }
42
+ const snap = memorio.snapshot()
43
+
44
+ // Make changes
45
+ state.user.name = 'Luigi'
46
+ state.counter = 100
47
+ state.items = ['a', 'b']
48
+
49
+ // Diff against the snapshot
50
+ const changes = memorio.diff(snap)
51
+ console.debug(changes)
52
+ // [
53
+ // { path: 'user.name', oldValue: 'Sara', newValue: 'Luigi' },
54
+ // { path: 'counter', oldValue: undefined, newValue: 100 },
55
+ // { path: 'items', oldValue: undefined, newValue: ['a', 'b'] }
56
+ // ]
57
+ ```
58
+
59
+ This is essential for AI agents: take a snapshot, make changes, inspect the diff, and decide whether to commit or rollback.
60
+
61
+ ---
62
+
63
+ ## Undo / Redo
64
+
65
+ ```javascript
66
+ state.user = { name: 'Sara' }
67
+ state.counter = 100
68
+ state.items = ['a', 'b']
69
+
70
+ memorio.undo() // removes state.items
71
+ memorio.undo() // counter → undefined
72
+ memorio.redo() // counter → 100 again
73
+
74
+ memorio.canUndo() // true
75
+ memorio.canRedo() // true (after above undo + redo cycle)
76
+ ```
77
+
78
+ - `undo()`: Restores the previous state by inverting the most recent mutation.
79
+ - `redo()`: Re-applies the most recently undone mutation.
80
+ - `canUndo()` / `canRedo()`: Check availability before calling.
81
+
82
+ ### Max history depth
83
+
84
+ ```javascript
85
+ memorio.setMaxHistory(50) // keep at most 50 mutations per stack (default: 100)
86
+ ```
87
+
88
+ ---
89
+
90
+ ## Rollback (full state restore)
91
+
92
+ Unlike undo (which works one step at a time), `rollback` replaces the entire state from a snapshot:
93
+
94
+ ```javascript
95
+ const snap = memorio.snapshot()
96
+
97
+ state.experiment = { result: 'failed' }
98
+ state.counter = 999
99
+
100
+ // Discard everything and restore to snapshot
101
+ memorio.rollback(snap)
102
+ // state.experiment is now gone
103
+ // state.counter is back to its snapshot value
104
+ ```
105
+
106
+ ---
107
+
108
+ ## Trace (mutation log)
109
+
110
+ The trace log records every mutation with timestamp, path, action, and before/after values:
111
+
112
+ ```javascript
113
+ memorio.enableHistory()
114
+
115
+ state.user.name = 'Sara'
116
+ state.counter = 1
117
+ state.counter = 2
118
+
119
+ const log = memorio.trace()
120
+ console.debug(log)
121
+ // [
122
+ // { path: 'user.name', action: 'set', newValue: 'Sara', previousValue: undefined, timestamp: 1725... },
123
+ // { path: 'counter', action: 'set', newValue: 1, previousValue: undefined, timestamp: 1725... },
124
+ // { path: 'counter', action: 'set', newValue: 2, previousValue: 1, timestamp: 1725... }
125
+ // ]
126
+ ```
127
+
128
+ This is useful for:
129
+ - **AI debugging**: inspect what changed and when
130
+ - **Event sourcing**: export the log and replay state from scratch
131
+ - **Audit trails**: log all mutations for compliance
132
+
133
+ ### Export / import trace
134
+
135
+ ```javascript
136
+ const log = memorio.trace()
137
+ localStorage.setItem('memorio-trace', JSON.stringify(log))
138
+
139
+ // Later, replay:
140
+ const saved = JSON.parse(localStorage.getItem('memorio-trace'))
141
+ for (const record of saved) {
142
+ if (record.action === 'set') {
143
+ state[record.path] = record.newValue
144
+ }
145
+ }
146
+ ```
147
+
148
+ ---
149
+
150
+ ## Clear History
151
+
152
+ ```javascript
153
+ memorio.clearHistory() // wipe undo/redo stacks + trace log
154
+ memorio.clearRedo() // clear only the redo stack (undo stack preserved)
155
+ ```
156
+
157
+ > `clearHistory()` does NOT reset the current `state` - only the history tracking data.
158
+
159
+ ---
160
+
161
+ ## Full API
162
+
163
+ | Method | Parameters | Returns | Description |
164
+ |--------|-----------|---------|-------------|
165
+ | `memorio.snapshot()` | none | `Record<string, any>` | Deep clone of current state |
166
+ | `memorio.diff(snap)` | `snap` | `DiffEntry[]` | Changed paths with old/new values |
167
+ | `memorio.undo()` | none | `MutationRecord \| undefined` | Undo last mutation |
168
+ | `memorio.redo()` | none | `MutationRecord \| undefined` | Redo last undone mutation |
169
+ | `memorio.canUndo()` | none | `boolean` | Whether undo is available |
170
+ | `memorio.canRedo()` | none | `boolean` | Whether redo is available |
171
+ | `memorio.rollback(snap)` | `snap` | `void` | Restore full state from snapshot |
172
+ | `memorio.trace()` | none | `MutationRecord[]` | List of all recorded mutations |
173
+ | `memorio.enableHistory(enabled?)` | `boolean` | `void` | Enable/disable tracking |
174
+ | `memorio.clearHistory()` | none | `void` | Clear all history stacks |
175
+ | `memorio.clearRedo()` | none | `void` | Clear only redo stack |
176
+ | `memorio.setMaxHistory(max)` | `number` | `void` | Set max stack depth |
177
+ | `memorio.getMaxHistory()` | none | `number` | Get current max depth |
178
+
179
+ ---
180
+
181
+ ## How It Works
182
+
183
+ 1. When `historyEnabled` is true, the state proxy's callback (`buildProxy` callback) fires on every `set`/`delete` trap, recording a `MutationRecord` with path, action, oldValue, newValue, and timestamp.
184
+ 2. Records are pushed to both a trace log (`mutations`) and an undo stack.
185
+ 3. Any new mutation clears the redo stack.
186
+ 4. `undo()` pops from the undo stack, pushes to the redo stack, and applies the inverse operation (restoring the previous value, or deleting if it was new).
187
+ 5. `redo()` pops from the redo stack, pushes back to the undo stack, and re-applies the original mutation.
188
+ 6. During undo/redo, history tracking is temporarily disabled to prevent recursive recording.
189
+ 7. `diff()` does a recursive key-by-key comparison between the snapshot and current `deepRaw(state)`.
190
+
191
+ ---
192
+
193
+ ## Best Practices
194
+
195
+ 1. **Always snapshot before AI experimentation** - `const snap = memorio.snapshot()` gives you a safe rollback point.
196
+ 2. **Call `diff()` before `rollback()`** - inspect what changed first; sometimes you only need to revert one key.
197
+ 3. **Keep `maxHistory` reasonable** - the default (100) is fine for most apps. Lower it for memory-constrained environments.
198
+ 4. **Don't rely on trace for sensitive data** - the trace log records *all* values written, including tokens/PII. Clear it or disable tracing in production paths.
@@ -0,0 +1,177 @@
1
+ > **Status:** Published
2
+ > **Date:** 2026-09-12
3
+ > **Scope**: API Reference
4
+ > **Standard**: Memorio API Specification v5
5
+ >
6
+ ---
7
+ # IDB - Memorio
8
+
9
+ > 🖥️ **Browser Only**: Requires IndexedDB (not available in Node.js/Deno)
10
+
11
+ IDB provides access to browser IndexedDB for large data storage. Unlike localStorage, IDB can store large amounts of structured data.
12
+
13
+ ## Installation
14
+
15
+ ```bash
16
+ npm install memorio
17
+ ```
18
+
19
+ ```javascript
20
+ import { idb } 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
+ // Create a database
33
+ idb.db.create('myApp');
34
+
35
+ // Add data
36
+ idb.data.set('myApp', 'users', { id: 1, name: 'Mario' });
37
+
38
+ // Get data
39
+ const user = idb.data.get('myApp', 'users', 1);
40
+ console.debug(user.name); // "Mario"
41
+ ```
42
+
43
+ ### Example 2: Intermediate
44
+
45
+ ```javascript
46
+ // Create database with tables
47
+ idb.db.create('store');
48
+ idb.table.create('store', 'products');
49
+
50
+ // Add multiple records
51
+ idb.data.set('store', 'products', { id: 1, name: 'Apple', price: 1.5 });
52
+ idb.data.set('store', 'products', { id: 2, name: 'Banana', price: 0.8 });
53
+
54
+ // List databases
55
+ const databases = idb.db.list();
56
+ console.debug(databases); // ['myApp', 'store']
57
+ ```
58
+
59
+ ### Example 3: Advanced
60
+
61
+ ```javascript
62
+ // Check database support
63
+ if (idb.db.support()) {
64
+ // Use IDB
65
+ }
66
+
67
+ // Get database info
68
+ const version = idb.db.version('store');
69
+ const size = idb.db.size('store');
70
+
71
+ // Delete database
72
+ idb.db.delete('store');
73
+
74
+ // Handle quota
75
+ const quota = idb.db.quota();
76
+ console.debug(`Using ${quota.used} of ${quota.total} bytes`);
77
+ ```
78
+
79
+ ---
80
+
81
+ ## API Reference
82
+
83
+ ### Database Methods
84
+
85
+ | Method | Parameters | Returns | Description |
86
+ |--------|------------|---------|-------------|
87
+ | `idb.db.create(name)` | `name: string` | `void` | Create database |
88
+ | `idb.db.delete(name)` | `name: string` | `void` | Delete database |
89
+ | `idb.db.list()` | none | `string[]` | List all databases |
90
+ | `idb.db.exist(name)` | `name: string` | `boolean` | Check if exists |
91
+ | `idb.db.size(name)` | `name: string` | `number` | Get database size |
92
+ | `idb.db.version(name)` | `name: string` | `number` | Get version |
93
+ | `idb.db.support()` | none | `boolean` | Check browser support |
94
+ | `idb.db.quota()` | none | `object` | Get storage quota |
95
+
96
+ ### Table Methods
97
+
98
+ | Method | Parameters | Returns | Description |
99
+ |--------|------------|---------|-------------|
100
+ | `idb.table.create(db, table)` | `db: string, table: string` | `void` | Create table |
101
+ | `idb.table.size(db, table)` | `db: string, table: string` | `number` | Get table size |
102
+
103
+ ### Data Methods
104
+
105
+ | Method | Parameters | Returns | Description |
106
+ |--------|------------|---------|-------------|
107
+ | `idb.data.get(db, table, id)` | `db, table, id` | `any` | Get single record |
108
+ | `idb.data.set(db, table, data)` | `db, table, data` | `void` | Set record |
109
+ | `idb.data.delete(db, table, id)` | `db, table, id` | `void` | Delete record |
110
+
111
+ ---
112
+
113
+ ## Data Structure
114
+
115
+ Each record needs an `id` field:
116
+
117
+ ```javascript
118
+ idb.data.set('myDB', 'users', {
119
+ id: 1, // Required!
120
+ name: 'Mario',
121
+ email: 'm@test.com'
122
+ });
123
+ ```
124
+
125
+ ---
126
+
127
+ ## Platform Support
128
+
129
+ | Platform | Support | Notes |
130
+ |----------|---------|-------|
131
+ | Browser | ✅ Full | Full IndexedDB support |
132
+ | Edge Worker | ⚠️ Limited | May not be available in all workers |
133
+ | Node.js | ❌ Not available | Use store or session instead |
134
+ | Deno | ❌ Not available | Use store or session instead |
135
+
136
+ ---
137
+
138
+ ## Storage Limits
139
+
140
+ - **Desktop browsers**: 50+ MB (often unlimited)
141
+ - **Mobile browsers**: 50-100 MB
142
+ - **More than localStorage**: Much higher limits
143
+
144
+ ---
145
+
146
+ ## Best Practices
147
+
148
+ 1. Always include `id` in records
149
+ 2. Use for large data: images, caches, offline data
150
+ 3. Check support: `idb.db.support()`
151
+ 4. Clean up: `idb.db.delete('tempDB')`
152
+
153
+ ---
154
+
155
+ ## Use Cases
156
+
157
+ ### Offline Data
158
+
159
+ ```javascript
160
+ // Cache API response
161
+ idb.data.set('cache', 'apiResponse', {
162
+ id: 'users',
163
+ data: usersArray,
164
+ timestamp: Date.now()
165
+ });
166
+ ```
167
+
168
+ ### Large User Data
169
+
170
+ ```javascript
171
+ // Store user-generated content
172
+ idb.data.set('app', 'uploads', {
173
+ id: Date.now(),
174
+ file: fileData,
175
+ userId: currentUser.id
176
+ });
177
+ ```
@@ -0,0 +1,152 @@
1
+ > **Status:** Published
2
+ > **Date:** 2026-09-12
3
+ > **Scope**: API Reference
4
+ > **Standard**: Memorio API Specification v5
5
+ >
6
+ ---
7
+ # Classic `import` support
8
+
9
+ Memorio supports two styles: the explicit named-import style (recommended),
10
+ and the global side-effect entrypoint for when global access is explicitly desired.
11
+ Both share the same instances (one source of truth).
12
+
13
+ ```typescript
14
+ // Explicit named imports (recommended for normal usage)
15
+ import { state } from 'memorio'
16
+ state.user = { name: 'Sara' }
17
+
18
+ // Global entrypoint (opt-in for global access)
19
+ import 'memorio/global'
20
+ state.user = { name: 'Sara' }
21
+ ```
22
+
23
+ `state` in both examples is the exact same Proxy object.
24
+
25
+ ---
26
+
27
+ ## Why two styles?
28
+
29
+ | Style | When to use |
30
+ |-------|-------------|
31
+ | `import { state } from 'memorio'` | Explicit dependencies, tree-shakeable bundles, TypeScript IntelliSense |
32
+ | `import 'memorio/global'` | When global access is explicitly desired (legacy scripts, shell-style usage) |
33
+
34
+ ---
35
+
36
+ ## ESM named imports
37
+
38
+ All modules are available as named exports:
39
+
40
+ ```typescript
41
+ import {
42
+ state,
43
+ store,
44
+ session,
45
+ cache,
46
+ idb,
47
+ observer,
48
+ useObserver,
49
+ dispatch,
50
+ message,
51
+ devtools,
52
+ logger
53
+ } from 'memorio'
54
+ ```
55
+
56
+ Platform helpers:
57
+
58
+ ```typescript
59
+ import {
60
+ isBrowser,
61
+ isNode,
62
+ isDeno,
63
+ isEdge,
64
+ getCapabilities,
65
+ createContext,
66
+ listContexts,
67
+ deleteContext
68
+ } from 'memorio'
69
+ ```
70
+
71
+ Internal utilities (for tests/debug):
72
+
73
+ ```typescript
74
+ import internal, { propertyName } from 'memorio'
75
+ import { setContext, getContext } from 'memorio'
76
+ ```
77
+
78
+ Default export (the public `memorio` namespace):
79
+
80
+ ```typescript
81
+ import memorio from 'memorio'
82
+ memorio.help() // dev-only introspection
83
+ ```
84
+
85
+ ---
86
+
87
+ ## CJS usage
88
+
89
+ ```javascript
90
+ const { state, store, memorio } = require('memorio')
91
+ ```
92
+
93
+ ---
94
+
95
+ ## React / useObserver
96
+
97
+ `useObserver` works the same way via named import:
98
+
99
+ ```tsx
100
+ import { useObserver, state } from 'memorio'
101
+
102
+ function Counter() {
103
+ const [, forceUpdate] = useReducer(x => x + 1, 0)
104
+
105
+ useObserver(forceUpdate, [state.counter])
106
+
107
+ return <div>Count: {state.counter}</div>
108
+ }
109
+ ```
110
+
111
+ ---
112
+
113
+ ## Context isolation
114
+
115
+ ```typescript
116
+ import { createContext, listContexts, deleteContext, isolate } from 'memorio'
117
+
118
+ const ctx = createContext('tenant-123')
119
+ ctx.state.user = { name: 'Isolated' }
120
+ ctx.store.set('settings', { theme: 'dark' })
121
+
122
+ listContexts() // ['tenant-123']
123
+ deleteContext('tenant-123')
124
+ ```
125
+
126
+ ---
127
+
128
+ ## Same-instance guarantee
129
+
130
+ Named exports point to the same instances as the global entrypoint. Mutating via a named export mutates the same proxy instance that `import 'memorio/global'` would install on `globalThis`, and vice versa.
131
+
132
+ ```typescript
133
+ import { state } from 'memorio'
134
+
135
+ state.importedFlag = true
136
+ console.debug(globalThis.state?.importedFlag) // true (when global entrypoint is loaded)
137
+ ```
138
+
139
+ ---
140
+
141
+ ## Migration to explicit imports
142
+
143
+ If you previously used `import 'memorio'` for global access, migrate to either:
144
+
145
+ ```typescript
146
+ // Option A: named imports (recommended)
147
+ import { state, store } from 'memorio'
148
+
149
+ // Option B: explicit global entrypoint (opt-in)
150
+ import 'memorio/global'
151
+ // state, store, etc. are now on globalThis
152
+ ```