@doeixd/machine 0.0.20 → 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 +277 -0
- package/dist/cjs/development/index.js.map +4 -4
- package/dist/cjs/production/index.js +4 -4
- package/dist/esm/development/core.js.map +1 -1
- package/dist/esm/development/index.js +277 -0
- package/dist/esm/development/index.js.map +4 -4
- package/dist/esm/production/index.js +4 -4
- package/dist/types/actor.d.ts +153 -0
- package/dist/types/actor.d.ts.map +1 -0
- package/dist/types/index.d.ts +3 -1
- 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/actor.ts +284 -0
- package/src/index.ts +18 -2
- package/src/mixins.ts +308 -0
- package/src/react.ts +95 -124
|
@@ -1434,6 +1434,104 @@ function withDebugging(machine) {
|
|
|
1434
1434
|
return withTimeTravel(withSnapshot(withHistory(machine)));
|
|
1435
1435
|
}
|
|
1436
1436
|
|
|
1437
|
+
// src/mixins.ts
|
|
1438
|
+
function getAllPropertyDescriptors(obj) {
|
|
1439
|
+
const descriptors = {};
|
|
1440
|
+
let current = obj;
|
|
1441
|
+
while (current && current !== Object.prototype) {
|
|
1442
|
+
const props = Object.getOwnPropertyDescriptors(current);
|
|
1443
|
+
for (const [key, desc] of Object.entries(props)) {
|
|
1444
|
+
if (key === "constructor") continue;
|
|
1445
|
+
if (!(key in descriptors)) {
|
|
1446
|
+
descriptors[key] = desc;
|
|
1447
|
+
}
|
|
1448
|
+
}
|
|
1449
|
+
current = Object.getPrototypeOf(current);
|
|
1450
|
+
}
|
|
1451
|
+
return descriptors;
|
|
1452
|
+
}
|
|
1453
|
+
function MachineUnion(...machines) {
|
|
1454
|
+
const Base = machines[0];
|
|
1455
|
+
class CombinedMachine extends Base {
|
|
1456
|
+
constructor(context) {
|
|
1457
|
+
super(context);
|
|
1458
|
+
}
|
|
1459
|
+
}
|
|
1460
|
+
const wrapMethod = (fn) => {
|
|
1461
|
+
return function(...args) {
|
|
1462
|
+
const result = fn.apply(this, args);
|
|
1463
|
+
if (result && typeof result === "object" && "context" in result) {
|
|
1464
|
+
const newContext = { ...this.context, ...result.context };
|
|
1465
|
+
return new CombinedMachine(newContext);
|
|
1466
|
+
}
|
|
1467
|
+
return result;
|
|
1468
|
+
};
|
|
1469
|
+
};
|
|
1470
|
+
for (const machine of machines) {
|
|
1471
|
+
const descriptors = getAllPropertyDescriptors(machine.prototype);
|
|
1472
|
+
for (const [key, descriptor] of Object.entries(descriptors)) {
|
|
1473
|
+
if (key === "constructor") continue;
|
|
1474
|
+
if (typeof descriptor.value === "function") {
|
|
1475
|
+
const originalFn = descriptor.value;
|
|
1476
|
+
const wrappedFn = wrapMethod(originalFn);
|
|
1477
|
+
Object.defineProperty(CombinedMachine.prototype, key, {
|
|
1478
|
+
...descriptor,
|
|
1479
|
+
value: wrappedFn
|
|
1480
|
+
});
|
|
1481
|
+
} else {
|
|
1482
|
+
Object.defineProperty(CombinedMachine.prototype, key, descriptor);
|
|
1483
|
+
}
|
|
1484
|
+
}
|
|
1485
|
+
}
|
|
1486
|
+
return CombinedMachine;
|
|
1487
|
+
}
|
|
1488
|
+
function MachineExclude(Source, ...Excluded) {
|
|
1489
|
+
class ExcludedMachine extends MachineBase {
|
|
1490
|
+
constructor(context) {
|
|
1491
|
+
super(context);
|
|
1492
|
+
}
|
|
1493
|
+
}
|
|
1494
|
+
const sourceDescriptors = getAllPropertyDescriptors(Source.prototype);
|
|
1495
|
+
for (const [key, descriptor] of Object.entries(sourceDescriptors)) {
|
|
1496
|
+
if (key === "constructor") continue;
|
|
1497
|
+
if (typeof descriptor.value === "function") {
|
|
1498
|
+
const originalFn = descriptor.value;
|
|
1499
|
+
const wrappedFn = function(...args) {
|
|
1500
|
+
const result = originalFn.apply(this, args);
|
|
1501
|
+
if (result && typeof result === "object" && "context" in result) {
|
|
1502
|
+
return new ExcludedMachine({ ...this.context, ...result.context });
|
|
1503
|
+
}
|
|
1504
|
+
return result;
|
|
1505
|
+
};
|
|
1506
|
+
Object.defineProperty(ExcludedMachine.prototype, key, { ...descriptor, value: wrappedFn });
|
|
1507
|
+
} else {
|
|
1508
|
+
Object.defineProperty(ExcludedMachine.prototype, key, descriptor);
|
|
1509
|
+
}
|
|
1510
|
+
}
|
|
1511
|
+
for (const Excl of Excluded) {
|
|
1512
|
+
const excludedDescriptors = getAllPropertyDescriptors(Excl.prototype);
|
|
1513
|
+
for (const key of Object.keys(excludedDescriptors)) {
|
|
1514
|
+
if (Object.prototype.hasOwnProperty.call(ExcludedMachine.prototype, key)) {
|
|
1515
|
+
delete ExcludedMachine.prototype[key];
|
|
1516
|
+
}
|
|
1517
|
+
}
|
|
1518
|
+
}
|
|
1519
|
+
return ExcludedMachine;
|
|
1520
|
+
}
|
|
1521
|
+
function machineUnion(...instances) {
|
|
1522
|
+
const constructors = instances.map((i) => i.constructor);
|
|
1523
|
+
const contexts = instances.map((i) => i.context);
|
|
1524
|
+
const mergedContext = Object.assign({}, ...contexts);
|
|
1525
|
+
const CombinedClass = MachineUnion(...constructors);
|
|
1526
|
+
return new CombinedClass(mergedContext);
|
|
1527
|
+
}
|
|
1528
|
+
function machineExclude(source, ...excluded) {
|
|
1529
|
+
const sourceCtor = source.constructor;
|
|
1530
|
+
const excludedCtors = excluded.map((e) => e.constructor);
|
|
1531
|
+
const ExcludedClass = MachineExclude(sourceCtor, ...excludedCtors);
|
|
1532
|
+
return new ExcludedClass(source.context);
|
|
1533
|
+
}
|
|
1534
|
+
|
|
1437
1535
|
// src/utils.ts
|
|
1438
1536
|
function isState(machine, machineClass) {
|
|
1439
1537
|
return machine instanceof machineClass;
|
|
@@ -1683,6 +1781,176 @@ function forContext() {
|
|
|
1683
1781
|
};
|
|
1684
1782
|
}
|
|
1685
1783
|
|
|
1784
|
+
// src/actor.ts
|
|
1785
|
+
var _Actor = class _Actor {
|
|
1786
|
+
constructor(initialMachine) {
|
|
1787
|
+
this._observers = /* @__PURE__ */ new Set();
|
|
1788
|
+
this._queue = [];
|
|
1789
|
+
this._processing = false;
|
|
1790
|
+
this._state = initialMachine;
|
|
1791
|
+
this.ref = {
|
|
1792
|
+
send: (event) => this.dispatch(event)
|
|
1793
|
+
};
|
|
1794
|
+
this.send = new Proxy({}, {
|
|
1795
|
+
get: (_target, prop) => {
|
|
1796
|
+
return (...args) => {
|
|
1797
|
+
this.dispatch({ type: prop, args });
|
|
1798
|
+
};
|
|
1799
|
+
}
|
|
1800
|
+
});
|
|
1801
|
+
}
|
|
1802
|
+
/**
|
|
1803
|
+
* Registers a global inspector.
|
|
1804
|
+
*/
|
|
1805
|
+
static inspect(inspector) {
|
|
1806
|
+
_Actor._inspector = inspector;
|
|
1807
|
+
}
|
|
1808
|
+
/**
|
|
1809
|
+
* Returns the current immutable snapshot of the machine.
|
|
1810
|
+
*/
|
|
1811
|
+
getSnapshot() {
|
|
1812
|
+
return this._state;
|
|
1813
|
+
}
|
|
1814
|
+
/**
|
|
1815
|
+
* Subscribes to state changes.
|
|
1816
|
+
* @param observer Callback function to be invoked on every state change.
|
|
1817
|
+
* @returns Unsubscribe function.
|
|
1818
|
+
*/
|
|
1819
|
+
subscribe(observer) {
|
|
1820
|
+
this._observers.add(observer);
|
|
1821
|
+
return () => {
|
|
1822
|
+
this._observers.delete(observer);
|
|
1823
|
+
};
|
|
1824
|
+
}
|
|
1825
|
+
/**
|
|
1826
|
+
* Selects a slice of the state.
|
|
1827
|
+
*/
|
|
1828
|
+
select(selector) {
|
|
1829
|
+
return selector(this._state);
|
|
1830
|
+
}
|
|
1831
|
+
/**
|
|
1832
|
+
* Starts the actor.
|
|
1833
|
+
*/
|
|
1834
|
+
start() {
|
|
1835
|
+
return this;
|
|
1836
|
+
}
|
|
1837
|
+
/**
|
|
1838
|
+
* Stops the actor.
|
|
1839
|
+
*/
|
|
1840
|
+
stop() {
|
|
1841
|
+
this._observers.clear();
|
|
1842
|
+
}
|
|
1843
|
+
/**
|
|
1844
|
+
* Dispatches an event to the actor.
|
|
1845
|
+
* Handles both sync and async transitions.
|
|
1846
|
+
*/
|
|
1847
|
+
dispatch(event) {
|
|
1848
|
+
if (_Actor._inspector) {
|
|
1849
|
+
_Actor._inspector({
|
|
1850
|
+
type: "@actor/send",
|
|
1851
|
+
actor: this,
|
|
1852
|
+
event,
|
|
1853
|
+
snapshot: this._state
|
|
1854
|
+
});
|
|
1855
|
+
}
|
|
1856
|
+
if (this._processing) {
|
|
1857
|
+
this._queue.push(event);
|
|
1858
|
+
return;
|
|
1859
|
+
}
|
|
1860
|
+
this._processing = true;
|
|
1861
|
+
this._queue.push(event);
|
|
1862
|
+
this._flush();
|
|
1863
|
+
}
|
|
1864
|
+
_flush() {
|
|
1865
|
+
while (this._queue.length > 0) {
|
|
1866
|
+
const event = this._queue[0];
|
|
1867
|
+
this._queue.shift();
|
|
1868
|
+
const transitions = this._state;
|
|
1869
|
+
const fn = transitions[event.type];
|
|
1870
|
+
if (typeof fn !== "function") {
|
|
1871
|
+
console.warn(`[Actor] Transition '${String(event.type)}' not found.`);
|
|
1872
|
+
continue;
|
|
1873
|
+
}
|
|
1874
|
+
let result;
|
|
1875
|
+
try {
|
|
1876
|
+
result = fn.apply(this._state.context, event.args);
|
|
1877
|
+
} catch (error) {
|
|
1878
|
+
console.error(`[Actor] Error in transition '${String(event.type)}':`, error);
|
|
1879
|
+
continue;
|
|
1880
|
+
}
|
|
1881
|
+
if (result instanceof Promise) {
|
|
1882
|
+
result.then((nextState) => {
|
|
1883
|
+
this._state = nextState;
|
|
1884
|
+
this._notify();
|
|
1885
|
+
this._flush();
|
|
1886
|
+
}).catch((error) => {
|
|
1887
|
+
console.error(`[Actor] Async error in transition '${String(event.type)}':`, error);
|
|
1888
|
+
this._flush();
|
|
1889
|
+
});
|
|
1890
|
+
return;
|
|
1891
|
+
} else {
|
|
1892
|
+
this._state = result;
|
|
1893
|
+
this._notify();
|
|
1894
|
+
}
|
|
1895
|
+
}
|
|
1896
|
+
this._processing = false;
|
|
1897
|
+
}
|
|
1898
|
+
_notify() {
|
|
1899
|
+
const snapshot = this.getSnapshot();
|
|
1900
|
+
this._observers.forEach((obs) => obs(snapshot));
|
|
1901
|
+
}
|
|
1902
|
+
};
|
|
1903
|
+
// Global inspector
|
|
1904
|
+
_Actor._inspector = null;
|
|
1905
|
+
var Actor = _Actor;
|
|
1906
|
+
function createActor(machine) {
|
|
1907
|
+
return new Actor(machine);
|
|
1908
|
+
}
|
|
1909
|
+
function spawn(machine) {
|
|
1910
|
+
return createActor(machine);
|
|
1911
|
+
}
|
|
1912
|
+
function fromPromise(promiseFn) {
|
|
1913
|
+
const initial = { status: "pending", data: void 0, error: void 0 };
|
|
1914
|
+
const machine = createMachine(
|
|
1915
|
+
initial,
|
|
1916
|
+
(next2) => ({
|
|
1917
|
+
resolve(data) {
|
|
1918
|
+
return next2({ status: "resolved", data, error: void 0 });
|
|
1919
|
+
},
|
|
1920
|
+
reject(error) {
|
|
1921
|
+
return next2({ status: "rejected", error, data: void 0 });
|
|
1922
|
+
}
|
|
1923
|
+
})
|
|
1924
|
+
);
|
|
1925
|
+
const actor = createActor(machine);
|
|
1926
|
+
promiseFn().then((data) => actor.send.resolve(data)).catch((err) => actor.send.reject(err));
|
|
1927
|
+
return actor;
|
|
1928
|
+
}
|
|
1929
|
+
function fromObservable(observable) {
|
|
1930
|
+
const initial = { status: "active", value: void 0, error: void 0 };
|
|
1931
|
+
const machine = createMachine(
|
|
1932
|
+
initial,
|
|
1933
|
+
(next2) => ({
|
|
1934
|
+
next(value) {
|
|
1935
|
+
return next2({ status: "active", value, error: void 0 });
|
|
1936
|
+
},
|
|
1937
|
+
error(error) {
|
|
1938
|
+
return next2({ status: "error", error, value: void 0 });
|
|
1939
|
+
},
|
|
1940
|
+
complete() {
|
|
1941
|
+
return next2({ status: "done", value: void 0, error: void 0 });
|
|
1942
|
+
}
|
|
1943
|
+
})
|
|
1944
|
+
);
|
|
1945
|
+
const actor = createActor(machine);
|
|
1946
|
+
observable.subscribe(
|
|
1947
|
+
(val) => actor.send.next(val),
|
|
1948
|
+
(err) => actor.send.error(err),
|
|
1949
|
+
() => actor.send.complete()
|
|
1950
|
+
);
|
|
1951
|
+
return actor;
|
|
1952
|
+
}
|
|
1953
|
+
|
|
1686
1954
|
// src/index.ts
|
|
1687
1955
|
function createMachine(context, fnsOrFactory) {
|
|
1688
1956
|
if (typeof fnsOrFactory === "function") {
|
|
@@ -1861,10 +2129,13 @@ function next(m, update) {
|
|
|
1861
2129
|
return createMachine(update(context), transitions);
|
|
1862
2130
|
}
|
|
1863
2131
|
export {
|
|
2132
|
+
Actor,
|
|
1864
2133
|
BoundMachine,
|
|
1865
2134
|
CANCEL,
|
|
1866
2135
|
META_KEY,
|
|
1867
2136
|
MachineBase,
|
|
2137
|
+
MachineExclude,
|
|
2138
|
+
MachineUnion,
|
|
1868
2139
|
MiddlewareBuilder,
|
|
1869
2140
|
MultiMachineBase,
|
|
1870
2141
|
action,
|
|
@@ -1877,6 +2148,7 @@ export {
|
|
|
1877
2148
|
combineFactories,
|
|
1878
2149
|
compose,
|
|
1879
2150
|
composeTyped,
|
|
2151
|
+
createActor,
|
|
1880
2152
|
createAsyncMachine,
|
|
1881
2153
|
createContext,
|
|
1882
2154
|
createCustomMiddleware,
|
|
@@ -1907,6 +2179,8 @@ export {
|
|
|
1907
2179
|
discriminantCase,
|
|
1908
2180
|
extendTransitions,
|
|
1909
2181
|
forContext,
|
|
2182
|
+
fromObservable,
|
|
2183
|
+
fromPromise,
|
|
1910
2184
|
guard,
|
|
1911
2185
|
guardAsync,
|
|
1912
2186
|
guarded,
|
|
@@ -1924,6 +2198,8 @@ export {
|
|
|
1924
2198
|
isPipelineConfig,
|
|
1925
2199
|
isState,
|
|
1926
2200
|
logState,
|
|
2201
|
+
machineExclude,
|
|
2202
|
+
machineUnion,
|
|
1927
2203
|
matchMachine,
|
|
1928
2204
|
mergeContext,
|
|
1929
2205
|
metadata,
|
|
@@ -1939,6 +2215,7 @@ export {
|
|
|
1939
2215
|
runWithEnsemble,
|
|
1940
2216
|
runWithRunner,
|
|
1941
2217
|
setContext,
|
|
2218
|
+
spawn,
|
|
1942
2219
|
state,
|
|
1943
2220
|
step,
|
|
1944
2221
|
stepAsync,
|