@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.
- package/README.md +101 -65
- package/dist/cjs/development/core.js +56 -57
- package/dist/cjs/development/core.js.map +4 -4
- package/dist/cjs/development/index.js +99 -58
- package/dist/cjs/development/index.js.map +4 -4
- package/dist/cjs/development/react.js +56 -58
- package/dist/cjs/development/react.js.map +4 -4
- package/dist/cjs/production/core.js +1 -1
- package/dist/cjs/production/index.js +3 -3
- package/dist/cjs/production/react.js +1 -1
- package/dist/esm/development/core.js +56 -57
- package/dist/esm/development/core.js.map +4 -4
- package/dist/esm/development/index.js +99 -58
- package/dist/esm/development/index.js.map +4 -4
- package/dist/esm/development/react.js +56 -58
- package/dist/esm/development/react.js.map +4 -4
- package/dist/esm/production/core.js +1 -1
- package/dist/esm/production/index.js +3 -3
- package/dist/esm/production/react.js +1 -1
- package/dist/types/actor.d.ts +4 -4
- package/dist/types/actor.d.ts.map +1 -1
- package/dist/types/context-bound.d.ts +94 -0
- package/dist/types/context-bound.d.ts.map +1 -0
- package/dist/types/entry-react.d.ts +1 -1
- package/dist/types/entry-react.d.ts.map +1 -1
- package/dist/types/functional-combinators.d.ts +5 -5
- package/dist/types/generators.d.ts +2 -2
- package/dist/types/index.d.ts +14 -34
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/internal-transitions.d.ts +5 -0
- package/dist/types/internal-transitions.d.ts.map +1 -0
- package/dist/types/primitives.d.ts +25 -5
- package/dist/types/primitives.d.ts.map +1 -1
- package/dist/types/react.d.ts.map +1 -1
- package/dist/types/utils.d.ts +22 -22
- package/dist/types/utils.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/actor.ts +1 -1
- package/src/context-bound.ts +160 -0
- package/src/entry-react.ts +9 -2
- package/src/functional-combinators.ts +5 -5
- package/src/generators.ts +2 -2
- package/src/higher-order.ts +2 -2
- package/src/index.ts +47 -80
- package/src/internal-transitions.ts +32 -0
- package/src/middleware/time-travel.ts +2 -2
- package/src/middleware.ts +2 -2
- package/src/multi.ts +4 -4
- package/src/primitives.ts +34 -14
- package/src/prototype_functional.ts +2 -2
- package/src/react.ts +1 -2
- package/src/test.ts +7 -7
- 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
|
|
132
|
-
return transition.apply(
|
|
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
|
|
173
|
-
return transition.apply(
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
1074
|
+
replayedMachine = transitionFn.apply(replayedMachine, entry.args);
|
|
1049
1075
|
}
|
|
1050
1076
|
}
|
|
1051
1077
|
return replayedMachine;
|
|
@@ -1563,8 +1589,8 @@ function createTransition(getTransitions, transformer) {
|
|
|
1563
1589
|
return createMachine(nextContext, getTransitions());
|
|
1564
1590
|
};
|
|
1565
1591
|
}
|
|
1566
|
-
function call(fn,
|
|
1567
|
-
return fn.apply(
|
|
1592
|
+
function call(fn, machine, ...args) {
|
|
1593
|
+
return fn.apply(machine, args);
|
|
1568
1594
|
}
|
|
1569
1595
|
function bindTransitions(machine) {
|
|
1570
1596
|
return new Proxy(machine, {
|
|
@@ -1572,7 +1598,7 @@ function bindTransitions(machine) {
|
|
|
1572
1598
|
const value = target[prop];
|
|
1573
1599
|
if (typeof value === "function") {
|
|
1574
1600
|
return function(...args) {
|
|
1575
|
-
const result = value.apply(target
|
|
1601
|
+
const result = value.apply(target, args);
|
|
1576
1602
|
if (result && typeof result === "object" && "context" in result) {
|
|
1577
1603
|
return bindTransitions(result);
|
|
1578
1604
|
}
|
|
@@ -1597,7 +1623,7 @@ var BoundMachine = class _BoundMachine {
|
|
|
1597
1623
|
const value = this.wrappedMachine[prop];
|
|
1598
1624
|
if (typeof value === "function") {
|
|
1599
1625
|
return (...args) => {
|
|
1600
|
-
const result = value.apply(this.wrappedMachine
|
|
1626
|
+
const result = value.apply(this.wrappedMachine, args);
|
|
1601
1627
|
if (result && typeof result === "object" && "context" in result) {
|
|
1602
1628
|
return new _BoundMachine(result);
|
|
1603
1629
|
}
|
|
@@ -1873,7 +1899,7 @@ var _Actor = class _Actor {
|
|
|
1873
1899
|
}
|
|
1874
1900
|
let result;
|
|
1875
1901
|
try {
|
|
1876
|
-
result = fn.apply(this._state
|
|
1902
|
+
result = fn.apply(this._state, event.args);
|
|
1877
1903
|
} catch (error) {
|
|
1878
1904
|
console.error(`[Actor] Error in transition '${String(event.type)}':`, error);
|
|
1879
1905
|
continue;
|
|
@@ -1951,62 +1977,73 @@ function fromObservable(observable) {
|
|
|
1951
1977
|
return actor;
|
|
1952
1978
|
}
|
|
1953
1979
|
|
|
1980
|
+
// src/context-bound.ts
|
|
1981
|
+
function createContextBoundMachine(initialContext, transformers) {
|
|
1982
|
+
const savedTransformers = transformers;
|
|
1983
|
+
const boundTransitions = Object.fromEntries(
|
|
1984
|
+
Object.entries(transformers).map(([key, transformer]) => [
|
|
1985
|
+
key,
|
|
1986
|
+
function(...args) {
|
|
1987
|
+
const contextOnly = { context: this.context };
|
|
1988
|
+
const newContext = transformer.apply(contextOnly, args);
|
|
1989
|
+
return createContextBoundMachine(newContext, savedTransformers);
|
|
1990
|
+
}
|
|
1991
|
+
])
|
|
1992
|
+
);
|
|
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
|
+
);
|
|
2005
|
+
}
|
|
2006
|
+
function callWithContext(machine, transitionName, ...args) {
|
|
2007
|
+
const fn = machine[transitionName];
|
|
2008
|
+
const contextOnly = { context: machine.context };
|
|
2009
|
+
return fn.apply(contextOnly, args);
|
|
2010
|
+
}
|
|
2011
|
+
function isContextBound(machine) {
|
|
2012
|
+
const firstTransition = Object.values(machine).find(
|
|
2013
|
+
(v) => typeof v === "function"
|
|
2014
|
+
);
|
|
2015
|
+
if (!firstTransition) return false;
|
|
2016
|
+
return firstTransition.__contextBound === true;
|
|
2017
|
+
}
|
|
2018
|
+
|
|
1954
2019
|
// src/index.ts
|
|
1955
2020
|
function createMachine(context, fnsOrFactory) {
|
|
1956
2021
|
if (typeof fnsOrFactory === "function") {
|
|
1957
2022
|
let transitions2;
|
|
1958
2023
|
const transition = (newContext) => {
|
|
1959
|
-
|
|
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);
|
|
2024
|
+
return createMachine(newContext, transitions2);
|
|
1967
2025
|
};
|
|
1968
2026
|
transitions2 = fnsOrFactory(transition);
|
|
1969
|
-
|
|
1970
|
-
Object.entries(transitions2).map(([key, fn]) => [
|
|
1971
|
-
key,
|
|
1972
|
-
fn.bind(context)
|
|
1973
|
-
])
|
|
1974
|
-
);
|
|
1975
|
-
return Object.assign({ context }, boundTransitions);
|
|
2027
|
+
return attachTransitions(Object.assign({ context }, transitions2), transitions2);
|
|
1976
2028
|
}
|
|
1977
|
-
const
|
|
1978
|
-
|
|
1979
|
-
) : fnsOrFactory;
|
|
2029
|
+
const stored = getStoredTransitions(fnsOrFactory);
|
|
2030
|
+
const transitions = stored != null ? stored : "context" in fnsOrFactory ? snapshotOwnTransitions(fnsOrFactory) : fnsOrFactory;
|
|
1980
2031
|
const machine = Object.assign({ context }, transitions);
|
|
1981
|
-
return machine;
|
|
2032
|
+
return attachTransitions(machine, transitions);
|
|
1982
2033
|
}
|
|
1983
2034
|
function createAsyncMachine(context, fnsOrFactory) {
|
|
1984
2035
|
if (typeof fnsOrFactory === "function") {
|
|
1985
2036
|
let transitions2;
|
|
1986
2037
|
const transition = (newContext) => {
|
|
1987
|
-
|
|
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);
|
|
2038
|
+
return createAsyncMachine(newContext, transitions2);
|
|
1995
2039
|
};
|
|
1996
2040
|
transitions2 = fnsOrFactory(transition);
|
|
1997
|
-
|
|
1998
|
-
Object.entries(transitions2).map(([key, fn]) => [
|
|
1999
|
-
key,
|
|
2000
|
-
fn.bind(context)
|
|
2001
|
-
])
|
|
2002
|
-
);
|
|
2003
|
-
return Object.assign({ context }, boundTransitions);
|
|
2041
|
+
return attachTransitions(Object.assign({ context }, transitions2), transitions2);
|
|
2004
2042
|
}
|
|
2005
|
-
const
|
|
2006
|
-
|
|
2007
|
-
) : fnsOrFactory;
|
|
2043
|
+
const stored = getStoredTransitions(fnsOrFactory);
|
|
2044
|
+
const transitions = stored != null ? stored : "context" in fnsOrFactory ? snapshotOwnTransitions(fnsOrFactory) : fnsOrFactory;
|
|
2008
2045
|
const machine = Object.assign({ context }, transitions);
|
|
2009
|
-
return machine;
|
|
2046
|
+
return attachTransitions(machine, transitions);
|
|
2010
2047
|
}
|
|
2011
2048
|
function createMachineFactory() {
|
|
2012
2049
|
return (transformers) => {
|
|
@@ -2025,8 +2062,10 @@ function createMachineFactory() {
|
|
|
2025
2062
|
};
|
|
2026
2063
|
}
|
|
2027
2064
|
function setContext(machine, newContextOrFn) {
|
|
2028
|
-
|
|
2029
|
-
const
|
|
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;
|
|
2030
2069
|
return createMachine(newContext, transitions);
|
|
2031
2070
|
}
|
|
2032
2071
|
function createContext(context) {
|
|
@@ -2085,7 +2124,7 @@ function runMachine(initial, onChange) {
|
|
|
2085
2124
|
const controller = new AbortController();
|
|
2086
2125
|
activeController = controller;
|
|
2087
2126
|
try {
|
|
2088
|
-
const nextStatePromise = fn.apply(current
|
|
2127
|
+
const nextStatePromise = fn.apply(current, [...event.args, { signal: controller.signal }]);
|
|
2089
2128
|
const nextState = await nextStatePromise;
|
|
2090
2129
|
if (controller.signal.aborted) {
|
|
2091
2130
|
return current;
|
|
@@ -2125,8 +2164,7 @@ var MachineBase = class {
|
|
|
2125
2164
|
}
|
|
2126
2165
|
};
|
|
2127
2166
|
function next(m, update) {
|
|
2128
|
-
|
|
2129
|
-
return createMachine(update(context), transitions);
|
|
2167
|
+
return setContext(m, (ctx) => update(ctx));
|
|
2130
2168
|
}
|
|
2131
2169
|
export {
|
|
2132
2170
|
Actor,
|
|
@@ -2142,6 +2180,7 @@ export {
|
|
|
2142
2180
|
bindTransitions,
|
|
2143
2181
|
branch,
|
|
2144
2182
|
call,
|
|
2183
|
+
callWithContext,
|
|
2145
2184
|
chain,
|
|
2146
2185
|
classCase,
|
|
2147
2186
|
combine,
|
|
@@ -2151,6 +2190,7 @@ export {
|
|
|
2151
2190
|
createActor,
|
|
2152
2191
|
createAsyncMachine,
|
|
2153
2192
|
createContext,
|
|
2193
|
+
createContextBoundMachine,
|
|
2154
2194
|
createCustomMiddleware,
|
|
2155
2195
|
createEnsemble,
|
|
2156
2196
|
createEnsembleFactory,
|
|
@@ -2188,6 +2228,7 @@ export {
|
|
|
2188
2228
|
inDevelopment,
|
|
2189
2229
|
invoke,
|
|
2190
2230
|
isConditionalMiddleware,
|
|
2231
|
+
isContextBound,
|
|
2191
2232
|
isMiddlewareContext,
|
|
2192
2233
|
isMiddlewareError,
|
|
2193
2234
|
isMiddlewareFn,
|