@rotorsoft/act 0.11.0 → 0.12.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
@@ -1355,6 +1355,24 @@ function registerState(state2, states, actions, events) {
1355
1355
  }
1356
1356
  }
1357
1357
  }
1358
+ function mergeProjection(proj, events) {
1359
+ for (const eventName of Object.keys(proj.events)) {
1360
+ const projRegister = proj.events[eventName];
1361
+ const existing = events[eventName];
1362
+ if (!existing) {
1363
+ events[eventName] = {
1364
+ schema: projRegister.schema,
1365
+ reactions: new Map(projRegister.reactions)
1366
+ };
1367
+ } else {
1368
+ for (const [name, reaction] of projRegister.reactions) {
1369
+ let key = name;
1370
+ while (existing.reactions.has(key)) key = `${key}_p`;
1371
+ existing.reactions.set(key, reaction);
1372
+ }
1373
+ }
1374
+ }
1375
+ }
1358
1376
  var _this_ = ({ stream }) => ({
1359
1377
  source: stream,
1360
1378
  target: stream
@@ -1429,11 +1447,20 @@ function projection(target, events = {}) {
1429
1447
  function isSlice(x) {
1430
1448
  return x != null && x._tag === "Slice";
1431
1449
  }
1432
- function slice(states = /* @__PURE__ */ new Map(), actions = {}, events = {}) {
1450
+ function slice(states = /* @__PURE__ */ new Map(), actions = {}, events = {}, projections = []) {
1433
1451
  const builder = {
1434
1452
  with: (state2) => {
1435
1453
  registerState(state2, states, actions, events);
1436
- return slice(states, actions, events);
1454
+ return slice(
1455
+ states,
1456
+ actions,
1457
+ events,
1458
+ projections
1459
+ );
1460
+ },
1461
+ projection: (proj) => {
1462
+ projections.push(proj);
1463
+ return slice(states, actions, events, projections);
1437
1464
  },
1438
1465
  on: (event) => ({
1439
1466
  do: (handler, options) => {
@@ -1469,7 +1496,8 @@ function slice(states = /* @__PURE__ */ new Map(), actions = {}, events = {}) {
1469
1496
  build: () => ({
1470
1497
  _tag: "Slice",
1471
1498
  states,
1472
- events
1499
+ events,
1500
+ projections
1473
1501
  }),
1474
1502
  events
1475
1503
  };
@@ -1480,27 +1508,16 @@ function slice(states = /* @__PURE__ */ new Map(), actions = {}, events = {}) {
1480
1508
  function act(states = /* @__PURE__ */ new Map(), registry = {
1481
1509
  actions: {},
1482
1510
  events: {}
1483
- }) {
1511
+ }, pendingProjections = []) {
1484
1512
  const builder = {
1485
1513
  with: ((input) => {
1486
1514
  if (isProjection(input)) {
1487
- for (const eventName of Object.keys(input.events)) {
1488
- const projRegister = input.events[eventName];
1489
- const existing = registry.events[eventName];
1490
- if (!existing) {
1491
- registry.events[eventName] = {
1492
- schema: projRegister.schema,
1493
- reactions: new Map(projRegister.reactions)
1494
- };
1495
- } else {
1496
- for (const [name, reaction] of projRegister.reactions) {
1497
- let key = name;
1498
- while (existing.reactions.has(key)) key = `${key}_p`;
1499
- existing.reactions.set(key, reaction);
1500
- }
1501
- }
1502
- }
1503
- return act(states, registry);
1515
+ mergeProjection(input, registry.events);
1516
+ return act(
1517
+ states,
1518
+ registry,
1519
+ pendingProjections
1520
+ );
1504
1521
  }
1505
1522
  if (isSlice(input)) {
1506
1523
  for (const s of input.states.values()) {
@@ -1512,10 +1529,19 @@ function act(states = /* @__PURE__ */ new Map(), registry = {
1512
1529
  registry.events[eventName].reactions.set(name, reaction);
1513
1530
  }
1514
1531
  }
1515
- return act(states, registry);
1532
+ pendingProjections.push(...input.projections);
1533
+ return act(
1534
+ states,
1535
+ registry,
1536
+ pendingProjections
1537
+ );
1516
1538
  }
1517
1539
  registerState(input, states, registry.actions, registry.events);
1518
- return act(states, registry);
1540
+ return act(
1541
+ states,
1542
+ registry,
1543
+ pendingProjections
1544
+ );
1519
1545
  }),
1520
1546
  /**
1521
1547
  * Adds a reaction to an event.
@@ -1555,7 +1581,12 @@ function act(states = /* @__PURE__ */ new Map(), registry = {
1555
1581
  };
1556
1582
  }
1557
1583
  }),
1558
- build: () => new Act(registry, states),
1584
+ build: () => {
1585
+ for (const proj of pendingProjections) {
1586
+ mergeProjection(proj, registry.events);
1587
+ }
1588
+ return new Act(registry, states);
1589
+ },
1559
1590
  events: registry.events
1560
1591
  };
1561
1592
  return builder;