@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.
@@ -134,6 +134,43 @@ __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
+
163
+ // src/base.ts
164
+ var MachineBase = class {
165
+ /**
166
+ * Initializes a new machine instance with its starting context.
167
+ * @param context - The initial state of the machine.
168
+ */
169
+ constructor(context) {
170
+ this.context = context;
171
+ }
172
+ };
173
+
137
174
  // src/generators.ts
138
175
  function run(flow, initial) {
139
176
  const generator = flow(initial);
@@ -2100,7 +2137,18 @@ function createContextBoundMachine(initialContext, transformers) {
2100
2137
  }
2101
2138
  ])
2102
2139
  );
2103
- return Object.assign({ context: initialContext }, boundTransitions);
2140
+ Object.values(boundTransitions).forEach((fn) => {
2141
+ if (typeof fn === "function") {
2142
+ Object.defineProperty(fn, "__contextBound", {
2143
+ value: true,
2144
+ enumerable: false
2145
+ });
2146
+ }
2147
+ });
2148
+ return attachTransitions(
2149
+ Object.assign({ context: initialContext }, boundTransitions),
2150
+ boundTransitions
2151
+ );
2104
2152
  }
2105
2153
  function callWithContext(machine, transitionName, ...args) {
2106
2154
  const fn = machine[transitionName];
@@ -2123,13 +2171,12 @@ function createMachine(context, fnsOrFactory) {
2123
2171
  return createMachine(newContext, transitions2);
2124
2172
  };
2125
2173
  transitions2 = fnsOrFactory(transition);
2126
- return Object.assign({ context }, transitions2);
2174
+ return attachTransitions(Object.assign({ context }, transitions2), transitions2);
2127
2175
  }
2128
- const transitions = "context" in fnsOrFactory ? Object.fromEntries(
2129
- Object.entries(fnsOrFactory).filter(([key]) => key !== "context")
2130
- ) : fnsOrFactory;
2176
+ const stored = getStoredTransitions(fnsOrFactory);
2177
+ const transitions = stored != null ? stored : "context" in fnsOrFactory ? snapshotOwnTransitions(fnsOrFactory) : fnsOrFactory;
2131
2178
  const machine = Object.assign({ context }, transitions);
2132
- return machine;
2179
+ return attachTransitions(machine, transitions);
2133
2180
  }
2134
2181
  function createAsyncMachine(context, fnsOrFactory) {
2135
2182
  if (typeof fnsOrFactory === "function") {
@@ -2138,13 +2185,12 @@ function createAsyncMachine(context, fnsOrFactory) {
2138
2185
  return createAsyncMachine(newContext, transitions2);
2139
2186
  };
2140
2187
  transitions2 = fnsOrFactory(transition);
2141
- return Object.assign({ context }, transitions2);
2188
+ return attachTransitions(Object.assign({ context }, transitions2), transitions2);
2142
2189
  }
2143
- const transitions = "context" in fnsOrFactory ? Object.fromEntries(
2144
- Object.entries(fnsOrFactory).filter(([key]) => key !== "context")
2145
- ) : fnsOrFactory;
2190
+ const stored = getStoredTransitions(fnsOrFactory);
2191
+ const transitions = stored != null ? stored : "context" in fnsOrFactory ? snapshotOwnTransitions(fnsOrFactory) : fnsOrFactory;
2146
2192
  const machine = Object.assign({ context }, transitions);
2147
- return machine;
2193
+ return attachTransitions(machine, transitions);
2148
2194
  }
2149
2195
  function createMachineFactory() {
2150
2196
  return (transformers) => {
@@ -2163,8 +2209,10 @@ function createMachineFactory() {
2163
2209
  };
2164
2210
  }
2165
2211
  function setContext(machine, newContextOrFn) {
2166
- const { context, ...transitions } = machine;
2167
- const newContext = typeof newContextOrFn === "function" ? newContextOrFn(context) : newContextOrFn;
2212
+ var _a;
2213
+ const currentContext = machine.context;
2214
+ const transitions = (_a = getStoredTransitions(machine)) != null ? _a : snapshotOwnTransitions(machine);
2215
+ const newContext = typeof newContextOrFn === "function" ? newContextOrFn(currentContext) : newContextOrFn;
2168
2216
  return createMachine(newContext, transitions);
2169
2217
  }
2170
2218
  function createContext(context) {
@@ -2253,17 +2301,7 @@ function runMachine(initial, onChange) {
2253
2301
  }
2254
2302
  };
2255
2303
  }
2256
- var MachineBase = class {
2257
- /**
2258
- * Initializes a new machine instance with its starting context.
2259
- * @param context - The initial state of the machine.
2260
- */
2261
- constructor(context) {
2262
- this.context = context;
2263
- }
2264
- };
2265
2304
  function next(m, update) {
2266
- const { context, ...transitions } = m;
2267
- return createMachine(update(context), transitions);
2305
+ return setContext(m, (ctx) => update(ctx));
2268
2306
  }
2269
2307
  //# sourceMappingURL=index.js.map