@rotorsoft/act 0.13.0 → 0.15.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
@@ -55,12 +55,12 @@ var ZodEmpty = z.record(z.string(), z.never());
55
55
  var ActorSchema = z.object({
56
56
  id: z.string(),
57
57
  name: z.string()
58
- }).readonly();
58
+ }).loose().readonly();
59
59
  var TargetSchema = z.object({
60
60
  stream: z.string(),
61
61
  actor: ActorSchema,
62
62
  expectedVersion: z.number().optional()
63
- }).readonly();
63
+ }).loose().readonly();
64
64
  var CausationEventSchema = z.object({
65
65
  id: z.number(),
66
66
  name: z.string(),
@@ -726,7 +726,7 @@ var Act = class {
726
726
  * 5. Applies events to create new state
727
727
  * 6. Commits events to the store with optimistic concurrency control
728
728
  *
729
- * @template K - Action name from registered actions
729
+ * @template TKey - Action name from registered actions
730
730
  * @param action - The name of the action to execute
731
731
  * @param target - Target specification with stream ID and actor context
732
732
  * @param payload - Action payload matching the action's schema
@@ -1412,7 +1412,18 @@ function act(states = /* @__PURE__ */ new Map(), registry = {
1412
1412
  },
1413
1413
  withProjection: (proj) => {
1414
1414
  mergeProjection(proj, registry.events);
1415
- return act(states, registry, pendingProjections);
1415
+ return act(
1416
+ states,
1417
+ registry,
1418
+ pendingProjections
1419
+ );
1420
+ },
1421
+ withActor: () => {
1422
+ return act(
1423
+ states,
1424
+ registry,
1425
+ pendingProjections
1426
+ );
1416
1427
  },
1417
1428
  on: (event) => ({
1418
1429
  do: (handler, options) => {
@@ -1449,7 +1460,10 @@ function act(states = /* @__PURE__ */ new Map(), registry = {
1449
1460
  for (const proj of pendingProjections) {
1450
1461
  mergeProjection(proj, registry.events);
1451
1462
  }
1452
- return new Act(registry, states);
1463
+ return new Act(
1464
+ registry,
1465
+ states
1466
+ );
1453
1467
  },
1454
1468
  events: registry.events
1455
1469
  };
@@ -1531,7 +1545,12 @@ function slice(states = /* @__PURE__ */ new Map(), actions = {}, events = {}, pr
1531
1545
  },
1532
1546
  withProjection: (proj) => {
1533
1547
  projections.push(proj);
1534
- return slice(states, actions, events, projections);
1548
+ return slice(
1549
+ states,
1550
+ actions,
1551
+ events,
1552
+ projections
1553
+ );
1535
1554
  },
1536
1555
  on: (event) => ({
1537
1556
  do: (handler, options) => {
@@ -1585,19 +1604,34 @@ function state(entry) {
1585
1604
  init(init) {
1586
1605
  return {
1587
1606
  emits(events) {
1588
- return {
1589
- patch(patch2) {
1607
+ const defaultPatch = Object.fromEntries(
1608
+ Object.keys(events).map((k) => [
1609
+ k,
1610
+ ({ data }) => data
1611
+ ])
1612
+ );
1613
+ const builder = action_builder({
1614
+ events,
1615
+ actions: {},
1616
+ state: stateSchema,
1617
+ name,
1618
+ init,
1619
+ patch: defaultPatch,
1620
+ on: {}
1621
+ });
1622
+ return Object.assign(builder, {
1623
+ patch(customPatch) {
1590
1624
  return action_builder({
1591
1625
  events,
1592
1626
  actions: {},
1593
1627
  state: stateSchema,
1594
1628
  name,
1595
1629
  init,
1596
- patch: patch2,
1630
+ patch: { ...defaultPatch, ...customPatch },
1597
1631
  on: {}
1598
1632
  });
1599
1633
  }
1600
- };
1634
+ });
1601
1635
  }
1602
1636
  };
1603
1637
  }
@@ -1612,7 +1646,10 @@ function action_builder(state2) {
1612
1646
  const schema = entry[action2];
1613
1647
  if (action2 in state2.actions)
1614
1648
  throw new Error(`Duplicate action "${action2}"`);
1615
- const actions = { ...state2.actions, [action2]: schema };
1649
+ const actions = {
1650
+ ...state2.actions,
1651
+ [action2]: schema
1652
+ };
1616
1653
  const on = { ...state2.on };
1617
1654
  const _given = { ...state2.given };
1618
1655
  function given(rules) {
@@ -1620,7 +1657,12 @@ function action_builder(state2) {
1620
1657
  return { emit };
1621
1658
  }
1622
1659
  function emit(handler) {
1623
- on[action2] = handler;
1660
+ if (typeof handler === "string") {
1661
+ const eventName = handler;
1662
+ on[action2] = ((payload) => [eventName, payload]);
1663
+ } else {
1664
+ on[action2] = handler;
1665
+ }
1624
1666
  return action_builder({
1625
1667
  ...state2,
1626
1668
  actions,
@@ -1631,7 +1673,10 @@ function action_builder(state2) {
1631
1673
  return { given, emit };
1632
1674
  },
1633
1675
  snap(snap2) {
1634
- return action_builder({ ...state2, snap: snap2 });
1676
+ return action_builder({
1677
+ ...state2,
1678
+ snap: snap2
1679
+ });
1635
1680
  },
1636
1681
  build() {
1637
1682
  return state2;