@doeixd/machine 0.0.23 → 1.0.1

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.
Files changed (50) hide show
  1. package/README.md +101 -65
  2. package/dist/cjs/development/core.js +19 -45
  3. package/dist/cjs/development/core.js.map +3 -3
  4. package/dist/cjs/development/index.js +51 -46
  5. package/dist/cjs/development/index.js.map +4 -4
  6. package/dist/cjs/development/react.js +19 -46
  7. package/dist/cjs/development/react.js.map +3 -3
  8. package/dist/cjs/production/core.js +1 -1
  9. package/dist/cjs/production/index.js +3 -3
  10. package/dist/cjs/production/react.js +1 -1
  11. package/dist/esm/development/core.js +19 -45
  12. package/dist/esm/development/core.js.map +3 -3
  13. package/dist/esm/development/index.js +51 -46
  14. package/dist/esm/development/index.js.map +4 -4
  15. package/dist/esm/development/react.js +19 -46
  16. package/dist/esm/development/react.js.map +3 -3
  17. package/dist/esm/production/core.js +1 -1
  18. package/dist/esm/production/index.js +3 -3
  19. package/dist/esm/production/react.js +1 -1
  20. package/dist/types/actor.d.ts +4 -4
  21. package/dist/types/actor.d.ts.map +1 -1
  22. package/dist/types/context-bound.d.ts +94 -0
  23. package/dist/types/context-bound.d.ts.map +1 -0
  24. package/dist/types/entry-react.d.ts +1 -1
  25. package/dist/types/entry-react.d.ts.map +1 -1
  26. package/dist/types/functional-combinators.d.ts +5 -5
  27. package/dist/types/generators.d.ts +2 -2
  28. package/dist/types/index.d.ts +14 -29
  29. package/dist/types/index.d.ts.map +1 -1
  30. package/dist/types/primitives.d.ts +25 -5
  31. package/dist/types/primitives.d.ts.map +1 -1
  32. package/dist/types/react.d.ts.map +1 -1
  33. package/dist/types/utils.d.ts +22 -22
  34. package/dist/types/utils.d.ts.map +1 -1
  35. package/package.json +1 -1
  36. package/src/actor.ts +1 -1
  37. package/src/context-bound.ts +147 -0
  38. package/src/entry-react.ts +9 -2
  39. package/src/functional-combinators.ts +5 -5
  40. package/src/generators.ts +2 -2
  41. package/src/higher-order.ts +2 -2
  42. package/src/index.ts +31 -68
  43. package/src/middleware/time-travel.ts +2 -2
  44. package/src/middleware.ts +2 -2
  45. package/src/multi.ts +4 -4
  46. package/src/primitives.ts +34 -14
  47. package/src/prototype_functional.ts +2 -2
  48. package/src/react.ts +1 -2
  49. package/src/test.ts +7 -7
  50. package/src/utils.ts +31 -31
@@ -33,6 +33,7 @@ __export(src_exports, {
33
33
  bindTransitions: () => bindTransitions,
34
34
  branch: () => branch,
35
35
  call: () => call,
36
+ callWithContext: () => callWithContext,
36
37
  chain: () => chain,
37
38
  classCase: () => classCase,
38
39
  combine: () => combine,
@@ -42,6 +43,7 @@ __export(src_exports, {
42
43
  createActor: () => createActor,
43
44
  createAsyncMachine: () => createAsyncMachine,
44
45
  createContext: () => createContext,
46
+ createContextBoundMachine: () => createContextBoundMachine,
45
47
  createCustomMiddleware: () => createCustomMiddleware,
46
48
  createEnsemble: () => createEnsemble,
47
49
  createEnsembleFactory: () => createEnsembleFactory,
@@ -79,6 +81,7 @@ __export(src_exports, {
79
81
  inDevelopment: () => inDevelopment,
80
82
  invoke: () => invoke,
81
83
  isConditionalMiddleware: () => isConditionalMiddleware,
84
+ isContextBound: () => isContextBound,
82
85
  isMiddlewareContext: () => isMiddlewareContext,
83
86
  isMiddlewareError: () => isMiddlewareError,
84
87
  isMiddlewareFn: () => isMiddlewareFn,
@@ -261,8 +264,8 @@ function guard(condition, transition, options = {}) {
261
264
  const ctx = isMachine ? this.context : this;
262
265
  const conditionResult = condition(ctx, ...args);
263
266
  if (conditionResult) {
264
- const contextForTransition = isMachine ? this.context : this;
265
- return transition.apply(contextForTransition, args);
267
+ const machineForTransition = isMachine ? this : { context: this };
268
+ return transition.apply(machineForTransition, args);
266
269
  } else {
267
270
  if (onFail === "throw") {
268
271
  const message = errorMessage || "Guard condition failed";
@@ -302,8 +305,8 @@ function guardAsync(condition, transition, options = {}) {
302
305
  const ctx = isMachine ? this.context : this;
303
306
  const conditionResult = await Promise.resolve(condition(ctx, ...args));
304
307
  if (conditionResult) {
305
- const contextForTransition = isMachine ? this.context : this;
306
- return transition.apply(contextForTransition, args);
308
+ const machineForTransition = isMachine ? this : { context: this };
309
+ return transition.apply(machineForTransition, args);
307
310
  } else {
308
311
  if (onFail === "throw") {
309
312
  const message = errorMessage || "Guard condition failed";
@@ -384,7 +387,7 @@ function createRunner(initialMachine, onChange) {
384
387
  return void 0;
385
388
  }
386
389
  return (...args) => {
387
- const nextState = transition.apply(currentMachine.context, args);
390
+ const nextState = transition.apply(currentMachine, args);
388
391
  const nextStateWithTransitions = Object.assign(
389
392
  { context: nextState.context },
390
393
  originalTransitions
@@ -427,7 +430,7 @@ function createEnsemble(store, factories, getDiscriminant) {
427
430
  );
428
431
  }
429
432
  return (...args) => {
430
- return action2.apply(currentMachine.context, args);
433
+ return action2.apply(currentMachine, args);
431
434
  };
432
435
  }
433
436
  });
@@ -578,7 +581,7 @@ function createMutableMachine(sharedContext, factories, getDiscriminant) {
578
581
  const transition = currentMachine[prop];
579
582
  if (typeof transition === "function") {
580
583
  return (...args) => {
581
- const nextContext = transition.apply(currentMachine.context, args);
584
+ const nextContext = transition.apply(currentMachine, args);
582
585
  if (typeof nextContext !== "object" || nextContext === null) {
583
586
  console.warn(`[MutableMachine] Transition "${String(prop)}" did not return a valid context object. State may be inconsistent.`);
584
587
  return;
@@ -700,14 +703,14 @@ function createParallelMachine(m1, m2) {
700
703
  for (const key in transitions1) {
701
704
  const transitionFn = transitions1[key];
702
705
  combinedTransitions[key] = (...args) => {
703
- const nextM1 = transitionFn.apply(m1.context, args);
706
+ const nextM1 = transitionFn.apply(m1, args);
704
707
  return createParallelMachine(nextM1, m2);
705
708
  };
706
709
  }
707
710
  for (const key in transitions2) {
708
711
  const transitionFn = transitions2[key];
709
712
  combinedTransitions[key] = (...args) => {
710
- const nextM2 = transitionFn.apply(m2.context, args);
713
+ const nextM2 = transitionFn.apply(m2, args);
711
714
  return createParallelMachine(m1, nextM2);
712
715
  };
713
716
  }
@@ -1178,7 +1181,7 @@ function withTimeTravel(machine, options = {}) {
1178
1181
  for (const entry of transitionsToReplay) {
1179
1182
  const transitionFn = replayedMachine[entry.transitionName];
1180
1183
  if (transitionFn) {
1181
- replayedMachine = transitionFn.apply(replayedMachine.context, entry.args);
1184
+ replayedMachine = transitionFn.apply(replayedMachine, entry.args);
1182
1185
  }
1183
1186
  }
1184
1187
  return replayedMachine;
@@ -1696,8 +1699,8 @@ function createTransition(getTransitions, transformer) {
1696
1699
  return createMachine(nextContext, getTransitions());
1697
1700
  };
1698
1701
  }
1699
- function call(fn, context, ...args) {
1700
- return fn.apply(context, args);
1702
+ function call(fn, machine, ...args) {
1703
+ return fn.apply(machine, args);
1701
1704
  }
1702
1705
  function bindTransitions(machine) {
1703
1706
  return new Proxy(machine, {
@@ -1705,7 +1708,7 @@ function bindTransitions(machine) {
1705
1708
  const value = target[prop];
1706
1709
  if (typeof value === "function") {
1707
1710
  return function(...args) {
1708
- const result = value.apply(target.context, args);
1711
+ const result = value.apply(target, args);
1709
1712
  if (result && typeof result === "object" && "context" in result) {
1710
1713
  return bindTransitions(result);
1711
1714
  }
@@ -1730,7 +1733,7 @@ var BoundMachine = class _BoundMachine {
1730
1733
  const value = this.wrappedMachine[prop];
1731
1734
  if (typeof value === "function") {
1732
1735
  return (...args) => {
1733
- const result = value.apply(this.wrappedMachine.context, args);
1736
+ const result = value.apply(this.wrappedMachine, args);
1734
1737
  if (result && typeof result === "object" && "context" in result) {
1735
1738
  return new _BoundMachine(result);
1736
1739
  }
@@ -2006,7 +2009,7 @@ var _Actor = class _Actor {
2006
2009
  }
2007
2010
  let result;
2008
2011
  try {
2009
- result = fn.apply(this._state.context, event.args);
2012
+ result = fn.apply(this._state, event.args);
2010
2013
  } catch (error) {
2011
2014
  console.error(`[Actor] Error in transition '${String(event.type)}':`, error);
2012
2015
  continue;
@@ -2084,28 +2087,43 @@ function fromObservable(observable) {
2084
2087
  return actor;
2085
2088
  }
2086
2089
 
2090
+ // src/context-bound.ts
2091
+ function createContextBoundMachine(initialContext, transformers) {
2092
+ const savedTransformers = transformers;
2093
+ const boundTransitions = Object.fromEntries(
2094
+ Object.entries(transformers).map(([key, transformer]) => [
2095
+ key,
2096
+ function(...args) {
2097
+ const contextOnly = { context: this.context };
2098
+ const newContext = transformer.apply(contextOnly, args);
2099
+ return createContextBoundMachine(newContext, savedTransformers);
2100
+ }
2101
+ ])
2102
+ );
2103
+ return Object.assign({ context: initialContext }, boundTransitions);
2104
+ }
2105
+ function callWithContext(machine, transitionName, ...args) {
2106
+ const fn = machine[transitionName];
2107
+ const contextOnly = { context: machine.context };
2108
+ return fn.apply(contextOnly, args);
2109
+ }
2110
+ function isContextBound(machine) {
2111
+ const firstTransition = Object.values(machine).find(
2112
+ (v) => typeof v === "function"
2113
+ );
2114
+ if (!firstTransition) return false;
2115
+ return firstTransition.__contextBound === true;
2116
+ }
2117
+
2087
2118
  // src/index.ts
2088
2119
  function createMachine(context, fnsOrFactory) {
2089
2120
  if (typeof fnsOrFactory === "function") {
2090
2121
  let transitions2;
2091
2122
  const transition = (newContext) => {
2092
- const machine2 = createMachine(newContext, transitions2);
2093
- const boundTransitions2 = Object.fromEntries(
2094
- Object.entries(transitions2).map(([key, fn]) => [
2095
- key,
2096
- fn.bind(newContext)
2097
- ])
2098
- );
2099
- return Object.assign(machine2, boundTransitions2);
2123
+ return createMachine(newContext, transitions2);
2100
2124
  };
2101
2125
  transitions2 = fnsOrFactory(transition);
2102
- const boundTransitions = Object.fromEntries(
2103
- Object.entries(transitions2).map(([key, fn]) => [
2104
- key,
2105
- fn.bind(context)
2106
- ])
2107
- );
2108
- return Object.assign({ context }, boundTransitions);
2126
+ return Object.assign({ context }, transitions2);
2109
2127
  }
2110
2128
  const transitions = "context" in fnsOrFactory ? Object.fromEntries(
2111
2129
  Object.entries(fnsOrFactory).filter(([key]) => key !== "context")
@@ -2117,23 +2135,10 @@ function createAsyncMachine(context, fnsOrFactory) {
2117
2135
  if (typeof fnsOrFactory === "function") {
2118
2136
  let transitions2;
2119
2137
  const transition = (newContext) => {
2120
- const machine2 = createAsyncMachine(newContext, transitions2);
2121
- const boundTransitions2 = Object.fromEntries(
2122
- Object.entries(transitions2).map(([key, fn]) => [
2123
- key,
2124
- fn.bind(newContext)
2125
- ])
2126
- );
2127
- return Object.assign(machine2, boundTransitions2);
2138
+ return createAsyncMachine(newContext, transitions2);
2128
2139
  };
2129
2140
  transitions2 = fnsOrFactory(transition);
2130
- const boundTransitions = Object.fromEntries(
2131
- Object.entries(transitions2).map(([key, fn]) => [
2132
- key,
2133
- fn.bind(context)
2134
- ])
2135
- );
2136
- return Object.assign({ context }, boundTransitions);
2141
+ return Object.assign({ context }, transitions2);
2137
2142
  }
2138
2143
  const transitions = "context" in fnsOrFactory ? Object.fromEntries(
2139
2144
  Object.entries(fnsOrFactory).filter(([key]) => key !== "context")
@@ -2218,7 +2223,7 @@ function runMachine(initial, onChange) {
2218
2223
  const controller = new AbortController();
2219
2224
  activeController = controller;
2220
2225
  try {
2221
- const nextStatePromise = fn.apply(current.context, [...event.args, { signal: controller.signal }]);
2226
+ const nextStatePromise = fn.apply(current, [...event.args, { signal: controller.signal }]);
2222
2227
  const nextState = await nextStatePromise;
2223
2228
  if (controller.signal.aborted) {
2224
2229
  return current;