@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;
|
|
@@ -1465,8 +1491,8 @@ function createTransition(getTransitions, transformer) {
|
|
|
1465
1491
|
return createMachine(nextContext, getTransitions());
|
|
1466
1492
|
};
|
|
1467
1493
|
}
|
|
1468
|
-
function call(fn,
|
|
1469
|
-
return fn.apply(
|
|
1494
|
+
function call(fn, machine, ...args) {
|
|
1495
|
+
return fn.apply(machine, args);
|
|
1470
1496
|
}
|
|
1471
1497
|
function bindTransitions(machine) {
|
|
1472
1498
|
return new Proxy(machine, {
|
|
@@ -1474,7 +1500,7 @@ function bindTransitions(machine) {
|
|
|
1474
1500
|
const value = target[prop];
|
|
1475
1501
|
if (typeof value === "function") {
|
|
1476
1502
|
return function(...args) {
|
|
1477
|
-
const result = value.apply(target
|
|
1503
|
+
const result = value.apply(target, args);
|
|
1478
1504
|
if (result && typeof result === "object" && "context" in result) {
|
|
1479
1505
|
return bindTransitions(result);
|
|
1480
1506
|
}
|
|
@@ -1499,7 +1525,7 @@ var BoundMachine = class _BoundMachine {
|
|
|
1499
1525
|
const value = this.wrappedMachine[prop];
|
|
1500
1526
|
if (typeof value === "function") {
|
|
1501
1527
|
return (...args) => {
|
|
1502
|
-
const result = value.apply(this.wrappedMachine
|
|
1528
|
+
const result = value.apply(this.wrappedMachine, args);
|
|
1503
1529
|
if (result && typeof result === "object" && "context" in result) {
|
|
1504
1530
|
return new _BoundMachine(result);
|
|
1505
1531
|
}
|
|
@@ -1562,57 +1588,29 @@ function createMachine(context, fnsOrFactory) {
|
|
|
1562
1588
|
if (typeof fnsOrFactory === "function") {
|
|
1563
1589
|
let transitions2;
|
|
1564
1590
|
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);
|
|
1591
|
+
return createMachine(newContext, transitions2);
|
|
1573
1592
|
};
|
|
1574
1593
|
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);
|
|
1594
|
+
return attachTransitions(Object.assign({ context }, transitions2), transitions2);
|
|
1582
1595
|
}
|
|
1583
|
-
const
|
|
1584
|
-
|
|
1585
|
-
) : fnsOrFactory;
|
|
1596
|
+
const stored = getStoredTransitions(fnsOrFactory);
|
|
1597
|
+
const transitions = stored != null ? stored : "context" in fnsOrFactory ? snapshotOwnTransitions(fnsOrFactory) : fnsOrFactory;
|
|
1586
1598
|
const machine = Object.assign({ context }, transitions);
|
|
1587
|
-
return machine;
|
|
1599
|
+
return attachTransitions(machine, transitions);
|
|
1588
1600
|
}
|
|
1589
1601
|
function createAsyncMachine(context, fnsOrFactory) {
|
|
1590
1602
|
if (typeof fnsOrFactory === "function") {
|
|
1591
1603
|
let transitions2;
|
|
1592
1604
|
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);
|
|
1605
|
+
return createAsyncMachine(newContext, transitions2);
|
|
1601
1606
|
};
|
|
1602
1607
|
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);
|
|
1608
|
+
return attachTransitions(Object.assign({ context }, transitions2), transitions2);
|
|
1610
1609
|
}
|
|
1611
|
-
const
|
|
1612
|
-
|
|
1613
|
-
) : fnsOrFactory;
|
|
1610
|
+
const stored = getStoredTransitions(fnsOrFactory);
|
|
1611
|
+
const transitions = stored != null ? stored : "context" in fnsOrFactory ? snapshotOwnTransitions(fnsOrFactory) : fnsOrFactory;
|
|
1614
1612
|
const machine = Object.assign({ context }, transitions);
|
|
1615
|
-
return machine;
|
|
1613
|
+
return attachTransitions(machine, transitions);
|
|
1616
1614
|
}
|
|
1617
1615
|
function createMachineFactory() {
|
|
1618
1616
|
return (transformers) => {
|
|
@@ -1631,8 +1629,10 @@ function createMachineFactory() {
|
|
|
1631
1629
|
};
|
|
1632
1630
|
}
|
|
1633
1631
|
function setContext(machine, newContextOrFn) {
|
|
1634
|
-
|
|
1635
|
-
const
|
|
1632
|
+
var _a;
|
|
1633
|
+
const currentContext = machine.context;
|
|
1634
|
+
const transitions = (_a = getStoredTransitions(machine)) != null ? _a : snapshotOwnTransitions(machine);
|
|
1635
|
+
const newContext = typeof newContextOrFn === "function" ? newContextOrFn(currentContext) : newContextOrFn;
|
|
1636
1636
|
return createMachine(newContext, transitions);
|
|
1637
1637
|
}
|
|
1638
1638
|
function overrideTransitions(machine, overrides) {
|
|
@@ -1688,7 +1688,7 @@ function runMachine(initial, onChange) {
|
|
|
1688
1688
|
const controller = new AbortController();
|
|
1689
1689
|
activeController = controller;
|
|
1690
1690
|
try {
|
|
1691
|
-
const nextStatePromise = fn.apply(current
|
|
1691
|
+
const nextStatePromise = fn.apply(current, [...event.args, { signal: controller.signal }]);
|
|
1692
1692
|
const nextState = await nextStatePromise;
|
|
1693
1693
|
if (controller.signal.aborted) {
|
|
1694
1694
|
return current;
|
|
@@ -1728,8 +1728,7 @@ var MachineBase = class {
|
|
|
1728
1728
|
}
|
|
1729
1729
|
};
|
|
1730
1730
|
function next(m, update) {
|
|
1731
|
-
|
|
1732
|
-
return createMachine(update(context), transitions);
|
|
1731
|
+
return setContext(m, (ctx) => update(ctx));
|
|
1733
1732
|
}
|
|
1734
1733
|
|
|
1735
1734
|
// src/react.ts
|
|
@@ -1835,7 +1834,6 @@ function useActor(actor) {
|
|
|
1835
1834
|
return useSyncExternalStore(subscribe, getSnapshot);
|
|
1836
1835
|
}
|
|
1837
1836
|
function useActorSelector(actor, selector, isEqual = Object.is) {
|
|
1838
|
-
const subscribe = useMemo(() => actor.subscribe.bind(actor), [actor]);
|
|
1839
1837
|
const getSnapshot = useMemo(() => actor.getSnapshot.bind(actor), [actor]);
|
|
1840
1838
|
const getSelection = () => selector(getSnapshot());
|
|
1841
1839
|
const [selection, setSelection] = useState(getSelection);
|