@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
@@ -1,3 +1,29 @@
1
+ // src/internal-transitions.ts
2
+ var TRANSITIONS_SYMBOL = Symbol.for("__machine_transitions__");
3
+ function attachTransitions(machine, transitions) {
4
+ Object.defineProperty(machine, TRANSITIONS_SYMBOL, {
5
+ value: transitions,
6
+ enumerable: false,
7
+ configurable: false
8
+ });
9
+ return machine;
10
+ }
11
+ function getStoredTransitions(machine) {
12
+ if (!machine || typeof machine !== "object") {
13
+ return void 0;
14
+ }
15
+ return machine[TRANSITIONS_SYMBOL];
16
+ }
17
+ function snapshotOwnTransitions(source) {
18
+ if (!source || typeof source !== "object") {
19
+ return {};
20
+ }
21
+ const entries = Object.entries(source).filter(
22
+ ([key, value]) => key !== "context" && typeof value === "function"
23
+ );
24
+ return Object.fromEntries(entries);
25
+ }
26
+
1
27
  // src/generators.ts
2
28
  function run(flow, initial) {
3
29
  const generator = flow(initial);
@@ -128,8 +154,8 @@ function guard(condition, transition, options = {}) {
128
154
  const ctx = isMachine ? this.context : this;
129
155
  const conditionResult = condition(ctx, ...args);
130
156
  if (conditionResult) {
131
- const contextForTransition = isMachine ? this.context : this;
132
- return transition.apply(contextForTransition, args);
157
+ const machineForTransition = isMachine ? this : { context: this };
158
+ return transition.apply(machineForTransition, args);
133
159
  } else {
134
160
  if (onFail === "throw") {
135
161
  const message = errorMessage || "Guard condition failed";
@@ -169,8 +195,8 @@ function guardAsync(condition, transition, options = {}) {
169
195
  const ctx = isMachine ? this.context : this;
170
196
  const conditionResult = await Promise.resolve(condition(ctx, ...args));
171
197
  if (conditionResult) {
172
- const contextForTransition = isMachine ? this.context : this;
173
- return transition.apply(contextForTransition, args);
198
+ const machineForTransition = isMachine ? this : { context: this };
199
+ return transition.apply(machineForTransition, args);
174
200
  } else {
175
201
  if (onFail === "throw") {
176
202
  const message = errorMessage || "Guard condition failed";
@@ -251,7 +277,7 @@ function createRunner(initialMachine, onChange) {
251
277
  return void 0;
252
278
  }
253
279
  return (...args) => {
254
- const nextState = transition.apply(currentMachine.context, args);
280
+ const nextState = transition.apply(currentMachine, args);
255
281
  const nextStateWithTransitions = Object.assign(
256
282
  { context: nextState.context },
257
283
  originalTransitions
@@ -294,7 +320,7 @@ function createEnsemble(store, factories, getDiscriminant) {
294
320
  );
295
321
  }
296
322
  return (...args) => {
297
- return action2.apply(currentMachine.context, args);
323
+ return action2.apply(currentMachine, args);
298
324
  };
299
325
  }
300
326
  });
@@ -445,7 +471,7 @@ function createMutableMachine(sharedContext, factories, getDiscriminant) {
445
471
  const transition = currentMachine[prop];
446
472
  if (typeof transition === "function") {
447
473
  return (...args) => {
448
- const nextContext = transition.apply(currentMachine.context, args);
474
+ const nextContext = transition.apply(currentMachine, args);
449
475
  if (typeof nextContext !== "object" || nextContext === null) {
450
476
  console.warn(`[MutableMachine] Transition "${String(prop)}" did not return a valid context object. State may be inconsistent.`);
451
477
  return;
@@ -567,14 +593,14 @@ function createParallelMachine(m1, m2) {
567
593
  for (const key in transitions1) {
568
594
  const transitionFn = transitions1[key];
569
595
  combinedTransitions[key] = (...args) => {
570
- const nextM1 = transitionFn.apply(m1.context, args);
596
+ const nextM1 = transitionFn.apply(m1, args);
571
597
  return createParallelMachine(nextM1, m2);
572
598
  };
573
599
  }
574
600
  for (const key in transitions2) {
575
601
  const transitionFn = transitions2[key];
576
602
  combinedTransitions[key] = (...args) => {
577
- const nextM2 = transitionFn.apply(m2.context, args);
603
+ const nextM2 = transitionFn.apply(m2, args);
578
604
  return createParallelMachine(m1, nextM2);
579
605
  };
580
606
  }
@@ -1045,7 +1071,7 @@ function withTimeTravel(machine, options = {}) {
1045
1071
  for (const entry of transitionsToReplay) {
1046
1072
  const transitionFn = replayedMachine[entry.transitionName];
1047
1073
  if (transitionFn) {
1048
- replayedMachine = transitionFn.apply(replayedMachine.context, entry.args);
1074
+ replayedMachine = transitionFn.apply(replayedMachine, entry.args);
1049
1075
  }
1050
1076
  }
1051
1077
  return replayedMachine;
@@ -1465,8 +1491,8 @@ function createTransition(getTransitions, transformer) {
1465
1491
  return createMachine(nextContext, getTransitions());
1466
1492
  };
1467
1493
  }
1468
- function call(fn, context, ...args) {
1469
- return fn.apply(context, args);
1494
+ function call(fn, machine, ...args) {
1495
+ return fn.apply(machine, args);
1470
1496
  }
1471
1497
  function bindTransitions(machine) {
1472
1498
  return new Proxy(machine, {
@@ -1474,7 +1500,7 @@ function bindTransitions(machine) {
1474
1500
  const value = target[prop];
1475
1501
  if (typeof value === "function") {
1476
1502
  return function(...args) {
1477
- const result = value.apply(target.context, args);
1503
+ const result = value.apply(target, args);
1478
1504
  if (result && typeof result === "object" && "context" in result) {
1479
1505
  return bindTransitions(result);
1480
1506
  }
@@ -1499,7 +1525,7 @@ var BoundMachine = class _BoundMachine {
1499
1525
  const value = this.wrappedMachine[prop];
1500
1526
  if (typeof value === "function") {
1501
1527
  return (...args) => {
1502
- const result = value.apply(this.wrappedMachine.context, args);
1528
+ const result = value.apply(this.wrappedMachine, args);
1503
1529
  if (result && typeof result === "object" && "context" in result) {
1504
1530
  return new _BoundMachine(result);
1505
1531
  }
@@ -1562,57 +1588,29 @@ function createMachine(context, fnsOrFactory) {
1562
1588
  if (typeof fnsOrFactory === "function") {
1563
1589
  let transitions2;
1564
1590
  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);
1591
+ return createMachine(newContext, transitions2);
1573
1592
  };
1574
1593
  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);
1594
+ return attachTransitions(Object.assign({ context }, transitions2), transitions2);
1582
1595
  }
1583
- const transitions = "context" in fnsOrFactory ? Object.fromEntries(
1584
- Object.entries(fnsOrFactory).filter(([key]) => key !== "context")
1585
- ) : fnsOrFactory;
1596
+ const stored = getStoredTransitions(fnsOrFactory);
1597
+ const transitions = stored != null ? stored : "context" in fnsOrFactory ? snapshotOwnTransitions(fnsOrFactory) : fnsOrFactory;
1586
1598
  const machine = Object.assign({ context }, transitions);
1587
- return machine;
1599
+ return attachTransitions(machine, transitions);
1588
1600
  }
1589
1601
  function createAsyncMachine(context, fnsOrFactory) {
1590
1602
  if (typeof fnsOrFactory === "function") {
1591
1603
  let transitions2;
1592
1604
  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);
1605
+ return createAsyncMachine(newContext, transitions2);
1601
1606
  };
1602
1607
  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);
1608
+ return attachTransitions(Object.assign({ context }, transitions2), transitions2);
1610
1609
  }
1611
- const transitions = "context" in fnsOrFactory ? Object.fromEntries(
1612
- Object.entries(fnsOrFactory).filter(([key]) => key !== "context")
1613
- ) : fnsOrFactory;
1610
+ const stored = getStoredTransitions(fnsOrFactory);
1611
+ const transitions = stored != null ? stored : "context" in fnsOrFactory ? snapshotOwnTransitions(fnsOrFactory) : fnsOrFactory;
1614
1612
  const machine = Object.assign({ context }, transitions);
1615
- return machine;
1613
+ return attachTransitions(machine, transitions);
1616
1614
  }
1617
1615
  function createMachineFactory() {
1618
1616
  return (transformers) => {
@@ -1631,8 +1629,10 @@ function createMachineFactory() {
1631
1629
  };
1632
1630
  }
1633
1631
  function setContext(machine, newContextOrFn) {
1634
- const { context, ...transitions } = machine;
1635
- const newContext = typeof newContextOrFn === "function" ? newContextOrFn(context) : newContextOrFn;
1632
+ var _a;
1633
+ const currentContext = machine.context;
1634
+ const transitions = (_a = getStoredTransitions(machine)) != null ? _a : snapshotOwnTransitions(machine);
1635
+ const newContext = typeof newContextOrFn === "function" ? newContextOrFn(currentContext) : newContextOrFn;
1636
1636
  return createMachine(newContext, transitions);
1637
1637
  }
1638
1638
  function overrideTransitions(machine, overrides) {
@@ -1688,7 +1688,7 @@ function runMachine(initial, onChange) {
1688
1688
  const controller = new AbortController();
1689
1689
  activeController = controller;
1690
1690
  try {
1691
- const nextStatePromise = fn.apply(current.context, [...event.args, { signal: controller.signal }]);
1691
+ const nextStatePromise = fn.apply(current, [...event.args, { signal: controller.signal }]);
1692
1692
  const nextState = await nextStatePromise;
1693
1693
  if (controller.signal.aborted) {
1694
1694
  return current;
@@ -1728,8 +1728,7 @@ var MachineBase = class {
1728
1728
  }
1729
1729
  };
1730
1730
  function next(m, update) {
1731
- const { context, ...transitions } = m;
1732
- return createMachine(update(context), transitions);
1731
+ return setContext(m, (ctx) => update(ctx));
1733
1732
  }
1734
1733
  export {
1735
1734
  BoundMachine,