@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.
@@ -134,6 +134,32 @@ __export(src_exports, {
134
134
  });
135
135
  module.exports = __toCommonJS(src_exports);
136
136
 
137
+ // src/internal-transitions.ts
138
+ var TRANSITIONS_SYMBOL = Symbol.for("__machine_transitions__");
139
+ function attachTransitions(machine, transitions) {
140
+ Object.defineProperty(machine, TRANSITIONS_SYMBOL, {
141
+ value: transitions,
142
+ enumerable: false,
143
+ configurable: false
144
+ });
145
+ return machine;
146
+ }
147
+ function getStoredTransitions(machine) {
148
+ if (!machine || typeof machine !== "object") {
149
+ return void 0;
150
+ }
151
+ return machine[TRANSITIONS_SYMBOL];
152
+ }
153
+ function snapshotOwnTransitions(source) {
154
+ if (!source || typeof source !== "object") {
155
+ return {};
156
+ }
157
+ const entries = Object.entries(source).filter(
158
+ ([key, value]) => key !== "context" && typeof value === "function"
159
+ );
160
+ return Object.fromEntries(entries);
161
+ }
162
+
137
163
  // src/generators.ts
138
164
  function run(flow, initial) {
139
165
  const generator = flow(initial);
@@ -2100,7 +2126,18 @@ function createContextBoundMachine(initialContext, transformers) {
2100
2126
  }
2101
2127
  ])
2102
2128
  );
2103
- return Object.assign({ context: initialContext }, boundTransitions);
2129
+ Object.values(boundTransitions).forEach((fn) => {
2130
+ if (typeof fn === "function") {
2131
+ Object.defineProperty(fn, "__contextBound", {
2132
+ value: true,
2133
+ enumerable: false
2134
+ });
2135
+ }
2136
+ });
2137
+ return attachTransitions(
2138
+ Object.assign({ context: initialContext }, boundTransitions),
2139
+ boundTransitions
2140
+ );
2104
2141
  }
2105
2142
  function callWithContext(machine, transitionName, ...args) {
2106
2143
  const fn = machine[transitionName];
@@ -2123,13 +2160,12 @@ function createMachine(context, fnsOrFactory) {
2123
2160
  return createMachine(newContext, transitions2);
2124
2161
  };
2125
2162
  transitions2 = fnsOrFactory(transition);
2126
- return Object.assign({ context }, transitions2);
2163
+ return attachTransitions(Object.assign({ context }, transitions2), transitions2);
2127
2164
  }
2128
- const transitions = "context" in fnsOrFactory ? Object.fromEntries(
2129
- Object.entries(fnsOrFactory).filter(([key]) => key !== "context")
2130
- ) : fnsOrFactory;
2165
+ const stored = getStoredTransitions(fnsOrFactory);
2166
+ const transitions = stored != null ? stored : "context" in fnsOrFactory ? snapshotOwnTransitions(fnsOrFactory) : fnsOrFactory;
2131
2167
  const machine = Object.assign({ context }, transitions);
2132
- return machine;
2168
+ return attachTransitions(machine, transitions);
2133
2169
  }
2134
2170
  function createAsyncMachine(context, fnsOrFactory) {
2135
2171
  if (typeof fnsOrFactory === "function") {
@@ -2138,13 +2174,12 @@ function createAsyncMachine(context, fnsOrFactory) {
2138
2174
  return createAsyncMachine(newContext, transitions2);
2139
2175
  };
2140
2176
  transitions2 = fnsOrFactory(transition);
2141
- return Object.assign({ context }, transitions2);
2177
+ return attachTransitions(Object.assign({ context }, transitions2), transitions2);
2142
2178
  }
2143
- const transitions = "context" in fnsOrFactory ? Object.fromEntries(
2144
- Object.entries(fnsOrFactory).filter(([key]) => key !== "context")
2145
- ) : fnsOrFactory;
2179
+ const stored = getStoredTransitions(fnsOrFactory);
2180
+ const transitions = stored != null ? stored : "context" in fnsOrFactory ? snapshotOwnTransitions(fnsOrFactory) : fnsOrFactory;
2146
2181
  const machine = Object.assign({ context }, transitions);
2147
- return machine;
2182
+ return attachTransitions(machine, transitions);
2148
2183
  }
2149
2184
  function createMachineFactory() {
2150
2185
  return (transformers) => {
@@ -2163,8 +2198,10 @@ function createMachineFactory() {
2163
2198
  };
2164
2199
  }
2165
2200
  function setContext(machine, newContextOrFn) {
2166
- const { context, ...transitions } = machine;
2167
- const newContext = typeof newContextOrFn === "function" ? newContextOrFn(context) : newContextOrFn;
2201
+ var _a;
2202
+ const currentContext = machine.context;
2203
+ const transitions = (_a = getStoredTransitions(machine)) != null ? _a : snapshotOwnTransitions(machine);
2204
+ const newContext = typeof newContextOrFn === "function" ? newContextOrFn(currentContext) : newContextOrFn;
2168
2205
  return createMachine(newContext, transitions);
2169
2206
  }
2170
2207
  function createContext(context) {
@@ -2263,7 +2300,6 @@ var MachineBase = class {
2263
2300
  }
2264
2301
  };
2265
2302
  function next(m, update) {
2266
- const { context, ...transitions } = m;
2267
- return createMachine(update(context), transitions);
2303
+ return setContext(m, (ctx) => update(ctx));
2268
2304
  }
2269
2305
  //# sourceMappingURL=index.js.map