@continuum-dev/session 0.1.1 → 0.1.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 +44 -5
- package/lib/session.d.ts.map +1 -1
- package/lib/session.js +40 -6
- package/lib/types.d.ts +12 -2
- package/lib/types.d.ts.map +1 -1
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -240,15 +240,54 @@ const log = session.getEventLog();
|
|
|
240
240
|
|
|
241
241
|
### 6) Actions Registry
|
|
242
242
|
|
|
243
|
-
Register
|
|
243
|
+
Register handlers for semantic intent ids and dispatch them with full session context.
|
|
244
|
+
|
|
245
|
+
#### `session.registerAction(intentId, registration, handler)`
|
|
244
246
|
|
|
245
247
|
```typescript
|
|
246
|
-
session.registerAction('
|
|
247
|
-
|
|
248
|
-
|
|
248
|
+
session.registerAction('submit_form', { label: 'Submit' }, async (context) => {
|
|
249
|
+
const response = await fetch('/api/submit', {
|
|
250
|
+
method: 'POST',
|
|
251
|
+
body: JSON.stringify(context.snapshot.values),
|
|
252
|
+
});
|
|
253
|
+
const data = await response.json();
|
|
254
|
+
context.session.updateState('status', { value: 'submitted' });
|
|
255
|
+
return { success: true, data };
|
|
249
256
|
});
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
Handlers receive an `ActionContext` with:
|
|
260
|
+
|
|
261
|
+
- `intentId` -- the dispatched intent identifier
|
|
262
|
+
- `snapshot` -- current `DataSnapshot` at dispatch time
|
|
263
|
+
- `nodeId` -- the node that triggered the action
|
|
264
|
+
- `session` -- an `ActionSessionRef` for post-action mutations (`pushView`, `updateState`, `getSnapshot`, `proposeValue`)
|
|
265
|
+
|
|
266
|
+
#### `session.dispatchAction(intentId, nodeId)`
|
|
267
|
+
|
|
268
|
+
Returns a `Promise<ActionResult>`. Catches handler errors automatically.
|
|
269
|
+
|
|
270
|
+
```typescript
|
|
271
|
+
const result = await session.dispatchAction('submit_form', 'btn_submit');
|
|
272
|
+
if (result.success) {
|
|
273
|
+
console.log('Submitted:', result.data);
|
|
274
|
+
} else {
|
|
275
|
+
console.error('Failed:', result.error);
|
|
276
|
+
}
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
If no handler is registered, a warning is logged and `{ success: false }` is returned.
|
|
280
|
+
|
|
281
|
+
#### `session.executeIntent(intent)`
|
|
250
282
|
|
|
251
|
-
|
|
283
|
+
Bridges the intent lifecycle and action dispatch in a single call: submits a pending intent, dispatches the registered action, and marks the intent as validated on success or cancelled on failure.
|
|
284
|
+
|
|
285
|
+
```typescript
|
|
286
|
+
const result = await session.executeIntent({
|
|
287
|
+
nodeId: 'btn_submit',
|
|
288
|
+
intentName: 'submit_form',
|
|
289
|
+
payload: { source: 'user' },
|
|
290
|
+
});
|
|
252
291
|
```
|
|
253
292
|
|
|
254
293
|
Also available:
|
package/lib/session.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../../../../packages/session/src/lib/session.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../../../../packages/session/src/lib/session.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AA4O1E;;;;;;;;GAQG;AACH,wBAAgB,aAAa,CAAC,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,CAqB/D;AAED;;;;;;GAMG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,CAuB5E;AAED;;;;;;;GAOG;AACH,wBAAgB,eAAe,CAAC,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,CAWjE;AAED;;GAEG;AACH,eAAO,MAAM,cAAc,EAAE,cAA+C,CAAC"}
|
package/lib/session.js
CHANGED
|
@@ -175,15 +175,49 @@ function assembleSessionFromInternalState(internal, cleanupPersistence) {
|
|
|
175
175
|
}
|
|
176
176
|
return result;
|
|
177
177
|
},
|
|
178
|
-
dispatchAction(intentId, nodeId) {
|
|
178
|
+
async dispatchAction(intentId, nodeId) {
|
|
179
179
|
assertNotDestroyed(internal);
|
|
180
180
|
const entry = internal.actionRegistry.get(intentId);
|
|
181
|
-
if (!entry)
|
|
182
|
-
|
|
181
|
+
if (!entry) {
|
|
182
|
+
console.warn(`[Continuum] dispatchAction: no handler registered for intentId "${intentId}"`);
|
|
183
|
+
return { success: false, error: `No handler registered for intentId "${intentId}"` };
|
|
184
|
+
}
|
|
183
185
|
const snapshot = buildSnapshotFromCurrentState(internal);
|
|
184
|
-
if (!snapshot)
|
|
185
|
-
return;
|
|
186
|
-
|
|
186
|
+
if (!snapshot) {
|
|
187
|
+
return { success: false, error: 'No active snapshot' };
|
|
188
|
+
}
|
|
189
|
+
const sessionRef = {
|
|
190
|
+
pushView: (v) => session.pushView(v),
|
|
191
|
+
updateState: (id, p) => session.updateState(id, p),
|
|
192
|
+
getSnapshot: () => session.getSnapshot(),
|
|
193
|
+
proposeValue: (id, v, s) => session.proposeValue(id, v, s),
|
|
194
|
+
};
|
|
195
|
+
try {
|
|
196
|
+
const raw = await entry.handler({ intentId, snapshot: snapshot.data, nodeId, session: sessionRef });
|
|
197
|
+
return raw ?? { success: true };
|
|
198
|
+
}
|
|
199
|
+
catch (error) {
|
|
200
|
+
return { success: false, error };
|
|
201
|
+
}
|
|
202
|
+
},
|
|
203
|
+
async executeIntent(partial) {
|
|
204
|
+
assertNotDestroyed(internal);
|
|
205
|
+
submitIntent(internal, partial);
|
|
206
|
+
const intent = internal.pendingIntents[internal.pendingIntents.length - 1];
|
|
207
|
+
try {
|
|
208
|
+
const result = await session.dispatchAction(partial.intentName, partial.nodeId);
|
|
209
|
+
if (result.success) {
|
|
210
|
+
validateIntent(internal, intent.intentId);
|
|
211
|
+
}
|
|
212
|
+
else {
|
|
213
|
+
cancelIntent(internal, intent.intentId);
|
|
214
|
+
}
|
|
215
|
+
return result;
|
|
216
|
+
}
|
|
217
|
+
catch (error) {
|
|
218
|
+
cancelIntent(internal, intent.intentId);
|
|
219
|
+
return { success: false, error };
|
|
220
|
+
}
|
|
187
221
|
},
|
|
188
222
|
};
|
|
189
223
|
return session;
|
package/lib/types.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ContinuitySnapshot, Interaction, DetachedValue, DetachedValuePolicy, ProposedValue, ViewportState, PendingIntent, Checkpoint, ViewDefinition, NodeValue, ActionRegistration, ActionHandler } from '@continuum-dev/contract';
|
|
1
|
+
import type { ContinuitySnapshot, Interaction, DetachedValue, DetachedValuePolicy, ProposedValue, ViewportState, PendingIntent, Checkpoint, ViewDefinition, NodeValue, ActionRegistration, ActionHandler, ActionResult } from '@continuum-dev/contract';
|
|
2
2
|
import type { ReconciliationIssue, ReconciliationOptions, ReconciliationResolution, StateDiff } from '@continuum-dev/runtime';
|
|
3
3
|
/**
|
|
4
4
|
* Minimal storage adapter used by session persistence.
|
|
@@ -246,8 +246,18 @@ export interface Session {
|
|
|
246
246
|
getRegisteredActions(): Record<string, ActionRegistration>;
|
|
247
247
|
/**
|
|
248
248
|
* Dispatches a registered action handler with current snapshot context.
|
|
249
|
+
*
|
|
250
|
+
* Returns `{ success: false }` when no handler is registered or when no
|
|
251
|
+
* active snapshot exists.
|
|
252
|
+
*/
|
|
253
|
+
dispatchAction(intentId: string, nodeId: string): Promise<ActionResult>;
|
|
254
|
+
/**
|
|
255
|
+
* Submits a pending intent, dispatches the registered action, and updates
|
|
256
|
+
* intent status based on the result (validated on success, cancelled on failure).
|
|
257
|
+
*
|
|
258
|
+
* Returns the same `ActionResult` shape as `dispatchAction`.
|
|
249
259
|
*/
|
|
250
|
-
|
|
260
|
+
executeIntent(intent: Omit<PendingIntent, 'intentId' | 'queuedAt' | 'status' | 'viewVersion'>): Promise<ActionResult>;
|
|
251
261
|
}
|
|
252
262
|
/**
|
|
253
263
|
* Helper interface for dependency injection and test doubles.
|
package/lib/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../../packages/session/src/lib/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,kBAAkB,EAClB,WAAW,EACX,aAAa,EACb,mBAAmB,EACnB,aAAa,EACb,aAAa,EACb,aAAa,EACb,UAAU,EACV,cAAc,EACd,SAAS,EACT,kBAAkB,EAClB,aAAa,
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../../packages/session/src/lib/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,kBAAkB,EAClB,WAAW,EACX,aAAa,EACb,mBAAmB,EACnB,aAAa,EACb,aAAa,EACb,aAAa,EACb,UAAU,EACV,cAAc,EACd,SAAS,EACT,kBAAkB,EAClB,aAAa,EACb,YAAY,EACb,MAAM,qBAAqB,CAAC;AAC7B,OAAO,KAAK,EACV,mBAAmB,EACnB,qBAAqB,EACrB,wBAAwB,EACxB,SAAS,EACV,MAAM,oBAAoB,CAAC;AAE5B;;;;GAIG;AACH,MAAM,WAAW,yBAAyB;IACxC;;OAEG;IACH,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAAC;IACpC;;OAEG;IACH,OAAO,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1C;;OAEG;IACH,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,CAAC;CAC/B;AAED;;GAEG;AACH,MAAM,WAAW,yBAAyB;IACxC;;OAEG;IACH,OAAO,EAAE,yBAAyB,CAAC;IACnC;;;;OAIG;IACH,GAAG,CAAC,EAAE,MAAM,CAAC;IACb;;;;OAIG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB;;OAEG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GAAG,QAAQ,GAAG,aAAa,CAAC,GAAG,OAAO,CAAC,YAAY,CAAC,CAAC;CACvH;AAED;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B;;OAEG;IACH,aAAa,CAAC,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC;IACjD;;OAEG;IACH,WAAW,CAAC,IAAI,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC;CAC/D"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@continuum-dev/session",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
"LICENSE*"
|
|
41
41
|
],
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"@continuum-dev/contract": "^0.1.
|
|
44
|
-
"@continuum-dev/runtime": "^0.1.
|
|
43
|
+
"@continuum-dev/contract": "^0.1.2",
|
|
44
|
+
"@continuum-dev/runtime": "^0.1.2"
|
|
45
45
|
}
|
|
46
46
|
}
|