@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.
@@ -116,6 +116,32 @@ __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
+
119
145
  // src/generators.ts
120
146
  function run(flow, initial) {
121
147
  const generator = flow(initial);
@@ -1683,13 +1709,12 @@ function createMachine(context, fnsOrFactory) {
1683
1709
  return createMachine(newContext, transitions2);
1684
1710
  };
1685
1711
  transitions2 = fnsOrFactory(transition);
1686
- return Object.assign({ context }, transitions2);
1712
+ return attachTransitions(Object.assign({ context }, transitions2), transitions2);
1687
1713
  }
1688
- const transitions = "context" in fnsOrFactory ? Object.fromEntries(
1689
- Object.entries(fnsOrFactory).filter(([key]) => key !== "context")
1690
- ) : fnsOrFactory;
1714
+ const stored = getStoredTransitions(fnsOrFactory);
1715
+ const transitions = stored != null ? stored : "context" in fnsOrFactory ? snapshotOwnTransitions(fnsOrFactory) : fnsOrFactory;
1691
1716
  const machine = Object.assign({ context }, transitions);
1692
- return machine;
1717
+ return attachTransitions(machine, transitions);
1693
1718
  }
1694
1719
  function createAsyncMachine(context, fnsOrFactory) {
1695
1720
  if (typeof fnsOrFactory === "function") {
@@ -1698,13 +1723,12 @@ function createAsyncMachine(context, fnsOrFactory) {
1698
1723
  return createAsyncMachine(newContext, transitions2);
1699
1724
  };
1700
1725
  transitions2 = fnsOrFactory(transition);
1701
- return Object.assign({ context }, transitions2);
1726
+ return attachTransitions(Object.assign({ context }, transitions2), transitions2);
1702
1727
  }
1703
- const transitions = "context" in fnsOrFactory ? Object.fromEntries(
1704
- Object.entries(fnsOrFactory).filter(([key]) => key !== "context")
1705
- ) : fnsOrFactory;
1728
+ const stored = getStoredTransitions(fnsOrFactory);
1729
+ const transitions = stored != null ? stored : "context" in fnsOrFactory ? snapshotOwnTransitions(fnsOrFactory) : fnsOrFactory;
1706
1730
  const machine = Object.assign({ context }, transitions);
1707
- return machine;
1731
+ return attachTransitions(machine, transitions);
1708
1732
  }
1709
1733
  function createMachineFactory() {
1710
1734
  return (transformers) => {
@@ -1723,8 +1747,10 @@ function createMachineFactory() {
1723
1747
  };
1724
1748
  }
1725
1749
  function setContext(machine, newContextOrFn) {
1726
- const { context, ...transitions } = machine;
1727
- const newContext = typeof newContextOrFn === "function" ? newContextOrFn(context) : newContextOrFn;
1750
+ var _a;
1751
+ const currentContext = machine.context;
1752
+ const transitions = (_a = getStoredTransitions(machine)) != null ? _a : snapshotOwnTransitions(machine);
1753
+ const newContext = typeof newContextOrFn === "function" ? newContextOrFn(currentContext) : newContextOrFn;
1728
1754
  return createMachine(newContext, transitions);
1729
1755
  }
1730
1756
  function overrideTransitions(machine, overrides) {
@@ -1820,7 +1846,6 @@ var MachineBase = class {
1820
1846
  }
1821
1847
  };
1822
1848
  function next(m, update) {
1823
- const { context, ...transitions } = m;
1824
- return createMachine(update(context), transitions);
1849
+ return setContext(m, (ctx) => update(ctx));
1825
1850
  }
1826
1851
  //# sourceMappingURL=core.js.map