@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
@@ -128,8 +128,8 @@ function guard(condition, transition, options = {}) {
128
128
  const ctx = isMachine ? this.context : this;
129
129
  const conditionResult = condition(ctx, ...args);
130
130
  if (conditionResult) {
131
- const contextForTransition = isMachine ? this.context : this;
132
- return transition.apply(contextForTransition, args);
131
+ const machineForTransition = isMachine ? this : { context: this };
132
+ return transition.apply(machineForTransition, args);
133
133
  } else {
134
134
  if (onFail === "throw") {
135
135
  const message = errorMessage || "Guard condition failed";
@@ -169,8 +169,8 @@ function guardAsync(condition, transition, options = {}) {
169
169
  const ctx = isMachine ? this.context : this;
170
170
  const conditionResult = await Promise.resolve(condition(ctx, ...args));
171
171
  if (conditionResult) {
172
- const contextForTransition = isMachine ? this.context : this;
173
- return transition.apply(contextForTransition, args);
172
+ const machineForTransition = isMachine ? this : { context: this };
173
+ return transition.apply(machineForTransition, args);
174
174
  } else {
175
175
  if (onFail === "throw") {
176
176
  const message = errorMessage || "Guard condition failed";
@@ -251,7 +251,7 @@ function createRunner(initialMachine, onChange) {
251
251
  return void 0;
252
252
  }
253
253
  return (...args) => {
254
- const nextState = transition.apply(currentMachine.context, args);
254
+ const nextState = transition.apply(currentMachine, args);
255
255
  const nextStateWithTransitions = Object.assign(
256
256
  { context: nextState.context },
257
257
  originalTransitions
@@ -294,7 +294,7 @@ function createEnsemble(store, factories, getDiscriminant) {
294
294
  );
295
295
  }
296
296
  return (...args) => {
297
- return action2.apply(currentMachine.context, args);
297
+ return action2.apply(currentMachine, args);
298
298
  };
299
299
  }
300
300
  });
@@ -445,7 +445,7 @@ function createMutableMachine(sharedContext, factories, getDiscriminant) {
445
445
  const transition = currentMachine[prop];
446
446
  if (typeof transition === "function") {
447
447
  return (...args) => {
448
- const nextContext = transition.apply(currentMachine.context, args);
448
+ const nextContext = transition.apply(currentMachine, args);
449
449
  if (typeof nextContext !== "object" || nextContext === null) {
450
450
  console.warn(`[MutableMachine] Transition "${String(prop)}" did not return a valid context object. State may be inconsistent.`);
451
451
  return;
@@ -567,14 +567,14 @@ function createParallelMachine(m1, m2) {
567
567
  for (const key in transitions1) {
568
568
  const transitionFn = transitions1[key];
569
569
  combinedTransitions[key] = (...args) => {
570
- const nextM1 = transitionFn.apply(m1.context, args);
570
+ const nextM1 = transitionFn.apply(m1, args);
571
571
  return createParallelMachine(nextM1, m2);
572
572
  };
573
573
  }
574
574
  for (const key in transitions2) {
575
575
  const transitionFn = transitions2[key];
576
576
  combinedTransitions[key] = (...args) => {
577
- const nextM2 = transitionFn.apply(m2.context, args);
577
+ const nextM2 = transitionFn.apply(m2, args);
578
578
  return createParallelMachine(m1, nextM2);
579
579
  };
580
580
  }
@@ -1045,7 +1045,7 @@ function withTimeTravel(machine, options = {}) {
1045
1045
  for (const entry of transitionsToReplay) {
1046
1046
  const transitionFn = replayedMachine[entry.transitionName];
1047
1047
  if (transitionFn) {
1048
- replayedMachine = transitionFn.apply(replayedMachine.context, entry.args);
1048
+ replayedMachine = transitionFn.apply(replayedMachine, entry.args);
1049
1049
  }
1050
1050
  }
1051
1051
  return replayedMachine;
@@ -1465,8 +1465,8 @@ function createTransition(getTransitions, transformer) {
1465
1465
  return createMachine(nextContext, getTransitions());
1466
1466
  };
1467
1467
  }
1468
- function call(fn, context, ...args) {
1469
- return fn.apply(context, args);
1468
+ function call(fn, machine, ...args) {
1469
+ return fn.apply(machine, args);
1470
1470
  }
1471
1471
  function bindTransitions(machine) {
1472
1472
  return new Proxy(machine, {
@@ -1474,7 +1474,7 @@ function bindTransitions(machine) {
1474
1474
  const value = target[prop];
1475
1475
  if (typeof value === "function") {
1476
1476
  return function(...args) {
1477
- const result = value.apply(target.context, args);
1477
+ const result = value.apply(target, args);
1478
1478
  if (result && typeof result === "object" && "context" in result) {
1479
1479
  return bindTransitions(result);
1480
1480
  }
@@ -1499,7 +1499,7 @@ var BoundMachine = class _BoundMachine {
1499
1499
  const value = this.wrappedMachine[prop];
1500
1500
  if (typeof value === "function") {
1501
1501
  return (...args) => {
1502
- const result = value.apply(this.wrappedMachine.context, args);
1502
+ const result = value.apply(this.wrappedMachine, args);
1503
1503
  if (result && typeof result === "object" && "context" in result) {
1504
1504
  return new _BoundMachine(result);
1505
1505
  }
@@ -1562,23 +1562,10 @@ function createMachine(context, fnsOrFactory) {
1562
1562
  if (typeof fnsOrFactory === "function") {
1563
1563
  let transitions2;
1564
1564
  const transition = (newContext) => {
1565
- const machine2 = createMachine(newContext, transitions2);
1566
- const boundTransitions2 = Object.fromEntries(
1567
- Object.entries(transitions2).map(([key, fn]) => [
1568
- key,
1569
- fn.bind(newContext)
1570
- ])
1571
- );
1572
- return Object.assign(machine2, boundTransitions2);
1565
+ return createMachine(newContext, transitions2);
1573
1566
  };
1574
1567
  transitions2 = fnsOrFactory(transition);
1575
- const boundTransitions = Object.fromEntries(
1576
- Object.entries(transitions2).map(([key, fn]) => [
1577
- key,
1578
- fn.bind(context)
1579
- ])
1580
- );
1581
- return Object.assign({ context }, boundTransitions);
1568
+ return Object.assign({ context }, transitions2);
1582
1569
  }
1583
1570
  const transitions = "context" in fnsOrFactory ? Object.fromEntries(
1584
1571
  Object.entries(fnsOrFactory).filter(([key]) => key !== "context")
@@ -1590,23 +1577,10 @@ function createAsyncMachine(context, fnsOrFactory) {
1590
1577
  if (typeof fnsOrFactory === "function") {
1591
1578
  let transitions2;
1592
1579
  const transition = (newContext) => {
1593
- const machine2 = createAsyncMachine(newContext, transitions2);
1594
- const boundTransitions2 = Object.fromEntries(
1595
- Object.entries(transitions2).map(([key, fn]) => [
1596
- key,
1597
- fn.bind(newContext)
1598
- ])
1599
- );
1600
- return Object.assign(machine2, boundTransitions2);
1580
+ return createAsyncMachine(newContext, transitions2);
1601
1581
  };
1602
1582
  transitions2 = fnsOrFactory(transition);
1603
- const boundTransitions = Object.fromEntries(
1604
- Object.entries(transitions2).map(([key, fn]) => [
1605
- key,
1606
- fn.bind(context)
1607
- ])
1608
- );
1609
- return Object.assign({ context }, boundTransitions);
1583
+ return Object.assign({ context }, transitions2);
1610
1584
  }
1611
1585
  const transitions = "context" in fnsOrFactory ? Object.fromEntries(
1612
1586
  Object.entries(fnsOrFactory).filter(([key]) => key !== "context")
@@ -1688,7 +1662,7 @@ function runMachine(initial, onChange) {
1688
1662
  const controller = new AbortController();
1689
1663
  activeController = controller;
1690
1664
  try {
1691
- const nextStatePromise = fn.apply(current.context, [...event.args, { signal: controller.signal }]);
1665
+ const nextStatePromise = fn.apply(current, [...event.args, { signal: controller.signal }]);
1692
1666
  const nextState = await nextStatePromise;
1693
1667
  if (controller.signal.aborted) {
1694
1668
  return current;
@@ -1835,7 +1809,6 @@ function useActor(actor) {
1835
1809
  return useSyncExternalStore(subscribe, getSnapshot);
1836
1810
  }
1837
1811
  function useActorSelector(actor, selector, isEqual = Object.is) {
1838
- const subscribe = useMemo(() => actor.subscribe.bind(actor), [actor]);
1839
1812
  const getSnapshot = useMemo(() => actor.getSnapshot.bind(actor), [actor]);
1840
1813
  const getSelection = () => selector(getSnapshot());
1841
1814
  const [selection, setSelection] = useState(getSelection);