@doeixd/machine 0.0.20 → 0.0.21
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.map +1 -1
- package/dist/cjs/development/index.js +175 -0
- package/dist/cjs/development/index.js.map +4 -4
- package/dist/cjs/production/index.js +3 -3
- package/dist/esm/development/core.js.map +1 -1
- package/dist/esm/development/index.js +175 -0
- package/dist/esm/development/index.js.map +4 -4
- package/dist/esm/production/index.js +3 -3
- package/dist/types/actor.d.ts +153 -0
- package/dist/types/actor.d.ts.map +1 -0
- package/dist/types/index.d.ts +2 -1
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/actor.ts +284 -0
- package/src/index.ts +16 -2
- package/src/react.ts +95 -124
|
@@ -1683,6 +1683,176 @@ function forContext() {
|
|
|
1683
1683
|
};
|
|
1684
1684
|
}
|
|
1685
1685
|
|
|
1686
|
+
// src/actor.ts
|
|
1687
|
+
var _Actor = class _Actor {
|
|
1688
|
+
constructor(initialMachine) {
|
|
1689
|
+
this._observers = /* @__PURE__ */ new Set();
|
|
1690
|
+
this._queue = [];
|
|
1691
|
+
this._processing = false;
|
|
1692
|
+
this._state = initialMachine;
|
|
1693
|
+
this.ref = {
|
|
1694
|
+
send: (event) => this.dispatch(event)
|
|
1695
|
+
};
|
|
1696
|
+
this.send = new Proxy({}, {
|
|
1697
|
+
get: (_target, prop) => {
|
|
1698
|
+
return (...args) => {
|
|
1699
|
+
this.dispatch({ type: prop, args });
|
|
1700
|
+
};
|
|
1701
|
+
}
|
|
1702
|
+
});
|
|
1703
|
+
}
|
|
1704
|
+
/**
|
|
1705
|
+
* Registers a global inspector.
|
|
1706
|
+
*/
|
|
1707
|
+
static inspect(inspector) {
|
|
1708
|
+
_Actor._inspector = inspector;
|
|
1709
|
+
}
|
|
1710
|
+
/**
|
|
1711
|
+
* Returns the current immutable snapshot of the machine.
|
|
1712
|
+
*/
|
|
1713
|
+
getSnapshot() {
|
|
1714
|
+
return this._state;
|
|
1715
|
+
}
|
|
1716
|
+
/**
|
|
1717
|
+
* Subscribes to state changes.
|
|
1718
|
+
* @param observer Callback function to be invoked on every state change.
|
|
1719
|
+
* @returns Unsubscribe function.
|
|
1720
|
+
*/
|
|
1721
|
+
subscribe(observer) {
|
|
1722
|
+
this._observers.add(observer);
|
|
1723
|
+
return () => {
|
|
1724
|
+
this._observers.delete(observer);
|
|
1725
|
+
};
|
|
1726
|
+
}
|
|
1727
|
+
/**
|
|
1728
|
+
* Selects a slice of the state.
|
|
1729
|
+
*/
|
|
1730
|
+
select(selector) {
|
|
1731
|
+
return selector(this._state);
|
|
1732
|
+
}
|
|
1733
|
+
/**
|
|
1734
|
+
* Starts the actor.
|
|
1735
|
+
*/
|
|
1736
|
+
start() {
|
|
1737
|
+
return this;
|
|
1738
|
+
}
|
|
1739
|
+
/**
|
|
1740
|
+
* Stops the actor.
|
|
1741
|
+
*/
|
|
1742
|
+
stop() {
|
|
1743
|
+
this._observers.clear();
|
|
1744
|
+
}
|
|
1745
|
+
/**
|
|
1746
|
+
* Dispatches an event to the actor.
|
|
1747
|
+
* Handles both sync and async transitions.
|
|
1748
|
+
*/
|
|
1749
|
+
dispatch(event) {
|
|
1750
|
+
if (_Actor._inspector) {
|
|
1751
|
+
_Actor._inspector({
|
|
1752
|
+
type: "@actor/send",
|
|
1753
|
+
actor: this,
|
|
1754
|
+
event,
|
|
1755
|
+
snapshot: this._state
|
|
1756
|
+
});
|
|
1757
|
+
}
|
|
1758
|
+
if (this._processing) {
|
|
1759
|
+
this._queue.push(event);
|
|
1760
|
+
return;
|
|
1761
|
+
}
|
|
1762
|
+
this._processing = true;
|
|
1763
|
+
this._queue.push(event);
|
|
1764
|
+
this._flush();
|
|
1765
|
+
}
|
|
1766
|
+
_flush() {
|
|
1767
|
+
while (this._queue.length > 0) {
|
|
1768
|
+
const event = this._queue[0];
|
|
1769
|
+
this._queue.shift();
|
|
1770
|
+
const transitions = this._state;
|
|
1771
|
+
const fn = transitions[event.type];
|
|
1772
|
+
if (typeof fn !== "function") {
|
|
1773
|
+
console.warn(`[Actor] Transition '${String(event.type)}' not found.`);
|
|
1774
|
+
continue;
|
|
1775
|
+
}
|
|
1776
|
+
let result;
|
|
1777
|
+
try {
|
|
1778
|
+
result = fn.apply(this._state.context, event.args);
|
|
1779
|
+
} catch (error) {
|
|
1780
|
+
console.error(`[Actor] Error in transition '${String(event.type)}':`, error);
|
|
1781
|
+
continue;
|
|
1782
|
+
}
|
|
1783
|
+
if (result instanceof Promise) {
|
|
1784
|
+
result.then((nextState) => {
|
|
1785
|
+
this._state = nextState;
|
|
1786
|
+
this._notify();
|
|
1787
|
+
this._flush();
|
|
1788
|
+
}).catch((error) => {
|
|
1789
|
+
console.error(`[Actor] Async error in transition '${String(event.type)}':`, error);
|
|
1790
|
+
this._flush();
|
|
1791
|
+
});
|
|
1792
|
+
return;
|
|
1793
|
+
} else {
|
|
1794
|
+
this._state = result;
|
|
1795
|
+
this._notify();
|
|
1796
|
+
}
|
|
1797
|
+
}
|
|
1798
|
+
this._processing = false;
|
|
1799
|
+
}
|
|
1800
|
+
_notify() {
|
|
1801
|
+
const snapshot = this.getSnapshot();
|
|
1802
|
+
this._observers.forEach((obs) => obs(snapshot));
|
|
1803
|
+
}
|
|
1804
|
+
};
|
|
1805
|
+
// Global inspector
|
|
1806
|
+
_Actor._inspector = null;
|
|
1807
|
+
var Actor = _Actor;
|
|
1808
|
+
function createActor(machine) {
|
|
1809
|
+
return new Actor(machine);
|
|
1810
|
+
}
|
|
1811
|
+
function spawn(machine) {
|
|
1812
|
+
return createActor(machine);
|
|
1813
|
+
}
|
|
1814
|
+
function fromPromise(promiseFn) {
|
|
1815
|
+
const initial = { status: "pending", data: void 0, error: void 0 };
|
|
1816
|
+
const machine = createMachine(
|
|
1817
|
+
initial,
|
|
1818
|
+
(next2) => ({
|
|
1819
|
+
resolve(data) {
|
|
1820
|
+
return next2({ status: "resolved", data, error: void 0 });
|
|
1821
|
+
},
|
|
1822
|
+
reject(error) {
|
|
1823
|
+
return next2({ status: "rejected", error, data: void 0 });
|
|
1824
|
+
}
|
|
1825
|
+
})
|
|
1826
|
+
);
|
|
1827
|
+
const actor = createActor(machine);
|
|
1828
|
+
promiseFn().then((data) => actor.send.resolve(data)).catch((err) => actor.send.reject(err));
|
|
1829
|
+
return actor;
|
|
1830
|
+
}
|
|
1831
|
+
function fromObservable(observable) {
|
|
1832
|
+
const initial = { status: "active", value: void 0, error: void 0 };
|
|
1833
|
+
const machine = createMachine(
|
|
1834
|
+
initial,
|
|
1835
|
+
(next2) => ({
|
|
1836
|
+
next(value) {
|
|
1837
|
+
return next2({ status: "active", value, error: void 0 });
|
|
1838
|
+
},
|
|
1839
|
+
error(error) {
|
|
1840
|
+
return next2({ status: "error", error, value: void 0 });
|
|
1841
|
+
},
|
|
1842
|
+
complete() {
|
|
1843
|
+
return next2({ status: "done", value: void 0, error: void 0 });
|
|
1844
|
+
}
|
|
1845
|
+
})
|
|
1846
|
+
);
|
|
1847
|
+
const actor = createActor(machine);
|
|
1848
|
+
observable.subscribe(
|
|
1849
|
+
(val) => actor.send.next(val),
|
|
1850
|
+
(err) => actor.send.error(err),
|
|
1851
|
+
() => actor.send.complete()
|
|
1852
|
+
);
|
|
1853
|
+
return actor;
|
|
1854
|
+
}
|
|
1855
|
+
|
|
1686
1856
|
// src/index.ts
|
|
1687
1857
|
function createMachine(context, fnsOrFactory) {
|
|
1688
1858
|
if (typeof fnsOrFactory === "function") {
|
|
@@ -1861,6 +2031,7 @@ function next(m, update) {
|
|
|
1861
2031
|
return createMachine(update(context), transitions);
|
|
1862
2032
|
}
|
|
1863
2033
|
export {
|
|
2034
|
+
Actor,
|
|
1864
2035
|
BoundMachine,
|
|
1865
2036
|
CANCEL,
|
|
1866
2037
|
META_KEY,
|
|
@@ -1877,6 +2048,7 @@ export {
|
|
|
1877
2048
|
combineFactories,
|
|
1878
2049
|
compose,
|
|
1879
2050
|
composeTyped,
|
|
2051
|
+
createActor,
|
|
1880
2052
|
createAsyncMachine,
|
|
1881
2053
|
createContext,
|
|
1882
2054
|
createCustomMiddleware,
|
|
@@ -1907,6 +2079,8 @@ export {
|
|
|
1907
2079
|
discriminantCase,
|
|
1908
2080
|
extendTransitions,
|
|
1909
2081
|
forContext,
|
|
2082
|
+
fromObservable,
|
|
2083
|
+
fromPromise,
|
|
1910
2084
|
guard,
|
|
1911
2085
|
guardAsync,
|
|
1912
2086
|
guarded,
|
|
@@ -1939,6 +2113,7 @@ export {
|
|
|
1939
2113
|
runWithEnsemble,
|
|
1940
2114
|
runWithRunner,
|
|
1941
2115
|
setContext,
|
|
2116
|
+
spawn,
|
|
1942
2117
|
state,
|
|
1943
2118
|
step,
|
|
1944
2119
|
stepAsync,
|