@doeixd/machine 1.0.1 → 1.0.3

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.
@@ -1,3 +1,40 @@
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
+
27
+ // src/base.ts
28
+ var MachineBase = class {
29
+ /**
30
+ * Initializes a new machine instance with its starting context.
31
+ * @param context - The initial state of the machine.
32
+ */
33
+ constructor(context) {
34
+ this.context = context;
35
+ }
36
+ };
37
+
1
38
  // src/generators.ts
2
39
  function run(flow, initial) {
3
40
  const generator = flow(initial);
@@ -1964,7 +2001,18 @@ function createContextBoundMachine(initialContext, transformers) {
1964
2001
  }
1965
2002
  ])
1966
2003
  );
1967
- return Object.assign({ context: initialContext }, boundTransitions);
2004
+ Object.values(boundTransitions).forEach((fn) => {
2005
+ if (typeof fn === "function") {
2006
+ Object.defineProperty(fn, "__contextBound", {
2007
+ value: true,
2008
+ enumerable: false
2009
+ });
2010
+ }
2011
+ });
2012
+ return attachTransitions(
2013
+ Object.assign({ context: initialContext }, boundTransitions),
2014
+ boundTransitions
2015
+ );
1968
2016
  }
1969
2017
  function callWithContext(machine, transitionName, ...args) {
1970
2018
  const fn = machine[transitionName];
@@ -1987,13 +2035,12 @@ function createMachine(context, fnsOrFactory) {
1987
2035
  return createMachine(newContext, transitions2);
1988
2036
  };
1989
2037
  transitions2 = fnsOrFactory(transition);
1990
- return Object.assign({ context }, transitions2);
2038
+ return attachTransitions(Object.assign({ context }, transitions2), transitions2);
1991
2039
  }
1992
- const transitions = "context" in fnsOrFactory ? Object.fromEntries(
1993
- Object.entries(fnsOrFactory).filter(([key]) => key !== "context")
1994
- ) : fnsOrFactory;
2040
+ const stored = getStoredTransitions(fnsOrFactory);
2041
+ const transitions = stored != null ? stored : "context" in fnsOrFactory ? snapshotOwnTransitions(fnsOrFactory) : fnsOrFactory;
1995
2042
  const machine = Object.assign({ context }, transitions);
1996
- return machine;
2043
+ return attachTransitions(machine, transitions);
1997
2044
  }
1998
2045
  function createAsyncMachine(context, fnsOrFactory) {
1999
2046
  if (typeof fnsOrFactory === "function") {
@@ -2002,13 +2049,12 @@ function createAsyncMachine(context, fnsOrFactory) {
2002
2049
  return createAsyncMachine(newContext, transitions2);
2003
2050
  };
2004
2051
  transitions2 = fnsOrFactory(transition);
2005
- return Object.assign({ context }, transitions2);
2052
+ return attachTransitions(Object.assign({ context }, transitions2), transitions2);
2006
2053
  }
2007
- const transitions = "context" in fnsOrFactory ? Object.fromEntries(
2008
- Object.entries(fnsOrFactory).filter(([key]) => key !== "context")
2009
- ) : fnsOrFactory;
2054
+ const stored = getStoredTransitions(fnsOrFactory);
2055
+ const transitions = stored != null ? stored : "context" in fnsOrFactory ? snapshotOwnTransitions(fnsOrFactory) : fnsOrFactory;
2010
2056
  const machine = Object.assign({ context }, transitions);
2011
- return machine;
2057
+ return attachTransitions(machine, transitions);
2012
2058
  }
2013
2059
  function createMachineFactory() {
2014
2060
  return (transformers) => {
@@ -2027,8 +2073,10 @@ function createMachineFactory() {
2027
2073
  };
2028
2074
  }
2029
2075
  function setContext(machine, newContextOrFn) {
2030
- const { context, ...transitions } = machine;
2031
- const newContext = typeof newContextOrFn === "function" ? newContextOrFn(context) : newContextOrFn;
2076
+ var _a;
2077
+ const currentContext = machine.context;
2078
+ const transitions = (_a = getStoredTransitions(machine)) != null ? _a : snapshotOwnTransitions(machine);
2079
+ const newContext = typeof newContextOrFn === "function" ? newContextOrFn(currentContext) : newContextOrFn;
2032
2080
  return createMachine(newContext, transitions);
2033
2081
  }
2034
2082
  function createContext(context) {
@@ -2117,18 +2165,8 @@ function runMachine(initial, onChange) {
2117
2165
  }
2118
2166
  };
2119
2167
  }
2120
- var MachineBase = class {
2121
- /**
2122
- * Initializes a new machine instance with its starting context.
2123
- * @param context - The initial state of the machine.
2124
- */
2125
- constructor(context) {
2126
- this.context = context;
2127
- }
2128
- };
2129
2168
  function next(m, update) {
2130
- const { context, ...transitions } = m;
2131
- return createMachine(update(context), transitions);
2169
+ return setContext(m, (ctx) => update(ctx));
2132
2170
  }
2133
2171
  export {
2134
2172
  Actor,