@doeixd/machine 1.0.1 → 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.
@@ -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);
@@ -1964,7 +1990,18 @@ function createContextBoundMachine(initialContext, transformers) {
1964
1990
  }
1965
1991
  ])
1966
1992
  );
1967
- return Object.assign({ context: initialContext }, boundTransitions);
1993
+ Object.values(boundTransitions).forEach((fn) => {
1994
+ if (typeof fn === "function") {
1995
+ Object.defineProperty(fn, "__contextBound", {
1996
+ value: true,
1997
+ enumerable: false
1998
+ });
1999
+ }
2000
+ });
2001
+ return attachTransitions(
2002
+ Object.assign({ context: initialContext }, boundTransitions),
2003
+ boundTransitions
2004
+ );
1968
2005
  }
1969
2006
  function callWithContext(machine, transitionName, ...args) {
1970
2007
  const fn = machine[transitionName];
@@ -1987,13 +2024,12 @@ function createMachine(context, fnsOrFactory) {
1987
2024
  return createMachine(newContext, transitions2);
1988
2025
  };
1989
2026
  transitions2 = fnsOrFactory(transition);
1990
- return Object.assign({ context }, transitions2);
2027
+ return attachTransitions(Object.assign({ context }, transitions2), transitions2);
1991
2028
  }
1992
- const transitions = "context" in fnsOrFactory ? Object.fromEntries(
1993
- Object.entries(fnsOrFactory).filter(([key]) => key !== "context")
1994
- ) : fnsOrFactory;
2029
+ const stored = getStoredTransitions(fnsOrFactory);
2030
+ const transitions = stored != null ? stored : "context" in fnsOrFactory ? snapshotOwnTransitions(fnsOrFactory) : fnsOrFactory;
1995
2031
  const machine = Object.assign({ context }, transitions);
1996
- return machine;
2032
+ return attachTransitions(machine, transitions);
1997
2033
  }
1998
2034
  function createAsyncMachine(context, fnsOrFactory) {
1999
2035
  if (typeof fnsOrFactory === "function") {
@@ -2002,13 +2038,12 @@ function createAsyncMachine(context, fnsOrFactory) {
2002
2038
  return createAsyncMachine(newContext, transitions2);
2003
2039
  };
2004
2040
  transitions2 = fnsOrFactory(transition);
2005
- return Object.assign({ context }, transitions2);
2041
+ return attachTransitions(Object.assign({ context }, transitions2), transitions2);
2006
2042
  }
2007
- const transitions = "context" in fnsOrFactory ? Object.fromEntries(
2008
- Object.entries(fnsOrFactory).filter(([key]) => key !== "context")
2009
- ) : fnsOrFactory;
2043
+ const stored = getStoredTransitions(fnsOrFactory);
2044
+ const transitions = stored != null ? stored : "context" in fnsOrFactory ? snapshotOwnTransitions(fnsOrFactory) : fnsOrFactory;
2010
2045
  const machine = Object.assign({ context }, transitions);
2011
- return machine;
2046
+ return attachTransitions(machine, transitions);
2012
2047
  }
2013
2048
  function createMachineFactory() {
2014
2049
  return (transformers) => {
@@ -2027,8 +2062,10 @@ function createMachineFactory() {
2027
2062
  };
2028
2063
  }
2029
2064
  function setContext(machine, newContextOrFn) {
2030
- const { context, ...transitions } = machine;
2031
- const newContext = typeof newContextOrFn === "function" ? newContextOrFn(context) : newContextOrFn;
2065
+ var _a;
2066
+ const currentContext = machine.context;
2067
+ const transitions = (_a = getStoredTransitions(machine)) != null ? _a : snapshotOwnTransitions(machine);
2068
+ const newContext = typeof newContextOrFn === "function" ? newContextOrFn(currentContext) : newContextOrFn;
2032
2069
  return createMachine(newContext, transitions);
2033
2070
  }
2034
2071
  function createContext(context) {
@@ -2127,8 +2164,7 @@ var MachineBase = class {
2127
2164
  }
2128
2165
  };
2129
2166
  function next(m, update) {
2130
- const { context, ...transitions } = m;
2131
- return createMachine(update(context), transitions);
2167
+ return setContext(m, (ctx) => update(ctx));
2132
2168
  }
2133
2169
  export {
2134
2170
  Actor,