@rotorsoft/act 0.6.33 → 0.7.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
@@ -689,9 +689,11 @@ var Act = class {
689
689
  * Create a new Act orchestrator.
690
690
  *
691
691
  * @param registry The registry of state, event, and action schemas
692
+ * @param states Map of state names to their (potentially merged) state definitions
692
693
  */
693
- constructor(registry) {
694
+ constructor(registry, _states = /* @__PURE__ */ new Map()) {
694
695
  this.registry = registry;
696
+ this._states = _states;
695
697
  dispose(() => {
696
698
  this._emitter.removeAllListeners();
697
699
  this.stop_correlations();
@@ -849,7 +851,8 @@ var Act = class {
849
851
  * @see {@link Snapshot} for snapshot structure
850
852
  */
851
853
  async load(state2, stream, callback) {
852
- return await load(state2, stream, callback);
854
+ const merged = this._states.get(state2.name) || state2;
855
+ return await load(merged, stream, callback);
853
856
  }
854
857
  /**
855
858
  * Queries the event store for events matching a filter.
@@ -1312,13 +1315,15 @@ var _this_ = ({ stream }) => ({
1312
1315
  target: stream
1313
1316
  });
1314
1317
  var _void_ = () => void 0;
1315
- function act(states = /* @__PURE__ */ new Set(), registry = {
1318
+ function act(states = /* @__PURE__ */ new Map(), registry = {
1316
1319
  actions: {},
1317
1320
  events: {}
1318
1321
  }) {
1319
1322
  const builder = {
1320
1323
  /**
1321
- * Adds a state to the builder.
1324
+ * Adds a state to the builder. When a state with the same name is already
1325
+ * registered, merges the new partial's actions, events, patches, and handlers
1326
+ * into the existing state (errors on duplicate action/event names).
1322
1327
  *
1323
1328
  * @template SX The type of state
1324
1329
  * @template EX The type of events
@@ -1327,8 +1332,37 @@ function act(states = /* @__PURE__ */ new Set(), registry = {
1327
1332
  * @returns The builder
1328
1333
  */
1329
1334
  with: (state2) => {
1330
- if (!states.has(state2.name)) {
1331
- states.add(state2.name);
1335
+ if (states.has(state2.name)) {
1336
+ const existing = states.get(state2.name);
1337
+ for (const name of Object.keys(state2.actions)) {
1338
+ if (registry.actions[name])
1339
+ throw new Error(`Duplicate action "${name}"`);
1340
+ }
1341
+ for (const name of Object.keys(state2.events)) {
1342
+ if (registry.events[name])
1343
+ throw new Error(`Duplicate event "${name}"`);
1344
+ }
1345
+ const merged = {
1346
+ ...existing,
1347
+ events: { ...existing.events, ...state2.events },
1348
+ actions: { ...existing.actions, ...state2.actions },
1349
+ patch: { ...existing.patch, ...state2.patch },
1350
+ on: { ...existing.on, ...state2.on },
1351
+ given: { ...existing.given, ...state2.given },
1352
+ snap: state2.snap || existing.snap
1353
+ };
1354
+ states.set(state2.name, merged);
1355
+ for (const name of Object.keys(merged.actions)) {
1356
+ registry.actions[name] = merged;
1357
+ }
1358
+ for (const name of Object.keys(state2.events)) {
1359
+ registry.events[name] = {
1360
+ schema: state2.events[name],
1361
+ reactions: /* @__PURE__ */ new Map()
1362
+ };
1363
+ }
1364
+ } else {
1365
+ states.set(state2.name, state2);
1332
1366
  for (const name of Object.keys(state2.actions)) {
1333
1367
  if (registry.actions[name])
1334
1368
  throw new Error(`Duplicate action "${name}"`);
@@ -1385,7 +1419,7 @@ function act(states = /* @__PURE__ */ new Set(), registry = {
1385
1419
  };
1386
1420
  }
1387
1421
  }),
1388
- build: () => new Act(registry),
1422
+ build: () => new Act(registry, states),
1389
1423
  events: registry.events
1390
1424
  };
1391
1425
  return builder;