@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.
- package/dist/cjs/development/core.js +39 -14
- package/dist/cjs/development/core.js.map +3 -3
- package/dist/cjs/development/index.js +51 -15
- package/dist/cjs/development/index.js.map +3 -3
- package/dist/cjs/development/react.js +39 -14
- package/dist/cjs/development/react.js.map +3 -3
- package/dist/cjs/production/core.js +1 -1
- package/dist/cjs/production/index.js +2 -2
- package/dist/cjs/production/react.js +1 -1
- package/dist/esm/development/core.js +39 -14
- package/dist/esm/development/core.js.map +3 -3
- package/dist/esm/development/index.js +51 -15
- package/dist/esm/development/index.js.map +3 -3
- package/dist/esm/development/react.js +39 -14
- package/dist/esm/development/react.js.map +3 -3
- package/dist/esm/production/core.js +1 -1
- package/dist/esm/production/index.js +2 -2
- package/dist/esm/production/react.js +1 -1
- package/dist/types/context-bound.d.ts.map +1 -1
- package/dist/types/index.d.ts +0 -5
- 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/package.json +1 -1
- package/src/context-bound.ts +14 -1
- package/src/index.ts +19 -15
- package/src/internal-transitions.ts +32 -0
|
@@ -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);
|
|
@@ -1565,13 +1591,12 @@ function createMachine(context, fnsOrFactory) {
|
|
|
1565
1591
|
return createMachine(newContext, transitions2);
|
|
1566
1592
|
};
|
|
1567
1593
|
transitions2 = fnsOrFactory(transition);
|
|
1568
|
-
return Object.assign({ context }, transitions2);
|
|
1594
|
+
return attachTransitions(Object.assign({ context }, transitions2), transitions2);
|
|
1569
1595
|
}
|
|
1570
|
-
const
|
|
1571
|
-
|
|
1572
|
-
) : fnsOrFactory;
|
|
1596
|
+
const stored = getStoredTransitions(fnsOrFactory);
|
|
1597
|
+
const transitions = stored != null ? stored : "context" in fnsOrFactory ? snapshotOwnTransitions(fnsOrFactory) : fnsOrFactory;
|
|
1573
1598
|
const machine = Object.assign({ context }, transitions);
|
|
1574
|
-
return machine;
|
|
1599
|
+
return attachTransitions(machine, transitions);
|
|
1575
1600
|
}
|
|
1576
1601
|
function createAsyncMachine(context, fnsOrFactory) {
|
|
1577
1602
|
if (typeof fnsOrFactory === "function") {
|
|
@@ -1580,13 +1605,12 @@ function createAsyncMachine(context, fnsOrFactory) {
|
|
|
1580
1605
|
return createAsyncMachine(newContext, transitions2);
|
|
1581
1606
|
};
|
|
1582
1607
|
transitions2 = fnsOrFactory(transition);
|
|
1583
|
-
return Object.assign({ context }, transitions2);
|
|
1608
|
+
return attachTransitions(Object.assign({ context }, transitions2), transitions2);
|
|
1584
1609
|
}
|
|
1585
|
-
const
|
|
1586
|
-
|
|
1587
|
-
) : fnsOrFactory;
|
|
1610
|
+
const stored = getStoredTransitions(fnsOrFactory);
|
|
1611
|
+
const transitions = stored != null ? stored : "context" in fnsOrFactory ? snapshotOwnTransitions(fnsOrFactory) : fnsOrFactory;
|
|
1588
1612
|
const machine = Object.assign({ context }, transitions);
|
|
1589
|
-
return machine;
|
|
1613
|
+
return attachTransitions(machine, transitions);
|
|
1590
1614
|
}
|
|
1591
1615
|
function createMachineFactory() {
|
|
1592
1616
|
return (transformers) => {
|
|
@@ -1605,8 +1629,10 @@ function createMachineFactory() {
|
|
|
1605
1629
|
};
|
|
1606
1630
|
}
|
|
1607
1631
|
function setContext(machine, newContextOrFn) {
|
|
1608
|
-
|
|
1609
|
-
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;
|
|
1610
1636
|
return createMachine(newContext, transitions);
|
|
1611
1637
|
}
|
|
1612
1638
|
function overrideTransitions(machine, overrides) {
|
|
@@ -1702,8 +1728,7 @@ var MachineBase = class {
|
|
|
1702
1728
|
}
|
|
1703
1729
|
};
|
|
1704
1730
|
function next(m, update) {
|
|
1705
|
-
|
|
1706
|
-
return createMachine(update(context), transitions);
|
|
1731
|
+
return setContext(m, (ctx) => update(ctx));
|
|
1707
1732
|
}
|
|
1708
1733
|
|
|
1709
1734
|
// src/react.ts
|