@doeixd/machine 0.0.23 → 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.
- package/README.md +101 -65
- package/dist/cjs/development/core.js +19 -45
- package/dist/cjs/development/core.js.map +3 -3
- package/dist/cjs/development/index.js +51 -46
- package/dist/cjs/development/index.js.map +4 -4
- package/dist/cjs/development/react.js +19 -46
- package/dist/cjs/development/react.js.map +3 -3
- 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 +19 -45
- package/dist/esm/development/core.js.map +3 -3
- package/dist/esm/development/index.js +51 -46
- package/dist/esm/development/index.js.map +4 -4
- package/dist/esm/development/react.js +19 -46
- package/dist/esm/development/react.js.map +3 -3
- 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 -29
- package/dist/types/index.d.ts.map +1 -1
- 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 +147 -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 +31 -68
- 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
|
@@ -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
|
|
132
|
-
return transition.apply(
|
|
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
|
|
173
|
-
return transition.apply(
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
1048
|
+
replayedMachine = transitionFn.apply(replayedMachine, entry.args);
|
|
1049
1049
|
}
|
|
1050
1050
|
}
|
|
1051
1051
|
return replayedMachine;
|
|
@@ -1465,8 +1465,8 @@ function createTransition(getTransitions, transformer) {
|
|
|
1465
1465
|
return createMachine(nextContext, getTransitions());
|
|
1466
1466
|
};
|
|
1467
1467
|
}
|
|
1468
|
-
function call(fn,
|
|
1469
|
-
return fn.apply(
|
|
1468
|
+
function call(fn, machine, ...args) {
|
|
1469
|
+
return fn.apply(machine, args);
|
|
1470
1470
|
}
|
|
1471
1471
|
function bindTransitions(machine) {
|
|
1472
1472
|
return new Proxy(machine, {
|
|
@@ -1474,7 +1474,7 @@ function bindTransitions(machine) {
|
|
|
1474
1474
|
const value = target[prop];
|
|
1475
1475
|
if (typeof value === "function") {
|
|
1476
1476
|
return function(...args) {
|
|
1477
|
-
const result = value.apply(target
|
|
1477
|
+
const result = value.apply(target, args);
|
|
1478
1478
|
if (result && typeof result === "object" && "context" in result) {
|
|
1479
1479
|
return bindTransitions(result);
|
|
1480
1480
|
}
|
|
@@ -1499,7 +1499,7 @@ var BoundMachine = class _BoundMachine {
|
|
|
1499
1499
|
const value = this.wrappedMachine[prop];
|
|
1500
1500
|
if (typeof value === "function") {
|
|
1501
1501
|
return (...args) => {
|
|
1502
|
-
const result = value.apply(this.wrappedMachine
|
|
1502
|
+
const result = value.apply(this.wrappedMachine, args);
|
|
1503
1503
|
if (result && typeof result === "object" && "context" in result) {
|
|
1504
1504
|
return new _BoundMachine(result);
|
|
1505
1505
|
}
|
|
@@ -1562,23 +1562,10 @@ function createMachine(context, fnsOrFactory) {
|
|
|
1562
1562
|
if (typeof fnsOrFactory === "function") {
|
|
1563
1563
|
let transitions2;
|
|
1564
1564
|
const transition = (newContext) => {
|
|
1565
|
-
|
|
1566
|
-
const boundTransitions2 = Object.fromEntries(
|
|
1567
|
-
Object.entries(transitions2).map(([key, fn]) => [
|
|
1568
|
-
key,
|
|
1569
|
-
fn.bind(newContext)
|
|
1570
|
-
])
|
|
1571
|
-
);
|
|
1572
|
-
return Object.assign(machine2, boundTransitions2);
|
|
1565
|
+
return createMachine(newContext, transitions2);
|
|
1573
1566
|
};
|
|
1574
1567
|
transitions2 = fnsOrFactory(transition);
|
|
1575
|
-
|
|
1576
|
-
Object.entries(transitions2).map(([key, fn]) => [
|
|
1577
|
-
key,
|
|
1578
|
-
fn.bind(context)
|
|
1579
|
-
])
|
|
1580
|
-
);
|
|
1581
|
-
return Object.assign({ context }, boundTransitions);
|
|
1568
|
+
return Object.assign({ context }, transitions2);
|
|
1582
1569
|
}
|
|
1583
1570
|
const transitions = "context" in fnsOrFactory ? Object.fromEntries(
|
|
1584
1571
|
Object.entries(fnsOrFactory).filter(([key]) => key !== "context")
|
|
@@ -1590,23 +1577,10 @@ function createAsyncMachine(context, fnsOrFactory) {
|
|
|
1590
1577
|
if (typeof fnsOrFactory === "function") {
|
|
1591
1578
|
let transitions2;
|
|
1592
1579
|
const transition = (newContext) => {
|
|
1593
|
-
|
|
1594
|
-
const boundTransitions2 = Object.fromEntries(
|
|
1595
|
-
Object.entries(transitions2).map(([key, fn]) => [
|
|
1596
|
-
key,
|
|
1597
|
-
fn.bind(newContext)
|
|
1598
|
-
])
|
|
1599
|
-
);
|
|
1600
|
-
return Object.assign(machine2, boundTransitions2);
|
|
1580
|
+
return createAsyncMachine(newContext, transitions2);
|
|
1601
1581
|
};
|
|
1602
1582
|
transitions2 = fnsOrFactory(transition);
|
|
1603
|
-
|
|
1604
|
-
Object.entries(transitions2).map(([key, fn]) => [
|
|
1605
|
-
key,
|
|
1606
|
-
fn.bind(context)
|
|
1607
|
-
])
|
|
1608
|
-
);
|
|
1609
|
-
return Object.assign({ context }, boundTransitions);
|
|
1583
|
+
return Object.assign({ context }, transitions2);
|
|
1610
1584
|
}
|
|
1611
1585
|
const transitions = "context" in fnsOrFactory ? Object.fromEntries(
|
|
1612
1586
|
Object.entries(fnsOrFactory).filter(([key]) => key !== "context")
|
|
@@ -1688,7 +1662,7 @@ function runMachine(initial, onChange) {
|
|
|
1688
1662
|
const controller = new AbortController();
|
|
1689
1663
|
activeController = controller;
|
|
1690
1664
|
try {
|
|
1691
|
-
const nextStatePromise = fn.apply(current
|
|
1665
|
+
const nextStatePromise = fn.apply(current, [...event.args, { signal: controller.signal }]);
|
|
1692
1666
|
const nextState = await nextStatePromise;
|
|
1693
1667
|
if (controller.signal.aborted) {
|
|
1694
1668
|
return current;
|