@doeixd/machine 0.0.22 → 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 (51) hide show
  1. package/README.md +103 -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 +1942 -0
  7. package/dist/cjs/development/react.js.map +7 -0
  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 -0
  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 +1928 -0
  16. package/dist/esm/development/react.js.map +7 -0
  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 -0
  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 +6 -0
  25. package/dist/types/entry-react.d.ts.map +1 -0
  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 +133 -0
  33. package/dist/types/react.d.ts.map +1 -0
  34. package/dist/types/utils.d.ts +22 -22
  35. package/dist/types/utils.d.ts.map +1 -1
  36. package/package.json +13 -1
  37. package/src/actor.ts +1 -1
  38. package/src/context-bound.ts +147 -0
  39. package/src/entry-react.ts +9 -2
  40. package/src/functional-combinators.ts +5 -5
  41. package/src/generators.ts +2 -2
  42. package/src/higher-order.ts +2 -2
  43. package/src/index.ts +31 -68
  44. package/src/middleware/time-travel.ts +2 -2
  45. package/src/middleware.ts +2 -2
  46. package/src/multi.ts +4 -4
  47. package/src/primitives.ts +34 -14
  48. package/src/prototype_functional.ts +2 -2
  49. package/src/react.ts +1 -2
  50. package/src/test.ts +7 -7
  51. 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;
@@ -1563,8 +1563,8 @@ function createTransition(getTransitions, transformer) {
1563
1563
  return createMachine(nextContext, getTransitions());
1564
1564
  };
1565
1565
  }
1566
- function call(fn, context, ...args) {
1567
- return fn.apply(context, args);
1566
+ function call(fn, machine, ...args) {
1567
+ return fn.apply(machine, args);
1568
1568
  }
1569
1569
  function bindTransitions(machine) {
1570
1570
  return new Proxy(machine, {
@@ -1572,7 +1572,7 @@ function bindTransitions(machine) {
1572
1572
  const value = target[prop];
1573
1573
  if (typeof value === "function") {
1574
1574
  return function(...args) {
1575
- const result = value.apply(target.context, args);
1575
+ const result = value.apply(target, args);
1576
1576
  if (result && typeof result === "object" && "context" in result) {
1577
1577
  return bindTransitions(result);
1578
1578
  }
@@ -1597,7 +1597,7 @@ var BoundMachine = class _BoundMachine {
1597
1597
  const value = this.wrappedMachine[prop];
1598
1598
  if (typeof value === "function") {
1599
1599
  return (...args) => {
1600
- const result = value.apply(this.wrappedMachine.context, args);
1600
+ const result = value.apply(this.wrappedMachine, args);
1601
1601
  if (result && typeof result === "object" && "context" in result) {
1602
1602
  return new _BoundMachine(result);
1603
1603
  }
@@ -1873,7 +1873,7 @@ var _Actor = class _Actor {
1873
1873
  }
1874
1874
  let result;
1875
1875
  try {
1876
- result = fn.apply(this._state.context, event.args);
1876
+ result = fn.apply(this._state, event.args);
1877
1877
  } catch (error) {
1878
1878
  console.error(`[Actor] Error in transition '${String(event.type)}':`, error);
1879
1879
  continue;
@@ -1951,28 +1951,43 @@ function fromObservable(observable) {
1951
1951
  return actor;
1952
1952
  }
1953
1953
 
1954
+ // src/context-bound.ts
1955
+ function createContextBoundMachine(initialContext, transformers) {
1956
+ const savedTransformers = transformers;
1957
+ const boundTransitions = Object.fromEntries(
1958
+ Object.entries(transformers).map(([key, transformer]) => [
1959
+ key,
1960
+ function(...args) {
1961
+ const contextOnly = { context: this.context };
1962
+ const newContext = transformer.apply(contextOnly, args);
1963
+ return createContextBoundMachine(newContext, savedTransformers);
1964
+ }
1965
+ ])
1966
+ );
1967
+ return Object.assign({ context: initialContext }, boundTransitions);
1968
+ }
1969
+ function callWithContext(machine, transitionName, ...args) {
1970
+ const fn = machine[transitionName];
1971
+ const contextOnly = { context: machine.context };
1972
+ return fn.apply(contextOnly, args);
1973
+ }
1974
+ function isContextBound(machine) {
1975
+ const firstTransition = Object.values(machine).find(
1976
+ (v) => typeof v === "function"
1977
+ );
1978
+ if (!firstTransition) return false;
1979
+ return firstTransition.__contextBound === true;
1980
+ }
1981
+
1954
1982
  // src/index.ts
1955
1983
  function createMachine(context, fnsOrFactory) {
1956
1984
  if (typeof fnsOrFactory === "function") {
1957
1985
  let transitions2;
1958
1986
  const transition = (newContext) => {
1959
- const machine2 = createMachine(newContext, transitions2);
1960
- const boundTransitions2 = Object.fromEntries(
1961
- Object.entries(transitions2).map(([key, fn]) => [
1962
- key,
1963
- fn.bind(newContext)
1964
- ])
1965
- );
1966
- return Object.assign(machine2, boundTransitions2);
1987
+ return createMachine(newContext, transitions2);
1967
1988
  };
1968
1989
  transitions2 = fnsOrFactory(transition);
1969
- const boundTransitions = Object.fromEntries(
1970
- Object.entries(transitions2).map(([key, fn]) => [
1971
- key,
1972
- fn.bind(context)
1973
- ])
1974
- );
1975
- return Object.assign({ context }, boundTransitions);
1990
+ return Object.assign({ context }, transitions2);
1976
1991
  }
1977
1992
  const transitions = "context" in fnsOrFactory ? Object.fromEntries(
1978
1993
  Object.entries(fnsOrFactory).filter(([key]) => key !== "context")
@@ -1984,23 +1999,10 @@ function createAsyncMachine(context, fnsOrFactory) {
1984
1999
  if (typeof fnsOrFactory === "function") {
1985
2000
  let transitions2;
1986
2001
  const transition = (newContext) => {
1987
- const machine2 = createAsyncMachine(newContext, transitions2);
1988
- const boundTransitions2 = Object.fromEntries(
1989
- Object.entries(transitions2).map(([key, fn]) => [
1990
- key,
1991
- fn.bind(newContext)
1992
- ])
1993
- );
1994
- return Object.assign(machine2, boundTransitions2);
2002
+ return createAsyncMachine(newContext, transitions2);
1995
2003
  };
1996
2004
  transitions2 = fnsOrFactory(transition);
1997
- const boundTransitions = Object.fromEntries(
1998
- Object.entries(transitions2).map(([key, fn]) => [
1999
- key,
2000
- fn.bind(context)
2001
- ])
2002
- );
2003
- return Object.assign({ context }, boundTransitions);
2005
+ return Object.assign({ context }, transitions2);
2004
2006
  }
2005
2007
  const transitions = "context" in fnsOrFactory ? Object.fromEntries(
2006
2008
  Object.entries(fnsOrFactory).filter(([key]) => key !== "context")
@@ -2085,7 +2087,7 @@ function runMachine(initial, onChange) {
2085
2087
  const controller = new AbortController();
2086
2088
  activeController = controller;
2087
2089
  try {
2088
- const nextStatePromise = fn.apply(current.context, [...event.args, { signal: controller.signal }]);
2090
+ const nextStatePromise = fn.apply(current, [...event.args, { signal: controller.signal }]);
2089
2091
  const nextState = await nextStatePromise;
2090
2092
  if (controller.signal.aborted) {
2091
2093
  return current;
@@ -2142,6 +2144,7 @@ export {
2142
2144
  bindTransitions,
2143
2145
  branch,
2144
2146
  call,
2147
+ callWithContext,
2145
2148
  chain,
2146
2149
  classCase,
2147
2150
  combine,
@@ -2151,6 +2154,7 @@ export {
2151
2154
  createActor,
2152
2155
  createAsyncMachine,
2153
2156
  createContext,
2157
+ createContextBoundMachine,
2154
2158
  createCustomMiddleware,
2155
2159
  createEnsemble,
2156
2160
  createEnsembleFactory,
@@ -2188,6 +2192,7 @@ export {
2188
2192
  inDevelopment,
2189
2193
  invoke,
2190
2194
  isConditionalMiddleware,
2195
+ isContextBound,
2191
2196
  isMiddlewareContext,
2192
2197
  isMiddlewareError,
2193
2198
  isMiddlewareFn,