@doeixd/machine 1.0.1 → 1.0.2

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,32 @@ __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
+
125
151
  // src/generators.ts
126
152
  function run(flow, initial) {
127
153
  const generator = flow(initial);
@@ -1689,13 +1715,12 @@ function createMachine(context, fnsOrFactory) {
1689
1715
  return createMachine(newContext, transitions2);
1690
1716
  };
1691
1717
  transitions2 = fnsOrFactory(transition);
1692
- return Object.assign({ context }, transitions2);
1718
+ return attachTransitions(Object.assign({ context }, transitions2), transitions2);
1693
1719
  }
1694
- const transitions = "context" in fnsOrFactory ? Object.fromEntries(
1695
- Object.entries(fnsOrFactory).filter(([key]) => key !== "context")
1696
- ) : fnsOrFactory;
1720
+ const stored = getStoredTransitions(fnsOrFactory);
1721
+ const transitions = stored != null ? stored : "context" in fnsOrFactory ? snapshotOwnTransitions(fnsOrFactory) : fnsOrFactory;
1697
1722
  const machine = Object.assign({ context }, transitions);
1698
- return machine;
1723
+ return attachTransitions(machine, transitions);
1699
1724
  }
1700
1725
  function createAsyncMachine(context, fnsOrFactory) {
1701
1726
  if (typeof fnsOrFactory === "function") {
@@ -1704,13 +1729,12 @@ function createAsyncMachine(context, fnsOrFactory) {
1704
1729
  return createAsyncMachine(newContext, transitions2);
1705
1730
  };
1706
1731
  transitions2 = fnsOrFactory(transition);
1707
- return Object.assign({ context }, transitions2);
1732
+ return attachTransitions(Object.assign({ context }, transitions2), transitions2);
1708
1733
  }
1709
- const transitions = "context" in fnsOrFactory ? Object.fromEntries(
1710
- Object.entries(fnsOrFactory).filter(([key]) => key !== "context")
1711
- ) : fnsOrFactory;
1734
+ const stored = getStoredTransitions(fnsOrFactory);
1735
+ const transitions = stored != null ? stored : "context" in fnsOrFactory ? snapshotOwnTransitions(fnsOrFactory) : fnsOrFactory;
1712
1736
  const machine = Object.assign({ context }, transitions);
1713
- return machine;
1737
+ return attachTransitions(machine, transitions);
1714
1738
  }
1715
1739
  function createMachineFactory() {
1716
1740
  return (transformers) => {
@@ -1729,8 +1753,10 @@ function createMachineFactory() {
1729
1753
  };
1730
1754
  }
1731
1755
  function setContext(machine, newContextOrFn) {
1732
- const { context, ...transitions } = machine;
1733
- const newContext = typeof newContextOrFn === "function" ? newContextOrFn(context) : newContextOrFn;
1756
+ var _a;
1757
+ const currentContext = machine.context;
1758
+ const transitions = (_a = getStoredTransitions(machine)) != null ? _a : snapshotOwnTransitions(machine);
1759
+ const newContext = typeof newContextOrFn === "function" ? newContextOrFn(currentContext) : newContextOrFn;
1734
1760
  return createMachine(newContext, transitions);
1735
1761
  }
1736
1762
  function overrideTransitions(machine, overrides) {
@@ -1826,8 +1852,7 @@ var MachineBase = class {
1826
1852
  }
1827
1853
  };
1828
1854
  function next(m, update) {
1829
- const { context, ...transitions } = m;
1830
- return createMachine(update(context), transitions);
1855
+ return setContext(m, (ctx) => update(ctx));
1831
1856
  }
1832
1857
 
1833
1858
  // src/react.ts