@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
@@ -252,8 +252,8 @@ function guard(condition, transition, options = {}) {
252
252
  const ctx = isMachine ? this.context : this;
253
253
  const conditionResult = condition(ctx, ...args);
254
254
  if (conditionResult) {
255
- const contextForTransition = isMachine ? this.context : this;
256
- return transition.apply(contextForTransition, args);
255
+ const machineForTransition = isMachine ? this : { context: this };
256
+ return transition.apply(machineForTransition, args);
257
257
  } else {
258
258
  if (onFail === "throw") {
259
259
  const message = errorMessage || "Guard condition failed";
@@ -293,8 +293,8 @@ function guardAsync(condition, transition, options = {}) {
293
293
  const ctx = isMachine ? this.context : this;
294
294
  const conditionResult = await Promise.resolve(condition(ctx, ...args));
295
295
  if (conditionResult) {
296
- const contextForTransition = isMachine ? this.context : this;
297
- return transition.apply(contextForTransition, args);
296
+ const machineForTransition = isMachine ? this : { context: this };
297
+ return transition.apply(machineForTransition, args);
298
298
  } else {
299
299
  if (onFail === "throw") {
300
300
  const message = errorMessage || "Guard condition failed";
@@ -375,7 +375,7 @@ function createRunner(initialMachine, onChange) {
375
375
  return void 0;
376
376
  }
377
377
  return (...args) => {
378
- const nextState = transition.apply(currentMachine.context, args);
378
+ const nextState = transition.apply(currentMachine, args);
379
379
  const nextStateWithTransitions = Object.assign(
380
380
  { context: nextState.context },
381
381
  originalTransitions
@@ -418,7 +418,7 @@ function createEnsemble(store, factories, getDiscriminant) {
418
418
  );
419
419
  }
420
420
  return (...args) => {
421
- return action2.apply(currentMachine.context, args);
421
+ return action2.apply(currentMachine, args);
422
422
  };
423
423
  }
424
424
  });
@@ -569,7 +569,7 @@ function createMutableMachine(sharedContext, factories, getDiscriminant) {
569
569
  const transition = currentMachine[prop];
570
570
  if (typeof transition === "function") {
571
571
  return (...args) => {
572
- const nextContext = transition.apply(currentMachine.context, args);
572
+ const nextContext = transition.apply(currentMachine, args);
573
573
  if (typeof nextContext !== "object" || nextContext === null) {
574
574
  console.warn(`[MutableMachine] Transition "${String(prop)}" did not return a valid context object. State may be inconsistent.`);
575
575
  return;
@@ -691,14 +691,14 @@ function createParallelMachine(m1, m2) {
691
691
  for (const key in transitions1) {
692
692
  const transitionFn = transitions1[key];
693
693
  combinedTransitions[key] = (...args) => {
694
- const nextM1 = transitionFn.apply(m1.context, args);
694
+ const nextM1 = transitionFn.apply(m1, args);
695
695
  return createParallelMachine(nextM1, m2);
696
696
  };
697
697
  }
698
698
  for (const key in transitions2) {
699
699
  const transitionFn = transitions2[key];
700
700
  combinedTransitions[key] = (...args) => {
701
- const nextM2 = transitionFn.apply(m2.context, args);
701
+ const nextM2 = transitionFn.apply(m2, args);
702
702
  return createParallelMachine(m1, nextM2);
703
703
  };
704
704
  }
@@ -1169,7 +1169,7 @@ function withTimeTravel(machine, options = {}) {
1169
1169
  for (const entry of transitionsToReplay) {
1170
1170
  const transitionFn = replayedMachine[entry.transitionName];
1171
1171
  if (transitionFn) {
1172
- replayedMachine = transitionFn.apply(replayedMachine.context, entry.args);
1172
+ replayedMachine = transitionFn.apply(replayedMachine, entry.args);
1173
1173
  }
1174
1174
  }
1175
1175
  return replayedMachine;
@@ -1589,8 +1589,8 @@ function createTransition(getTransitions, transformer) {
1589
1589
  return createMachine(nextContext, getTransitions());
1590
1590
  };
1591
1591
  }
1592
- function call(fn, context, ...args) {
1593
- return fn.apply(context, args);
1592
+ function call(fn, machine, ...args) {
1593
+ return fn.apply(machine, args);
1594
1594
  }
1595
1595
  function bindTransitions(machine) {
1596
1596
  return new Proxy(machine, {
@@ -1598,7 +1598,7 @@ function bindTransitions(machine) {
1598
1598
  const value = target[prop];
1599
1599
  if (typeof value === "function") {
1600
1600
  return function(...args) {
1601
- const result = value.apply(target.context, args);
1601
+ const result = value.apply(target, args);
1602
1602
  if (result && typeof result === "object" && "context" in result) {
1603
1603
  return bindTransitions(result);
1604
1604
  }
@@ -1623,7 +1623,7 @@ var BoundMachine = class _BoundMachine {
1623
1623
  const value = this.wrappedMachine[prop];
1624
1624
  if (typeof value === "function") {
1625
1625
  return (...args) => {
1626
- const result = value.apply(this.wrappedMachine.context, args);
1626
+ const result = value.apply(this.wrappedMachine, args);
1627
1627
  if (result && typeof result === "object" && "context" in result) {
1628
1628
  return new _BoundMachine(result);
1629
1629
  }
@@ -1686,23 +1686,10 @@ function createMachine(context, fnsOrFactory) {
1686
1686
  if (typeof fnsOrFactory === "function") {
1687
1687
  let transitions2;
1688
1688
  const transition = (newContext) => {
1689
- const machine2 = createMachine(newContext, transitions2);
1690
- const boundTransitions2 = Object.fromEntries(
1691
- Object.entries(transitions2).map(([key, fn]) => [
1692
- key,
1693
- fn.bind(newContext)
1694
- ])
1695
- );
1696
- return Object.assign(machine2, boundTransitions2);
1689
+ return createMachine(newContext, transitions2);
1697
1690
  };
1698
1691
  transitions2 = fnsOrFactory(transition);
1699
- const boundTransitions = Object.fromEntries(
1700
- Object.entries(transitions2).map(([key, fn]) => [
1701
- key,
1702
- fn.bind(context)
1703
- ])
1704
- );
1705
- return Object.assign({ context }, boundTransitions);
1692
+ return Object.assign({ context }, transitions2);
1706
1693
  }
1707
1694
  const transitions = "context" in fnsOrFactory ? Object.fromEntries(
1708
1695
  Object.entries(fnsOrFactory).filter(([key]) => key !== "context")
@@ -1714,23 +1701,10 @@ function createAsyncMachine(context, fnsOrFactory) {
1714
1701
  if (typeof fnsOrFactory === "function") {
1715
1702
  let transitions2;
1716
1703
  const transition = (newContext) => {
1717
- const machine2 = createAsyncMachine(newContext, transitions2);
1718
- const boundTransitions2 = Object.fromEntries(
1719
- Object.entries(transitions2).map(([key, fn]) => [
1720
- key,
1721
- fn.bind(newContext)
1722
- ])
1723
- );
1724
- return Object.assign(machine2, boundTransitions2);
1704
+ return createAsyncMachine(newContext, transitions2);
1725
1705
  };
1726
1706
  transitions2 = fnsOrFactory(transition);
1727
- const boundTransitions = Object.fromEntries(
1728
- Object.entries(transitions2).map(([key, fn]) => [
1729
- key,
1730
- fn.bind(context)
1731
- ])
1732
- );
1733
- return Object.assign({ context }, boundTransitions);
1707
+ return Object.assign({ context }, transitions2);
1734
1708
  }
1735
1709
  const transitions = "context" in fnsOrFactory ? Object.fromEntries(
1736
1710
  Object.entries(fnsOrFactory).filter(([key]) => key !== "context")
@@ -1812,7 +1786,7 @@ function runMachine(initial, onChange) {
1812
1786
  const controller = new AbortController();
1813
1787
  activeController = controller;
1814
1788
  try {
1815
- const nextStatePromise = fn.apply(current.context, [...event.args, { signal: controller.signal }]);
1789
+ const nextStatePromise = fn.apply(current, [...event.args, { signal: controller.signal }]);
1816
1790
  const nextState = await nextStatePromise;
1817
1791
  if (controller.signal.aborted) {
1818
1792
  return current;
@@ -1950,7 +1924,6 @@ function useActor(actor) {
1950
1924
  return (0, import_react.useSyncExternalStore)(subscribe, getSnapshot);
1951
1925
  }
1952
1926
  function useActorSelector(actor, selector, isEqual = Object.is) {
1953
- const subscribe = (0, import_react.useMemo)(() => actor.subscribe.bind(actor), [actor]);
1954
1927
  const getSnapshot = (0, import_react.useMemo)(() => actor.getSnapshot.bind(actor), [actor]);
1955
1928
  const getSelection = () => selector(getSnapshot());
1956
1929
  const [selection, setSelection] = (0, import_react.useState)(getSelection);