@doeixd/machine 0.0.23 → 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.
Files changed (53) hide show
  1. package/README.md +101 -65
  2. package/dist/cjs/development/core.js +56 -57
  3. package/dist/cjs/development/core.js.map +4 -4
  4. package/dist/cjs/development/index.js +99 -58
  5. package/dist/cjs/development/index.js.map +4 -4
  6. package/dist/cjs/development/react.js +56 -58
  7. package/dist/cjs/development/react.js.map +4 -4
  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 +56 -57
  12. package/dist/esm/development/core.js.map +4 -4
  13. package/dist/esm/development/index.js +99 -58
  14. package/dist/esm/development/index.js.map +4 -4
  15. package/dist/esm/development/react.js +56 -58
  16. package/dist/esm/development/react.js.map +4 -4
  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 -34
  29. package/dist/types/index.d.ts.map +1 -1
  30. package/dist/types/internal-transitions.d.ts +5 -0
  31. package/dist/types/internal-transitions.d.ts.map +1 -0
  32. package/dist/types/primitives.d.ts +25 -5
  33. package/dist/types/primitives.d.ts.map +1 -1
  34. package/dist/types/react.d.ts.map +1 -1
  35. package/dist/types/utils.d.ts +22 -22
  36. package/dist/types/utils.d.ts.map +1 -1
  37. package/package.json +1 -1
  38. package/src/actor.ts +1 -1
  39. package/src/context-bound.ts +160 -0
  40. package/src/entry-react.ts +9 -2
  41. package/src/functional-combinators.ts +5 -5
  42. package/src/generators.ts +2 -2
  43. package/src/higher-order.ts +2 -2
  44. package/src/index.ts +47 -80
  45. package/src/internal-transitions.ts +32 -0
  46. package/src/middleware/time-travel.ts +2 -2
  47. package/src/middleware.ts +2 -2
  48. package/src/multi.ts +4 -4
  49. package/src/primitives.ts +34 -14
  50. package/src/prototype_functional.ts +2 -2
  51. package/src/react.ts +1 -2
  52. package/src/test.ts +7 -7
  53. package/src/utils.ts +31 -31
@@ -122,6 +122,32 @@ __export(entry_react_exports, {
122
122
  });
123
123
  module.exports = __toCommonJS(entry_react_exports);
124
124
 
125
+ // src/internal-transitions.ts
126
+ var TRANSITIONS_SYMBOL = Symbol.for("__machine_transitions__");
127
+ function attachTransitions(machine, transitions) {
128
+ Object.defineProperty(machine, TRANSITIONS_SYMBOL, {
129
+ value: transitions,
130
+ enumerable: false,
131
+ configurable: false
132
+ });
133
+ return machine;
134
+ }
135
+ function getStoredTransitions(machine) {
136
+ if (!machine || typeof machine !== "object") {
137
+ return void 0;
138
+ }
139
+ return machine[TRANSITIONS_SYMBOL];
140
+ }
141
+ function snapshotOwnTransitions(source) {
142
+ if (!source || typeof source !== "object") {
143
+ return {};
144
+ }
145
+ const entries = Object.entries(source).filter(
146
+ ([key, value]) => key !== "context" && typeof value === "function"
147
+ );
148
+ return Object.fromEntries(entries);
149
+ }
150
+
125
151
  // src/generators.ts
126
152
  function run(flow, initial) {
127
153
  const generator = flow(initial);
@@ -252,8 +278,8 @@ function guard(condition, transition, options = {}) {
252
278
  const ctx = isMachine ? this.context : this;
253
279
  const conditionResult = condition(ctx, ...args);
254
280
  if (conditionResult) {
255
- const contextForTransition = isMachine ? this.context : this;
256
- return transition.apply(contextForTransition, args);
281
+ const machineForTransition = isMachine ? this : { context: this };
282
+ return transition.apply(machineForTransition, args);
257
283
  } else {
258
284
  if (onFail === "throw") {
259
285
  const message = errorMessage || "Guard condition failed";
@@ -293,8 +319,8 @@ function guardAsync(condition, transition, options = {}) {
293
319
  const ctx = isMachine ? this.context : this;
294
320
  const conditionResult = await Promise.resolve(condition(ctx, ...args));
295
321
  if (conditionResult) {
296
- const contextForTransition = isMachine ? this.context : this;
297
- return transition.apply(contextForTransition, args);
322
+ const machineForTransition = isMachine ? this : { context: this };
323
+ return transition.apply(machineForTransition, args);
298
324
  } else {
299
325
  if (onFail === "throw") {
300
326
  const message = errorMessage || "Guard condition failed";
@@ -375,7 +401,7 @@ function createRunner(initialMachine, onChange) {
375
401
  return void 0;
376
402
  }
377
403
  return (...args) => {
378
- const nextState = transition.apply(currentMachine.context, args);
404
+ const nextState = transition.apply(currentMachine, args);
379
405
  const nextStateWithTransitions = Object.assign(
380
406
  { context: nextState.context },
381
407
  originalTransitions
@@ -418,7 +444,7 @@ function createEnsemble(store, factories, getDiscriminant) {
418
444
  );
419
445
  }
420
446
  return (...args) => {
421
- return action2.apply(currentMachine.context, args);
447
+ return action2.apply(currentMachine, args);
422
448
  };
423
449
  }
424
450
  });
@@ -569,7 +595,7 @@ function createMutableMachine(sharedContext, factories, getDiscriminant) {
569
595
  const transition = currentMachine[prop];
570
596
  if (typeof transition === "function") {
571
597
  return (...args) => {
572
- const nextContext = transition.apply(currentMachine.context, args);
598
+ const nextContext = transition.apply(currentMachine, args);
573
599
  if (typeof nextContext !== "object" || nextContext === null) {
574
600
  console.warn(`[MutableMachine] Transition "${String(prop)}" did not return a valid context object. State may be inconsistent.`);
575
601
  return;
@@ -691,14 +717,14 @@ function createParallelMachine(m1, m2) {
691
717
  for (const key in transitions1) {
692
718
  const transitionFn = transitions1[key];
693
719
  combinedTransitions[key] = (...args) => {
694
- const nextM1 = transitionFn.apply(m1.context, args);
720
+ const nextM1 = transitionFn.apply(m1, args);
695
721
  return createParallelMachine(nextM1, m2);
696
722
  };
697
723
  }
698
724
  for (const key in transitions2) {
699
725
  const transitionFn = transitions2[key];
700
726
  combinedTransitions[key] = (...args) => {
701
- const nextM2 = transitionFn.apply(m2.context, args);
727
+ const nextM2 = transitionFn.apply(m2, args);
702
728
  return createParallelMachine(m1, nextM2);
703
729
  };
704
730
  }
@@ -1169,7 +1195,7 @@ function withTimeTravel(machine, options = {}) {
1169
1195
  for (const entry of transitionsToReplay) {
1170
1196
  const transitionFn = replayedMachine[entry.transitionName];
1171
1197
  if (transitionFn) {
1172
- replayedMachine = transitionFn.apply(replayedMachine.context, entry.args);
1198
+ replayedMachine = transitionFn.apply(replayedMachine, entry.args);
1173
1199
  }
1174
1200
  }
1175
1201
  return replayedMachine;
@@ -1589,8 +1615,8 @@ function createTransition(getTransitions, transformer) {
1589
1615
  return createMachine(nextContext, getTransitions());
1590
1616
  };
1591
1617
  }
1592
- function call(fn, context, ...args) {
1593
- return fn.apply(context, args);
1618
+ function call(fn, machine, ...args) {
1619
+ return fn.apply(machine, args);
1594
1620
  }
1595
1621
  function bindTransitions(machine) {
1596
1622
  return new Proxy(machine, {
@@ -1598,7 +1624,7 @@ function bindTransitions(machine) {
1598
1624
  const value = target[prop];
1599
1625
  if (typeof value === "function") {
1600
1626
  return function(...args) {
1601
- const result = value.apply(target.context, args);
1627
+ const result = value.apply(target, args);
1602
1628
  if (result && typeof result === "object" && "context" in result) {
1603
1629
  return bindTransitions(result);
1604
1630
  }
@@ -1623,7 +1649,7 @@ var BoundMachine = class _BoundMachine {
1623
1649
  const value = this.wrappedMachine[prop];
1624
1650
  if (typeof value === "function") {
1625
1651
  return (...args) => {
1626
- const result = value.apply(this.wrappedMachine.context, args);
1652
+ const result = value.apply(this.wrappedMachine, args);
1627
1653
  if (result && typeof result === "object" && "context" in result) {
1628
1654
  return new _BoundMachine(result);
1629
1655
  }
@@ -1686,57 +1712,29 @@ function createMachine(context, fnsOrFactory) {
1686
1712
  if (typeof fnsOrFactory === "function") {
1687
1713
  let transitions2;
1688
1714
  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);
1715
+ return createMachine(newContext, transitions2);
1697
1716
  };
1698
1717
  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);
1718
+ return attachTransitions(Object.assign({ context }, transitions2), transitions2);
1706
1719
  }
1707
- const transitions = "context" in fnsOrFactory ? Object.fromEntries(
1708
- Object.entries(fnsOrFactory).filter(([key]) => key !== "context")
1709
- ) : fnsOrFactory;
1720
+ const stored = getStoredTransitions(fnsOrFactory);
1721
+ const transitions = stored != null ? stored : "context" in fnsOrFactory ? snapshotOwnTransitions(fnsOrFactory) : fnsOrFactory;
1710
1722
  const machine = Object.assign({ context }, transitions);
1711
- return machine;
1723
+ return attachTransitions(machine, transitions);
1712
1724
  }
1713
1725
  function createAsyncMachine(context, fnsOrFactory) {
1714
1726
  if (typeof fnsOrFactory === "function") {
1715
1727
  let transitions2;
1716
1728
  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);
1729
+ return createAsyncMachine(newContext, transitions2);
1725
1730
  };
1726
1731
  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);
1732
+ return attachTransitions(Object.assign({ context }, transitions2), transitions2);
1734
1733
  }
1735
- const transitions = "context" in fnsOrFactory ? Object.fromEntries(
1736
- Object.entries(fnsOrFactory).filter(([key]) => key !== "context")
1737
- ) : fnsOrFactory;
1734
+ const stored = getStoredTransitions(fnsOrFactory);
1735
+ const transitions = stored != null ? stored : "context" in fnsOrFactory ? snapshotOwnTransitions(fnsOrFactory) : fnsOrFactory;
1738
1736
  const machine = Object.assign({ context }, transitions);
1739
- return machine;
1737
+ return attachTransitions(machine, transitions);
1740
1738
  }
1741
1739
  function createMachineFactory() {
1742
1740
  return (transformers) => {
@@ -1755,8 +1753,10 @@ function createMachineFactory() {
1755
1753
  };
1756
1754
  }
1757
1755
  function setContext(machine, newContextOrFn) {
1758
- const { context, ...transitions } = machine;
1759
- const newContext = typeof newContextOrFn === "function" ? newContextOrFn(context) : newContextOrFn;
1756
+ var _a;
1757
+ const currentContext = machine.context;
1758
+ const transitions = (_a = getStoredTransitions(machine)) != null ? _a : snapshotOwnTransitions(machine);
1759
+ const newContext = typeof newContextOrFn === "function" ? newContextOrFn(currentContext) : newContextOrFn;
1760
1760
  return createMachine(newContext, transitions);
1761
1761
  }
1762
1762
  function overrideTransitions(machine, overrides) {
@@ -1812,7 +1812,7 @@ function runMachine(initial, onChange) {
1812
1812
  const controller = new AbortController();
1813
1813
  activeController = controller;
1814
1814
  try {
1815
- const nextStatePromise = fn.apply(current.context, [...event.args, { signal: controller.signal }]);
1815
+ const nextStatePromise = fn.apply(current, [...event.args, { signal: controller.signal }]);
1816
1816
  const nextState = await nextStatePromise;
1817
1817
  if (controller.signal.aborted) {
1818
1818
  return current;
@@ -1852,8 +1852,7 @@ var MachineBase = class {
1852
1852
  }
1853
1853
  };
1854
1854
  function next(m, update) {
1855
- const { context, ...transitions } = m;
1856
- return createMachine(update(context), transitions);
1855
+ return setContext(m, (ctx) => update(ctx));
1857
1856
  }
1858
1857
 
1859
1858
  // src/react.ts
@@ -1950,7 +1949,6 @@ function useActor(actor) {
1950
1949
  return (0, import_react.useSyncExternalStore)(subscribe, getSnapshot);
1951
1950
  }
1952
1951
  function useActorSelector(actor, selector, isEqual = Object.is) {
1953
- const subscribe = (0, import_react.useMemo)(() => actor.subscribe.bind(actor), [actor]);
1954
1952
  const getSnapshot = (0, import_react.useMemo)(() => actor.getSnapshot.bind(actor), [actor]);
1955
1953
  const getSelection = () => selector(getSnapshot());
1956
1954
  const [selection, setSelection] = (0, import_react.useState)(getSelection);