@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.
@@ -122,6 +122,43 @@ __export(entry_react_exports, {
122
122
  });
123
123
  module.exports = __toCommonJS(entry_react_exports);
124
124
 
125
+ // src/internal-transitions.ts
126
+ var TRANSITIONS_SYMBOL = Symbol.for("__machine_transitions__");
127
+ function attachTransitions(machine, transitions) {
128
+ Object.defineProperty(machine, TRANSITIONS_SYMBOL, {
129
+ value: transitions,
130
+ enumerable: false,
131
+ configurable: false
132
+ });
133
+ return machine;
134
+ }
135
+ function getStoredTransitions(machine) {
136
+ if (!machine || typeof machine !== "object") {
137
+ return void 0;
138
+ }
139
+ return machine[TRANSITIONS_SYMBOL];
140
+ }
141
+ function snapshotOwnTransitions(source) {
142
+ if (!source || typeof source !== "object") {
143
+ return {};
144
+ }
145
+ const entries = Object.entries(source).filter(
146
+ ([key, value]) => key !== "context" && typeof value === "function"
147
+ );
148
+ return Object.fromEntries(entries);
149
+ }
150
+
151
+ // src/base.ts
152
+ var MachineBase = class {
153
+ /**
154
+ * Initializes a new machine instance with its starting context.
155
+ * @param context - The initial state of the machine.
156
+ */
157
+ constructor(context) {
158
+ this.context = context;
159
+ }
160
+ };
161
+
125
162
  // src/generators.ts
126
163
  function run(flow, initial) {
127
164
  const generator = flow(initial);
@@ -1689,13 +1726,12 @@ function createMachine(context, fnsOrFactory) {
1689
1726
  return createMachine(newContext, transitions2);
1690
1727
  };
1691
1728
  transitions2 = fnsOrFactory(transition);
1692
- return Object.assign({ context }, transitions2);
1729
+ return attachTransitions(Object.assign({ context }, transitions2), transitions2);
1693
1730
  }
1694
- const transitions = "context" in fnsOrFactory ? Object.fromEntries(
1695
- Object.entries(fnsOrFactory).filter(([key]) => key !== "context")
1696
- ) : fnsOrFactory;
1731
+ const stored = getStoredTransitions(fnsOrFactory);
1732
+ const transitions = stored != null ? stored : "context" in fnsOrFactory ? snapshotOwnTransitions(fnsOrFactory) : fnsOrFactory;
1697
1733
  const machine = Object.assign({ context }, transitions);
1698
- return machine;
1734
+ return attachTransitions(machine, transitions);
1699
1735
  }
1700
1736
  function createAsyncMachine(context, fnsOrFactory) {
1701
1737
  if (typeof fnsOrFactory === "function") {
@@ -1704,13 +1740,12 @@ function createAsyncMachine(context, fnsOrFactory) {
1704
1740
  return createAsyncMachine(newContext, transitions2);
1705
1741
  };
1706
1742
  transitions2 = fnsOrFactory(transition);
1707
- return Object.assign({ context }, transitions2);
1743
+ return attachTransitions(Object.assign({ context }, transitions2), transitions2);
1708
1744
  }
1709
- const transitions = "context" in fnsOrFactory ? Object.fromEntries(
1710
- Object.entries(fnsOrFactory).filter(([key]) => key !== "context")
1711
- ) : fnsOrFactory;
1745
+ const stored = getStoredTransitions(fnsOrFactory);
1746
+ const transitions = stored != null ? stored : "context" in fnsOrFactory ? snapshotOwnTransitions(fnsOrFactory) : fnsOrFactory;
1712
1747
  const machine = Object.assign({ context }, transitions);
1713
- return machine;
1748
+ return attachTransitions(machine, transitions);
1714
1749
  }
1715
1750
  function createMachineFactory() {
1716
1751
  return (transformers) => {
@@ -1729,8 +1764,10 @@ function createMachineFactory() {
1729
1764
  };
1730
1765
  }
1731
1766
  function setContext(machine, newContextOrFn) {
1732
- const { context, ...transitions } = machine;
1733
- const newContext = typeof newContextOrFn === "function" ? newContextOrFn(context) : newContextOrFn;
1767
+ var _a;
1768
+ const currentContext = machine.context;
1769
+ const transitions = (_a = getStoredTransitions(machine)) != null ? _a : snapshotOwnTransitions(machine);
1770
+ const newContext = typeof newContextOrFn === "function" ? newContextOrFn(currentContext) : newContextOrFn;
1734
1771
  return createMachine(newContext, transitions);
1735
1772
  }
1736
1773
  function overrideTransitions(machine, overrides) {
@@ -1816,18 +1853,8 @@ function runMachine(initial, onChange) {
1816
1853
  }
1817
1854
  };
1818
1855
  }
1819
- var MachineBase = class {
1820
- /**
1821
- * Initializes a new machine instance with its starting context.
1822
- * @param context - The initial state of the machine.
1823
- */
1824
- constructor(context) {
1825
- this.context = context;
1826
- }
1827
- };
1828
1856
  function next(m, update) {
1829
- const { context, ...transitions } = m;
1830
- return createMachine(update(context), transitions);
1857
+ return setContext(m, (ctx) => update(ctx));
1831
1858
  }
1832
1859
 
1833
1860
  // src/react.ts