@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.cjs CHANGED
@@ -122,12 +122,12 @@ var ZodEmpty = import_zod.z.record(import_zod.z.string(), import_zod.z.never());
122
122
  var ActorSchema = import_zod.z.object({
123
123
  id: import_zod.z.string(),
124
124
  name: import_zod.z.string()
125
- }).readonly();
125
+ }).loose().readonly();
126
126
  var TargetSchema = import_zod.z.object({
127
127
  stream: import_zod.z.string(),
128
128
  actor: ActorSchema,
129
129
  expectedVersion: import_zod.z.number().optional()
130
- }).readonly();
130
+ }).loose().readonly();
131
131
  var CausationEventSchema = import_zod.z.object({
132
132
  id: import_zod.z.number(),
133
133
  name: import_zod.z.string(),
@@ -793,7 +793,7 @@ var Act = class {
793
793
  * 5. Applies events to create new state
794
794
  * 6. Commits events to the store with optimistic concurrency control
795
795
  *
796
- * @template K - Action name from registered actions
796
+ * @template TKey - Action name from registered actions
797
797
  * @param action - The name of the action to execute
798
798
  * @param target - Target specification with stream ID and actor context
799
799
  * @param payload - Action payload matching the action's schema
@@ -1479,7 +1479,18 @@ function act(states = /* @__PURE__ */ new Map(), registry = {
1479
1479
  },
1480
1480
  withProjection: (proj) => {
1481
1481
  mergeProjection(proj, registry.events);
1482
- return act(states, registry, pendingProjections);
1482
+ return act(
1483
+ states,
1484
+ registry,
1485
+ pendingProjections
1486
+ );
1487
+ },
1488
+ withActor: () => {
1489
+ return act(
1490
+ states,
1491
+ registry,
1492
+ pendingProjections
1493
+ );
1483
1494
  },
1484
1495
  on: (event) => ({
1485
1496
  do: (handler, options) => {
@@ -1516,7 +1527,10 @@ function act(states = /* @__PURE__ */ new Map(), registry = {
1516
1527
  for (const proj of pendingProjections) {
1517
1528
  mergeProjection(proj, registry.events);
1518
1529
  }
1519
- return new Act(registry, states);
1530
+ return new Act(
1531
+ registry,
1532
+ states
1533
+ );
1520
1534
  },
1521
1535
  events: registry.events
1522
1536
  };
@@ -1598,7 +1612,12 @@ function slice(states = /* @__PURE__ */ new Map(), actions = {}, events = {}, pr
1598
1612
  },
1599
1613
  withProjection: (proj) => {
1600
1614
  projections.push(proj);
1601
- return slice(states, actions, events, projections);
1615
+ return slice(
1616
+ states,
1617
+ actions,
1618
+ events,
1619
+ projections
1620
+ );
1602
1621
  },
1603
1622
  on: (event) => ({
1604
1623
  do: (handler, options) => {
@@ -1652,19 +1671,34 @@ function state(entry) {
1652
1671
  init(init) {
1653
1672
  return {
1654
1673
  emits(events) {
1655
- return {
1656
- patch(patch2) {
1674
+ const defaultPatch = Object.fromEntries(
1675
+ Object.keys(events).map((k) => [
1676
+ k,
1677
+ ({ data }) => data
1678
+ ])
1679
+ );
1680
+ const builder = action_builder({
1681
+ events,
1682
+ actions: {},
1683
+ state: stateSchema,
1684
+ name,
1685
+ init,
1686
+ patch: defaultPatch,
1687
+ on: {}
1688
+ });
1689
+ return Object.assign(builder, {
1690
+ patch(customPatch) {
1657
1691
  return action_builder({
1658
1692
  events,
1659
1693
  actions: {},
1660
1694
  state: stateSchema,
1661
1695
  name,
1662
1696
  init,
1663
- patch: patch2,
1697
+ patch: { ...defaultPatch, ...customPatch },
1664
1698
  on: {}
1665
1699
  });
1666
1700
  }
1667
- };
1701
+ });
1668
1702
  }
1669
1703
  };
1670
1704
  }
@@ -1679,7 +1713,10 @@ function action_builder(state2) {
1679
1713
  const schema = entry[action2];
1680
1714
  if (action2 in state2.actions)
1681
1715
  throw new Error(`Duplicate action "${action2}"`);
1682
- const actions = { ...state2.actions, [action2]: schema };
1716
+ const actions = {
1717
+ ...state2.actions,
1718
+ [action2]: schema
1719
+ };
1683
1720
  const on = { ...state2.on };
1684
1721
  const _given = { ...state2.given };
1685
1722
  function given(rules) {
@@ -1687,7 +1724,12 @@ function action_builder(state2) {
1687
1724
  return { emit };
1688
1725
  }
1689
1726
  function emit(handler) {
1690
- on[action2] = handler;
1727
+ if (typeof handler === "string") {
1728
+ const eventName = handler;
1729
+ on[action2] = ((payload) => [eventName, payload]);
1730
+ } else {
1731
+ on[action2] = handler;
1732
+ }
1691
1733
  return action_builder({
1692
1734
  ...state2,
1693
1735
  actions,
@@ -1698,7 +1740,10 @@ function action_builder(state2) {
1698
1740
  return { given, emit };
1699
1741
  },
1700
1742
  snap(snap2) {
1701
- return action_builder({ ...state2, snap: snap2 });
1743
+ return action_builder({
1744
+ ...state2,
1745
+ snap: snap2
1746
+ });
1702
1747
  },
1703
1748
  build() {
1704
1749
  return state2;