@doeixd/machine 1.0.1 → 1.0.3

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.
@@ -1,3 +1,40 @@
1
+ // src/internal-transitions.ts
2
+ var TRANSITIONS_SYMBOL = Symbol.for("__machine_transitions__");
3
+ function attachTransitions(machine, transitions) {
4
+ Object.defineProperty(machine, TRANSITIONS_SYMBOL, {
5
+ value: transitions,
6
+ enumerable: false,
7
+ configurable: false
8
+ });
9
+ return machine;
10
+ }
11
+ function getStoredTransitions(machine) {
12
+ if (!machine || typeof machine !== "object") {
13
+ return void 0;
14
+ }
15
+ return machine[TRANSITIONS_SYMBOL];
16
+ }
17
+ function snapshotOwnTransitions(source) {
18
+ if (!source || typeof source !== "object") {
19
+ return {};
20
+ }
21
+ const entries = Object.entries(source).filter(
22
+ ([key, value]) => key !== "context" && typeof value === "function"
23
+ );
24
+ return Object.fromEntries(entries);
25
+ }
26
+
27
+ // src/base.ts
28
+ var MachineBase = class {
29
+ /**
30
+ * Initializes a new machine instance with its starting context.
31
+ * @param context - The initial state of the machine.
32
+ */
33
+ constructor(context) {
34
+ this.context = context;
35
+ }
36
+ };
37
+
1
38
  // src/generators.ts
2
39
  function run(flow, initial) {
3
40
  const generator = flow(initial);
@@ -1565,13 +1602,12 @@ function createMachine(context, fnsOrFactory) {
1565
1602
  return createMachine(newContext, transitions2);
1566
1603
  };
1567
1604
  transitions2 = fnsOrFactory(transition);
1568
- return Object.assign({ context }, transitions2);
1605
+ return attachTransitions(Object.assign({ context }, transitions2), transitions2);
1569
1606
  }
1570
- const transitions = "context" in fnsOrFactory ? Object.fromEntries(
1571
- Object.entries(fnsOrFactory).filter(([key]) => key !== "context")
1572
- ) : fnsOrFactory;
1607
+ const stored = getStoredTransitions(fnsOrFactory);
1608
+ const transitions = stored != null ? stored : "context" in fnsOrFactory ? snapshotOwnTransitions(fnsOrFactory) : fnsOrFactory;
1573
1609
  const machine = Object.assign({ context }, transitions);
1574
- return machine;
1610
+ return attachTransitions(machine, transitions);
1575
1611
  }
1576
1612
  function createAsyncMachine(context, fnsOrFactory) {
1577
1613
  if (typeof fnsOrFactory === "function") {
@@ -1580,13 +1616,12 @@ function createAsyncMachine(context, fnsOrFactory) {
1580
1616
  return createAsyncMachine(newContext, transitions2);
1581
1617
  };
1582
1618
  transitions2 = fnsOrFactory(transition);
1583
- return Object.assign({ context }, transitions2);
1619
+ return attachTransitions(Object.assign({ context }, transitions2), transitions2);
1584
1620
  }
1585
- const transitions = "context" in fnsOrFactory ? Object.fromEntries(
1586
- Object.entries(fnsOrFactory).filter(([key]) => key !== "context")
1587
- ) : fnsOrFactory;
1621
+ const stored = getStoredTransitions(fnsOrFactory);
1622
+ const transitions = stored != null ? stored : "context" in fnsOrFactory ? snapshotOwnTransitions(fnsOrFactory) : fnsOrFactory;
1588
1623
  const machine = Object.assign({ context }, transitions);
1589
- return machine;
1624
+ return attachTransitions(machine, transitions);
1590
1625
  }
1591
1626
  function createMachineFactory() {
1592
1627
  return (transformers) => {
@@ -1605,8 +1640,10 @@ function createMachineFactory() {
1605
1640
  };
1606
1641
  }
1607
1642
  function setContext(machine, newContextOrFn) {
1608
- const { context, ...transitions } = machine;
1609
- const newContext = typeof newContextOrFn === "function" ? newContextOrFn(context) : newContextOrFn;
1643
+ var _a;
1644
+ const currentContext = machine.context;
1645
+ const transitions = (_a = getStoredTransitions(machine)) != null ? _a : snapshotOwnTransitions(machine);
1646
+ const newContext = typeof newContextOrFn === "function" ? newContextOrFn(currentContext) : newContextOrFn;
1610
1647
  return createMachine(newContext, transitions);
1611
1648
  }
1612
1649
  function overrideTransitions(machine, overrides) {
@@ -1692,18 +1729,8 @@ function runMachine(initial, onChange) {
1692
1729
  }
1693
1730
  };
1694
1731
  }
1695
- var MachineBase = class {
1696
- /**
1697
- * Initializes a new machine instance with its starting context.
1698
- * @param context - The initial state of the machine.
1699
- */
1700
- constructor(context) {
1701
- this.context = context;
1702
- }
1703
- };
1704
1732
  function next(m, update) {
1705
- const { context, ...transitions } = m;
1706
- return createMachine(update(context), transitions);
1733
+ return setContext(m, (ctx) => update(ctx));
1707
1734
  }
1708
1735
 
1709
1736
  // src/react.ts