@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
|
@@ -33,6 +33,7 @@ __export(src_exports, {
|
|
|
33
33
|
bindTransitions: () => bindTransitions,
|
|
34
34
|
branch: () => branch,
|
|
35
35
|
call: () => call,
|
|
36
|
+
callWithContext: () => callWithContext,
|
|
36
37
|
chain: () => chain,
|
|
37
38
|
classCase: () => classCase,
|
|
38
39
|
combine: () => combine,
|
|
@@ -42,6 +43,7 @@ __export(src_exports, {
|
|
|
42
43
|
createActor: () => createActor,
|
|
43
44
|
createAsyncMachine: () => createAsyncMachine,
|
|
44
45
|
createContext: () => createContext,
|
|
46
|
+
createContextBoundMachine: () => createContextBoundMachine,
|
|
45
47
|
createCustomMiddleware: () => createCustomMiddleware,
|
|
46
48
|
createEnsemble: () => createEnsemble,
|
|
47
49
|
createEnsembleFactory: () => createEnsembleFactory,
|
|
@@ -79,6 +81,7 @@ __export(src_exports, {
|
|
|
79
81
|
inDevelopment: () => inDevelopment,
|
|
80
82
|
invoke: () => invoke,
|
|
81
83
|
isConditionalMiddleware: () => isConditionalMiddleware,
|
|
84
|
+
isContextBound: () => isContextBound,
|
|
82
85
|
isMiddlewareContext: () => isMiddlewareContext,
|
|
83
86
|
isMiddlewareError: () => isMiddlewareError,
|
|
84
87
|
isMiddlewareFn: () => isMiddlewareFn,
|
|
@@ -131,6 +134,32 @@ __export(src_exports, {
|
|
|
131
134
|
});
|
|
132
135
|
module.exports = __toCommonJS(src_exports);
|
|
133
136
|
|
|
137
|
+
// src/internal-transitions.ts
|
|
138
|
+
var TRANSITIONS_SYMBOL = Symbol.for("__machine_transitions__");
|
|
139
|
+
function attachTransitions(machine, transitions) {
|
|
140
|
+
Object.defineProperty(machine, TRANSITIONS_SYMBOL, {
|
|
141
|
+
value: transitions,
|
|
142
|
+
enumerable: false,
|
|
143
|
+
configurable: false
|
|
144
|
+
});
|
|
145
|
+
return machine;
|
|
146
|
+
}
|
|
147
|
+
function getStoredTransitions(machine) {
|
|
148
|
+
if (!machine || typeof machine !== "object") {
|
|
149
|
+
return void 0;
|
|
150
|
+
}
|
|
151
|
+
return machine[TRANSITIONS_SYMBOL];
|
|
152
|
+
}
|
|
153
|
+
function snapshotOwnTransitions(source) {
|
|
154
|
+
if (!source || typeof source !== "object") {
|
|
155
|
+
return {};
|
|
156
|
+
}
|
|
157
|
+
const entries = Object.entries(source).filter(
|
|
158
|
+
([key, value]) => key !== "context" && typeof value === "function"
|
|
159
|
+
);
|
|
160
|
+
return Object.fromEntries(entries);
|
|
161
|
+
}
|
|
162
|
+
|
|
134
163
|
// src/generators.ts
|
|
135
164
|
function run(flow, initial) {
|
|
136
165
|
const generator = flow(initial);
|
|
@@ -261,8 +290,8 @@ function guard(condition, transition, options = {}) {
|
|
|
261
290
|
const ctx = isMachine ? this.context : this;
|
|
262
291
|
const conditionResult = condition(ctx, ...args);
|
|
263
292
|
if (conditionResult) {
|
|
264
|
-
const
|
|
265
|
-
return transition.apply(
|
|
293
|
+
const machineForTransition = isMachine ? this : { context: this };
|
|
294
|
+
return transition.apply(machineForTransition, args);
|
|
266
295
|
} else {
|
|
267
296
|
if (onFail === "throw") {
|
|
268
297
|
const message = errorMessage || "Guard condition failed";
|
|
@@ -302,8 +331,8 @@ function guardAsync(condition, transition, options = {}) {
|
|
|
302
331
|
const ctx = isMachine ? this.context : this;
|
|
303
332
|
const conditionResult = await Promise.resolve(condition(ctx, ...args));
|
|
304
333
|
if (conditionResult) {
|
|
305
|
-
const
|
|
306
|
-
return transition.apply(
|
|
334
|
+
const machineForTransition = isMachine ? this : { context: this };
|
|
335
|
+
return transition.apply(machineForTransition, args);
|
|
307
336
|
} else {
|
|
308
337
|
if (onFail === "throw") {
|
|
309
338
|
const message = errorMessage || "Guard condition failed";
|
|
@@ -384,7 +413,7 @@ function createRunner(initialMachine, onChange) {
|
|
|
384
413
|
return void 0;
|
|
385
414
|
}
|
|
386
415
|
return (...args) => {
|
|
387
|
-
const nextState = transition.apply(currentMachine
|
|
416
|
+
const nextState = transition.apply(currentMachine, args);
|
|
388
417
|
const nextStateWithTransitions = Object.assign(
|
|
389
418
|
{ context: nextState.context },
|
|
390
419
|
originalTransitions
|
|
@@ -427,7 +456,7 @@ function createEnsemble(store, factories, getDiscriminant) {
|
|
|
427
456
|
);
|
|
428
457
|
}
|
|
429
458
|
return (...args) => {
|
|
430
|
-
return action2.apply(currentMachine
|
|
459
|
+
return action2.apply(currentMachine, args);
|
|
431
460
|
};
|
|
432
461
|
}
|
|
433
462
|
});
|
|
@@ -578,7 +607,7 @@ function createMutableMachine(sharedContext, factories, getDiscriminant) {
|
|
|
578
607
|
const transition = currentMachine[prop];
|
|
579
608
|
if (typeof transition === "function") {
|
|
580
609
|
return (...args) => {
|
|
581
|
-
const nextContext = transition.apply(currentMachine
|
|
610
|
+
const nextContext = transition.apply(currentMachine, args);
|
|
582
611
|
if (typeof nextContext !== "object" || nextContext === null) {
|
|
583
612
|
console.warn(`[MutableMachine] Transition "${String(prop)}" did not return a valid context object. State may be inconsistent.`);
|
|
584
613
|
return;
|
|
@@ -700,14 +729,14 @@ function createParallelMachine(m1, m2) {
|
|
|
700
729
|
for (const key in transitions1) {
|
|
701
730
|
const transitionFn = transitions1[key];
|
|
702
731
|
combinedTransitions[key] = (...args) => {
|
|
703
|
-
const nextM1 = transitionFn.apply(m1
|
|
732
|
+
const nextM1 = transitionFn.apply(m1, args);
|
|
704
733
|
return createParallelMachine(nextM1, m2);
|
|
705
734
|
};
|
|
706
735
|
}
|
|
707
736
|
for (const key in transitions2) {
|
|
708
737
|
const transitionFn = transitions2[key];
|
|
709
738
|
combinedTransitions[key] = (...args) => {
|
|
710
|
-
const nextM2 = transitionFn.apply(m2
|
|
739
|
+
const nextM2 = transitionFn.apply(m2, args);
|
|
711
740
|
return createParallelMachine(m1, nextM2);
|
|
712
741
|
};
|
|
713
742
|
}
|
|
@@ -1178,7 +1207,7 @@ function withTimeTravel(machine, options = {}) {
|
|
|
1178
1207
|
for (const entry of transitionsToReplay) {
|
|
1179
1208
|
const transitionFn = replayedMachine[entry.transitionName];
|
|
1180
1209
|
if (transitionFn) {
|
|
1181
|
-
replayedMachine = transitionFn.apply(replayedMachine
|
|
1210
|
+
replayedMachine = transitionFn.apply(replayedMachine, entry.args);
|
|
1182
1211
|
}
|
|
1183
1212
|
}
|
|
1184
1213
|
return replayedMachine;
|
|
@@ -1696,8 +1725,8 @@ function createTransition(getTransitions, transformer) {
|
|
|
1696
1725
|
return createMachine(nextContext, getTransitions());
|
|
1697
1726
|
};
|
|
1698
1727
|
}
|
|
1699
|
-
function call(fn,
|
|
1700
|
-
return fn.apply(
|
|
1728
|
+
function call(fn, machine, ...args) {
|
|
1729
|
+
return fn.apply(machine, args);
|
|
1701
1730
|
}
|
|
1702
1731
|
function bindTransitions(machine) {
|
|
1703
1732
|
return new Proxy(machine, {
|
|
@@ -1705,7 +1734,7 @@ function bindTransitions(machine) {
|
|
|
1705
1734
|
const value = target[prop];
|
|
1706
1735
|
if (typeof value === "function") {
|
|
1707
1736
|
return function(...args) {
|
|
1708
|
-
const result = value.apply(target
|
|
1737
|
+
const result = value.apply(target, args);
|
|
1709
1738
|
if (result && typeof result === "object" && "context" in result) {
|
|
1710
1739
|
return bindTransitions(result);
|
|
1711
1740
|
}
|
|
@@ -1730,7 +1759,7 @@ var BoundMachine = class _BoundMachine {
|
|
|
1730
1759
|
const value = this.wrappedMachine[prop];
|
|
1731
1760
|
if (typeof value === "function") {
|
|
1732
1761
|
return (...args) => {
|
|
1733
|
-
const result = value.apply(this.wrappedMachine
|
|
1762
|
+
const result = value.apply(this.wrappedMachine, args);
|
|
1734
1763
|
if (result && typeof result === "object" && "context" in result) {
|
|
1735
1764
|
return new _BoundMachine(result);
|
|
1736
1765
|
}
|
|
@@ -2006,7 +2035,7 @@ var _Actor = class _Actor {
|
|
|
2006
2035
|
}
|
|
2007
2036
|
let result;
|
|
2008
2037
|
try {
|
|
2009
|
-
result = fn.apply(this._state
|
|
2038
|
+
result = fn.apply(this._state, event.args);
|
|
2010
2039
|
} catch (error) {
|
|
2011
2040
|
console.error(`[Actor] Error in transition '${String(event.type)}':`, error);
|
|
2012
2041
|
continue;
|
|
@@ -2084,62 +2113,73 @@ function fromObservable(observable) {
|
|
|
2084
2113
|
return actor;
|
|
2085
2114
|
}
|
|
2086
2115
|
|
|
2116
|
+
// src/context-bound.ts
|
|
2117
|
+
function createContextBoundMachine(initialContext, transformers) {
|
|
2118
|
+
const savedTransformers = transformers;
|
|
2119
|
+
const boundTransitions = Object.fromEntries(
|
|
2120
|
+
Object.entries(transformers).map(([key, transformer]) => [
|
|
2121
|
+
key,
|
|
2122
|
+
function(...args) {
|
|
2123
|
+
const contextOnly = { context: this.context };
|
|
2124
|
+
const newContext = transformer.apply(contextOnly, args);
|
|
2125
|
+
return createContextBoundMachine(newContext, savedTransformers);
|
|
2126
|
+
}
|
|
2127
|
+
])
|
|
2128
|
+
);
|
|
2129
|
+
Object.values(boundTransitions).forEach((fn) => {
|
|
2130
|
+
if (typeof fn === "function") {
|
|
2131
|
+
Object.defineProperty(fn, "__contextBound", {
|
|
2132
|
+
value: true,
|
|
2133
|
+
enumerable: false
|
|
2134
|
+
});
|
|
2135
|
+
}
|
|
2136
|
+
});
|
|
2137
|
+
return attachTransitions(
|
|
2138
|
+
Object.assign({ context: initialContext }, boundTransitions),
|
|
2139
|
+
boundTransitions
|
|
2140
|
+
);
|
|
2141
|
+
}
|
|
2142
|
+
function callWithContext(machine, transitionName, ...args) {
|
|
2143
|
+
const fn = machine[transitionName];
|
|
2144
|
+
const contextOnly = { context: machine.context };
|
|
2145
|
+
return fn.apply(contextOnly, args);
|
|
2146
|
+
}
|
|
2147
|
+
function isContextBound(machine) {
|
|
2148
|
+
const firstTransition = Object.values(machine).find(
|
|
2149
|
+
(v) => typeof v === "function"
|
|
2150
|
+
);
|
|
2151
|
+
if (!firstTransition) return false;
|
|
2152
|
+
return firstTransition.__contextBound === true;
|
|
2153
|
+
}
|
|
2154
|
+
|
|
2087
2155
|
// src/index.ts
|
|
2088
2156
|
function createMachine(context, fnsOrFactory) {
|
|
2089
2157
|
if (typeof fnsOrFactory === "function") {
|
|
2090
2158
|
let transitions2;
|
|
2091
2159
|
const transition = (newContext) => {
|
|
2092
|
-
|
|
2093
|
-
const boundTransitions2 = Object.fromEntries(
|
|
2094
|
-
Object.entries(transitions2).map(([key, fn]) => [
|
|
2095
|
-
key,
|
|
2096
|
-
fn.bind(newContext)
|
|
2097
|
-
])
|
|
2098
|
-
);
|
|
2099
|
-
return Object.assign(machine2, boundTransitions2);
|
|
2160
|
+
return createMachine(newContext, transitions2);
|
|
2100
2161
|
};
|
|
2101
2162
|
transitions2 = fnsOrFactory(transition);
|
|
2102
|
-
|
|
2103
|
-
Object.entries(transitions2).map(([key, fn]) => [
|
|
2104
|
-
key,
|
|
2105
|
-
fn.bind(context)
|
|
2106
|
-
])
|
|
2107
|
-
);
|
|
2108
|
-
return Object.assign({ context }, boundTransitions);
|
|
2163
|
+
return attachTransitions(Object.assign({ context }, transitions2), transitions2);
|
|
2109
2164
|
}
|
|
2110
|
-
const
|
|
2111
|
-
|
|
2112
|
-
) : fnsOrFactory;
|
|
2165
|
+
const stored = getStoredTransitions(fnsOrFactory);
|
|
2166
|
+
const transitions = stored != null ? stored : "context" in fnsOrFactory ? snapshotOwnTransitions(fnsOrFactory) : fnsOrFactory;
|
|
2113
2167
|
const machine = Object.assign({ context }, transitions);
|
|
2114
|
-
return machine;
|
|
2168
|
+
return attachTransitions(machine, transitions);
|
|
2115
2169
|
}
|
|
2116
2170
|
function createAsyncMachine(context, fnsOrFactory) {
|
|
2117
2171
|
if (typeof fnsOrFactory === "function") {
|
|
2118
2172
|
let transitions2;
|
|
2119
2173
|
const transition = (newContext) => {
|
|
2120
|
-
|
|
2121
|
-
const boundTransitions2 = Object.fromEntries(
|
|
2122
|
-
Object.entries(transitions2).map(([key, fn]) => [
|
|
2123
|
-
key,
|
|
2124
|
-
fn.bind(newContext)
|
|
2125
|
-
])
|
|
2126
|
-
);
|
|
2127
|
-
return Object.assign(machine2, boundTransitions2);
|
|
2174
|
+
return createAsyncMachine(newContext, transitions2);
|
|
2128
2175
|
};
|
|
2129
2176
|
transitions2 = fnsOrFactory(transition);
|
|
2130
|
-
|
|
2131
|
-
Object.entries(transitions2).map(([key, fn]) => [
|
|
2132
|
-
key,
|
|
2133
|
-
fn.bind(context)
|
|
2134
|
-
])
|
|
2135
|
-
);
|
|
2136
|
-
return Object.assign({ context }, boundTransitions);
|
|
2177
|
+
return attachTransitions(Object.assign({ context }, transitions2), transitions2);
|
|
2137
2178
|
}
|
|
2138
|
-
const
|
|
2139
|
-
|
|
2140
|
-
) : fnsOrFactory;
|
|
2179
|
+
const stored = getStoredTransitions(fnsOrFactory);
|
|
2180
|
+
const transitions = stored != null ? stored : "context" in fnsOrFactory ? snapshotOwnTransitions(fnsOrFactory) : fnsOrFactory;
|
|
2141
2181
|
const machine = Object.assign({ context }, transitions);
|
|
2142
|
-
return machine;
|
|
2182
|
+
return attachTransitions(machine, transitions);
|
|
2143
2183
|
}
|
|
2144
2184
|
function createMachineFactory() {
|
|
2145
2185
|
return (transformers) => {
|
|
@@ -2158,8 +2198,10 @@ function createMachineFactory() {
|
|
|
2158
2198
|
};
|
|
2159
2199
|
}
|
|
2160
2200
|
function setContext(machine, newContextOrFn) {
|
|
2161
|
-
|
|
2162
|
-
const
|
|
2201
|
+
var _a;
|
|
2202
|
+
const currentContext = machine.context;
|
|
2203
|
+
const transitions = (_a = getStoredTransitions(machine)) != null ? _a : snapshotOwnTransitions(machine);
|
|
2204
|
+
const newContext = typeof newContextOrFn === "function" ? newContextOrFn(currentContext) : newContextOrFn;
|
|
2163
2205
|
return createMachine(newContext, transitions);
|
|
2164
2206
|
}
|
|
2165
2207
|
function createContext(context) {
|
|
@@ -2218,7 +2260,7 @@ function runMachine(initial, onChange) {
|
|
|
2218
2260
|
const controller = new AbortController();
|
|
2219
2261
|
activeController = controller;
|
|
2220
2262
|
try {
|
|
2221
|
-
const nextStatePromise = fn.apply(current
|
|
2263
|
+
const nextStatePromise = fn.apply(current, [...event.args, { signal: controller.signal }]);
|
|
2222
2264
|
const nextState = await nextStatePromise;
|
|
2223
2265
|
if (controller.signal.aborted) {
|
|
2224
2266
|
return current;
|
|
@@ -2258,7 +2300,6 @@ var MachineBase = class {
|
|
|
2258
2300
|
}
|
|
2259
2301
|
};
|
|
2260
2302
|
function next(m, update) {
|
|
2261
|
-
|
|
2262
|
-
return createMachine(update(context), transitions);
|
|
2303
|
+
return setContext(m, (ctx) => update(ctx));
|
|
2263
2304
|
}
|
|
2264
2305
|
//# sourceMappingURL=index.js.map
|