@doeixd/machine 0.0.21 → 0.0.23

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.
@@ -1434,6 +1434,104 @@ function withDebugging(machine) {
1434
1434
  return withTimeTravel(withSnapshot(withHistory(machine)));
1435
1435
  }
1436
1436
 
1437
+ // src/mixins.ts
1438
+ function getAllPropertyDescriptors(obj) {
1439
+ const descriptors = {};
1440
+ let current = obj;
1441
+ while (current && current !== Object.prototype) {
1442
+ const props = Object.getOwnPropertyDescriptors(current);
1443
+ for (const [key, desc] of Object.entries(props)) {
1444
+ if (key === "constructor") continue;
1445
+ if (!(key in descriptors)) {
1446
+ descriptors[key] = desc;
1447
+ }
1448
+ }
1449
+ current = Object.getPrototypeOf(current);
1450
+ }
1451
+ return descriptors;
1452
+ }
1453
+ function MachineUnion(...machines) {
1454
+ const Base = machines[0];
1455
+ class CombinedMachine extends Base {
1456
+ constructor(context) {
1457
+ super(context);
1458
+ }
1459
+ }
1460
+ const wrapMethod = (fn) => {
1461
+ return function(...args) {
1462
+ const result = fn.apply(this, args);
1463
+ if (result && typeof result === "object" && "context" in result) {
1464
+ const newContext = { ...this.context, ...result.context };
1465
+ return new CombinedMachine(newContext);
1466
+ }
1467
+ return result;
1468
+ };
1469
+ };
1470
+ for (const machine of machines) {
1471
+ const descriptors = getAllPropertyDescriptors(machine.prototype);
1472
+ for (const [key, descriptor] of Object.entries(descriptors)) {
1473
+ if (key === "constructor") continue;
1474
+ if (typeof descriptor.value === "function") {
1475
+ const originalFn = descriptor.value;
1476
+ const wrappedFn = wrapMethod(originalFn);
1477
+ Object.defineProperty(CombinedMachine.prototype, key, {
1478
+ ...descriptor,
1479
+ value: wrappedFn
1480
+ });
1481
+ } else {
1482
+ Object.defineProperty(CombinedMachine.prototype, key, descriptor);
1483
+ }
1484
+ }
1485
+ }
1486
+ return CombinedMachine;
1487
+ }
1488
+ function MachineExclude(Source, ...Excluded) {
1489
+ class ExcludedMachine extends MachineBase {
1490
+ constructor(context) {
1491
+ super(context);
1492
+ }
1493
+ }
1494
+ const sourceDescriptors = getAllPropertyDescriptors(Source.prototype);
1495
+ for (const [key, descriptor] of Object.entries(sourceDescriptors)) {
1496
+ if (key === "constructor") continue;
1497
+ if (typeof descriptor.value === "function") {
1498
+ const originalFn = descriptor.value;
1499
+ const wrappedFn = function(...args) {
1500
+ const result = originalFn.apply(this, args);
1501
+ if (result && typeof result === "object" && "context" in result) {
1502
+ return new ExcludedMachine({ ...this.context, ...result.context });
1503
+ }
1504
+ return result;
1505
+ };
1506
+ Object.defineProperty(ExcludedMachine.prototype, key, { ...descriptor, value: wrappedFn });
1507
+ } else {
1508
+ Object.defineProperty(ExcludedMachine.prototype, key, descriptor);
1509
+ }
1510
+ }
1511
+ for (const Excl of Excluded) {
1512
+ const excludedDescriptors = getAllPropertyDescriptors(Excl.prototype);
1513
+ for (const key of Object.keys(excludedDescriptors)) {
1514
+ if (Object.prototype.hasOwnProperty.call(ExcludedMachine.prototype, key)) {
1515
+ delete ExcludedMachine.prototype[key];
1516
+ }
1517
+ }
1518
+ }
1519
+ return ExcludedMachine;
1520
+ }
1521
+ function machineUnion(...instances) {
1522
+ const constructors = instances.map((i) => i.constructor);
1523
+ const contexts = instances.map((i) => i.context);
1524
+ const mergedContext = Object.assign({}, ...contexts);
1525
+ const CombinedClass = MachineUnion(...constructors);
1526
+ return new CombinedClass(mergedContext);
1527
+ }
1528
+ function machineExclude(source, ...excluded) {
1529
+ const sourceCtor = source.constructor;
1530
+ const excludedCtors = excluded.map((e) => e.constructor);
1531
+ const ExcludedClass = MachineExclude(sourceCtor, ...excludedCtors);
1532
+ return new ExcludedClass(source.context);
1533
+ }
1534
+
1437
1535
  // src/utils.ts
1438
1536
  function isState(machine, machineClass) {
1439
1537
  return machine instanceof machineClass;
@@ -2036,6 +2134,8 @@ export {
2036
2134
  CANCEL,
2037
2135
  META_KEY,
2038
2136
  MachineBase,
2137
+ MachineExclude,
2138
+ MachineUnion,
2039
2139
  MiddlewareBuilder,
2040
2140
  MultiMachineBase,
2041
2141
  action,
@@ -2098,6 +2198,8 @@ export {
2098
2198
  isPipelineConfig,
2099
2199
  isState,
2100
2200
  logState,
2201
+ machineExclude,
2202
+ machineUnion,
2101
2203
  matchMachine,
2102
2204
  mergeContext,
2103
2205
  metadata,