@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.
@@ -116,6 +116,43 @@ __export(core_exports, {
116
116
  });
117
117
  module.exports = __toCommonJS(core_exports);
118
118
 
119
+ // src/internal-transitions.ts
120
+ var TRANSITIONS_SYMBOL = Symbol.for("__machine_transitions__");
121
+ function attachTransitions(machine, transitions) {
122
+ Object.defineProperty(machine, TRANSITIONS_SYMBOL, {
123
+ value: transitions,
124
+ enumerable: false,
125
+ configurable: false
126
+ });
127
+ return machine;
128
+ }
129
+ function getStoredTransitions(machine) {
130
+ if (!machine || typeof machine !== "object") {
131
+ return void 0;
132
+ }
133
+ return machine[TRANSITIONS_SYMBOL];
134
+ }
135
+ function snapshotOwnTransitions(source) {
136
+ if (!source || typeof source !== "object") {
137
+ return {};
138
+ }
139
+ const entries = Object.entries(source).filter(
140
+ ([key, value]) => key !== "context" && typeof value === "function"
141
+ );
142
+ return Object.fromEntries(entries);
143
+ }
144
+
145
+ // src/base.ts
146
+ var MachineBase = class {
147
+ /**
148
+ * Initializes a new machine instance with its starting context.
149
+ * @param context - The initial state of the machine.
150
+ */
151
+ constructor(context) {
152
+ this.context = context;
153
+ }
154
+ };
155
+
119
156
  // src/generators.ts
120
157
  function run(flow, initial) {
121
158
  const generator = flow(initial);
@@ -1683,13 +1720,12 @@ function createMachine(context, fnsOrFactory) {
1683
1720
  return createMachine(newContext, transitions2);
1684
1721
  };
1685
1722
  transitions2 = fnsOrFactory(transition);
1686
- return Object.assign({ context }, transitions2);
1723
+ return attachTransitions(Object.assign({ context }, transitions2), transitions2);
1687
1724
  }
1688
- const transitions = "context" in fnsOrFactory ? Object.fromEntries(
1689
- Object.entries(fnsOrFactory).filter(([key]) => key !== "context")
1690
- ) : fnsOrFactory;
1725
+ const stored = getStoredTransitions(fnsOrFactory);
1726
+ const transitions = stored != null ? stored : "context" in fnsOrFactory ? snapshotOwnTransitions(fnsOrFactory) : fnsOrFactory;
1691
1727
  const machine = Object.assign({ context }, transitions);
1692
- return machine;
1728
+ return attachTransitions(machine, transitions);
1693
1729
  }
1694
1730
  function createAsyncMachine(context, fnsOrFactory) {
1695
1731
  if (typeof fnsOrFactory === "function") {
@@ -1698,13 +1734,12 @@ function createAsyncMachine(context, fnsOrFactory) {
1698
1734
  return createAsyncMachine(newContext, transitions2);
1699
1735
  };
1700
1736
  transitions2 = fnsOrFactory(transition);
1701
- return Object.assign({ context }, transitions2);
1737
+ return attachTransitions(Object.assign({ context }, transitions2), transitions2);
1702
1738
  }
1703
- const transitions = "context" in fnsOrFactory ? Object.fromEntries(
1704
- Object.entries(fnsOrFactory).filter(([key]) => key !== "context")
1705
- ) : fnsOrFactory;
1739
+ const stored = getStoredTransitions(fnsOrFactory);
1740
+ const transitions = stored != null ? stored : "context" in fnsOrFactory ? snapshotOwnTransitions(fnsOrFactory) : fnsOrFactory;
1706
1741
  const machine = Object.assign({ context }, transitions);
1707
- return machine;
1742
+ return attachTransitions(machine, transitions);
1708
1743
  }
1709
1744
  function createMachineFactory() {
1710
1745
  return (transformers) => {
@@ -1723,8 +1758,10 @@ function createMachineFactory() {
1723
1758
  };
1724
1759
  }
1725
1760
  function setContext(machine, newContextOrFn) {
1726
- const { context, ...transitions } = machine;
1727
- const newContext = typeof newContextOrFn === "function" ? newContextOrFn(context) : newContextOrFn;
1761
+ var _a;
1762
+ const currentContext = machine.context;
1763
+ const transitions = (_a = getStoredTransitions(machine)) != null ? _a : snapshotOwnTransitions(machine);
1764
+ const newContext = typeof newContextOrFn === "function" ? newContextOrFn(currentContext) : newContextOrFn;
1728
1765
  return createMachine(newContext, transitions);
1729
1766
  }
1730
1767
  function overrideTransitions(machine, overrides) {
@@ -1810,17 +1847,7 @@ function runMachine(initial, onChange) {
1810
1847
  }
1811
1848
  };
1812
1849
  }
1813
- var MachineBase = class {
1814
- /**
1815
- * Initializes a new machine instance with its starting context.
1816
- * @param context - The initial state of the machine.
1817
- */
1818
- constructor(context) {
1819
- this.context = context;
1820
- }
1821
- };
1822
1850
  function next(m, update) {
1823
- const { context, ...transitions } = m;
1824
- return createMachine(update(context), transitions);
1851
+ return setContext(m, (ctx) => update(ctx));
1825
1852
  }
1826
1853
  //# sourceMappingURL=core.js.map