@rotorsoft/act 0.29.1 → 0.30.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/dist/index.js CHANGED
@@ -1033,24 +1033,22 @@ var Act = class {
1033
1033
  * }
1034
1034
  * ```
1035
1035
  *
1036
- * @example Reaction triggering another action
1036
+ * @example Reaction triggering another action (reactingTo auto-injected)
1037
1037
  * ```typescript
1038
1038
  * const app = act()
1039
1039
  * .withState(Order)
1040
1040
  * .withState(Inventory)
1041
1041
  * .on("OrderPlaced")
1042
- * .do(async (event, context) => {
1043
- * // This action is triggered by an event
1044
- * const result = await context.app.do(
1042
+ * .do(async function reduceInventory(event, _stream, app) {
1043
+ * // Inside reaction handlers, reactingTo is auto-injected when omitted.
1044
+ * // The triggering event is used by default, maintaining the correlation chain.
1045
+ * await app.do(
1045
1046
  * "reduceStock",
1046
- * {
1047
- * stream: "inventory-1",
1048
- * actor: event.meta.causation.action.actor
1049
- * },
1050
- * { amount: event.data.items.length },
1051
- * event // Pass event for correlation tracking
1047
+ * { stream: "inventory-1", actor: { id: "sys", name: "system" } },
1048
+ * { amount: event.data.items.length }
1052
1049
  * );
1053
- * return result;
1050
+ * // To use a different correlation, pass reactingTo explicitly:
1051
+ * // await app.do("reduceStock", target, payload, customEvent);
1054
1052
  * })
1055
1053
  * .to("inventory-1")
1056
1054
  * .build();
@@ -1186,6 +1184,11 @@ var Act = class {
1186
1184
  * This is called by the main `drain` loop after fetching new events.
1187
1185
  * It handles reactions, supporting retries, blocking, and error handling.
1188
1186
  *
1187
+ * Each handler receives a scoped `IAct` proxy that auto-injects the
1188
+ * triggering event as `reactingTo` when `do()` is called without it,
1189
+ * maintaining correlation chains by default (#587). Handlers can still
1190
+ * pass an explicit `reactingTo` to override this behavior.
1191
+ *
1189
1192
  * @internal
1190
1193
  * @param lease The lease to handle
1191
1194
  * @param payloads The reactions to handle
@@ -1196,10 +1199,24 @@ var Act = class {
1196
1199
  const stream = lease.stream;
1197
1200
  let at = payloads.at(0).event.id, handled = 0;
1198
1201
  lease.retry > 0 && logger3.warn(`Retrying ${stream}@${at} (${lease.retry}).`);
1202
+ const doAction = this.do.bind(this);
1203
+ const scopedApp = {
1204
+ do: doAction,
1205
+ load: this.load.bind(this),
1206
+ query: this.query.bind(this),
1207
+ query_array: this.query_array.bind(this)
1208
+ };
1199
1209
  for (const payload of payloads) {
1200
1210
  const { event, handler, options } = payload;
1211
+ scopedApp.do = (action2, target, payload2, reactingTo, skipValidation) => doAction(
1212
+ action2,
1213
+ target,
1214
+ payload2,
1215
+ reactingTo ?? event,
1216
+ skipValidation
1217
+ );
1201
1218
  try {
1202
- await handler(event, stream, this);
1219
+ await handler(event, stream, scopedApp);
1203
1220
  at = event.id;
1204
1221
  handled++;
1205
1222
  } catch (error) {