@doeixd/machine 0.0.21 → 0.0.22
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 +19 -0
- package/dist/cjs/development/core.js.map +1 -1
- package/dist/cjs/development/index.js +102 -0
- package/dist/cjs/development/index.js.map +3 -3
- package/dist/cjs/production/index.js +3 -3
- package/dist/esm/development/core.js.map +1 -1
- package/dist/esm/development/index.js +102 -0
- package/dist/esm/development/index.js.map +3 -3
- package/dist/esm/production/index.js +3 -3
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/mixins.d.ts +118 -0
- package/dist/types/mixins.d.ts.map +1 -0
- package/package.json +1 -1
- package/src/index.ts +2 -0
- package/src/mixins.ts +308 -0
|
@@ -25,6 +25,8 @@ __export(src_exports, {
|
|
|
25
25
|
CANCEL: () => CANCEL,
|
|
26
26
|
META_KEY: () => META_KEY,
|
|
27
27
|
MachineBase: () => MachineBase,
|
|
28
|
+
MachineExclude: () => MachineExclude,
|
|
29
|
+
MachineUnion: () => MachineUnion,
|
|
28
30
|
MiddlewareBuilder: () => MiddlewareBuilder,
|
|
29
31
|
MultiMachineBase: () => MultiMachineBase,
|
|
30
32
|
action: () => action,
|
|
@@ -87,6 +89,8 @@ __export(src_exports, {
|
|
|
87
89
|
isPipelineConfig: () => isPipelineConfig,
|
|
88
90
|
isState: () => isState,
|
|
89
91
|
logState: () => logState,
|
|
92
|
+
machineExclude: () => machineExclude,
|
|
93
|
+
machineUnion: () => machineUnion,
|
|
90
94
|
matchMachine: () => matchMachine,
|
|
91
95
|
mergeContext: () => mergeContext,
|
|
92
96
|
metadata: () => metadata,
|
|
@@ -1563,6 +1567,104 @@ function withDebugging(machine) {
|
|
|
1563
1567
|
return withTimeTravel(withSnapshot(withHistory(machine)));
|
|
1564
1568
|
}
|
|
1565
1569
|
|
|
1570
|
+
// src/mixins.ts
|
|
1571
|
+
function getAllPropertyDescriptors(obj) {
|
|
1572
|
+
const descriptors = {};
|
|
1573
|
+
let current = obj;
|
|
1574
|
+
while (current && current !== Object.prototype) {
|
|
1575
|
+
const props = Object.getOwnPropertyDescriptors(current);
|
|
1576
|
+
for (const [key, desc] of Object.entries(props)) {
|
|
1577
|
+
if (key === "constructor") continue;
|
|
1578
|
+
if (!(key in descriptors)) {
|
|
1579
|
+
descriptors[key] = desc;
|
|
1580
|
+
}
|
|
1581
|
+
}
|
|
1582
|
+
current = Object.getPrototypeOf(current);
|
|
1583
|
+
}
|
|
1584
|
+
return descriptors;
|
|
1585
|
+
}
|
|
1586
|
+
function MachineUnion(...machines) {
|
|
1587
|
+
const Base = machines[0];
|
|
1588
|
+
class CombinedMachine extends Base {
|
|
1589
|
+
constructor(context) {
|
|
1590
|
+
super(context);
|
|
1591
|
+
}
|
|
1592
|
+
}
|
|
1593
|
+
const wrapMethod = (fn) => {
|
|
1594
|
+
return function(...args) {
|
|
1595
|
+
const result = fn.apply(this, args);
|
|
1596
|
+
if (result && typeof result === "object" && "context" in result) {
|
|
1597
|
+
const newContext = { ...this.context, ...result.context };
|
|
1598
|
+
return new CombinedMachine(newContext);
|
|
1599
|
+
}
|
|
1600
|
+
return result;
|
|
1601
|
+
};
|
|
1602
|
+
};
|
|
1603
|
+
for (const machine of machines) {
|
|
1604
|
+
const descriptors = getAllPropertyDescriptors(machine.prototype);
|
|
1605
|
+
for (const [key, descriptor] of Object.entries(descriptors)) {
|
|
1606
|
+
if (key === "constructor") continue;
|
|
1607
|
+
if (typeof descriptor.value === "function") {
|
|
1608
|
+
const originalFn = descriptor.value;
|
|
1609
|
+
const wrappedFn = wrapMethod(originalFn);
|
|
1610
|
+
Object.defineProperty(CombinedMachine.prototype, key, {
|
|
1611
|
+
...descriptor,
|
|
1612
|
+
value: wrappedFn
|
|
1613
|
+
});
|
|
1614
|
+
} else {
|
|
1615
|
+
Object.defineProperty(CombinedMachine.prototype, key, descriptor);
|
|
1616
|
+
}
|
|
1617
|
+
}
|
|
1618
|
+
}
|
|
1619
|
+
return CombinedMachine;
|
|
1620
|
+
}
|
|
1621
|
+
function MachineExclude(Source, ...Excluded) {
|
|
1622
|
+
class ExcludedMachine extends MachineBase {
|
|
1623
|
+
constructor(context) {
|
|
1624
|
+
super(context);
|
|
1625
|
+
}
|
|
1626
|
+
}
|
|
1627
|
+
const sourceDescriptors = getAllPropertyDescriptors(Source.prototype);
|
|
1628
|
+
for (const [key, descriptor] of Object.entries(sourceDescriptors)) {
|
|
1629
|
+
if (key === "constructor") continue;
|
|
1630
|
+
if (typeof descriptor.value === "function") {
|
|
1631
|
+
const originalFn = descriptor.value;
|
|
1632
|
+
const wrappedFn = function(...args) {
|
|
1633
|
+
const result = originalFn.apply(this, args);
|
|
1634
|
+
if (result && typeof result === "object" && "context" in result) {
|
|
1635
|
+
return new ExcludedMachine({ ...this.context, ...result.context });
|
|
1636
|
+
}
|
|
1637
|
+
return result;
|
|
1638
|
+
};
|
|
1639
|
+
Object.defineProperty(ExcludedMachine.prototype, key, { ...descriptor, value: wrappedFn });
|
|
1640
|
+
} else {
|
|
1641
|
+
Object.defineProperty(ExcludedMachine.prototype, key, descriptor);
|
|
1642
|
+
}
|
|
1643
|
+
}
|
|
1644
|
+
for (const Excl of Excluded) {
|
|
1645
|
+
const excludedDescriptors = getAllPropertyDescriptors(Excl.prototype);
|
|
1646
|
+
for (const key of Object.keys(excludedDescriptors)) {
|
|
1647
|
+
if (Object.prototype.hasOwnProperty.call(ExcludedMachine.prototype, key)) {
|
|
1648
|
+
delete ExcludedMachine.prototype[key];
|
|
1649
|
+
}
|
|
1650
|
+
}
|
|
1651
|
+
}
|
|
1652
|
+
return ExcludedMachine;
|
|
1653
|
+
}
|
|
1654
|
+
function machineUnion(...instances) {
|
|
1655
|
+
const constructors = instances.map((i) => i.constructor);
|
|
1656
|
+
const contexts = instances.map((i) => i.context);
|
|
1657
|
+
const mergedContext = Object.assign({}, ...contexts);
|
|
1658
|
+
const CombinedClass = MachineUnion(...constructors);
|
|
1659
|
+
return new CombinedClass(mergedContext);
|
|
1660
|
+
}
|
|
1661
|
+
function machineExclude(source, ...excluded) {
|
|
1662
|
+
const sourceCtor = source.constructor;
|
|
1663
|
+
const excludedCtors = excluded.map((e) => e.constructor);
|
|
1664
|
+
const ExcludedClass = MachineExclude(sourceCtor, ...excludedCtors);
|
|
1665
|
+
return new ExcludedClass(source.context);
|
|
1666
|
+
}
|
|
1667
|
+
|
|
1566
1668
|
// src/utils.ts
|
|
1567
1669
|
function isState(machine, machineClass) {
|
|
1568
1670
|
return machine instanceof machineClass;
|