@doeixd/machine 0.0.19 → 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/README.md +71 -0
- 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/dist/types/matcher.d.ts +16 -9
- package/dist/types/matcher.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/actor.ts +284 -0
- package/src/index.ts +16 -2
- package/src/matcher.ts +37 -21
- package/src/react.ts +95 -124
|
@@ -20,6 +20,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/index.ts
|
|
21
21
|
var src_exports = {};
|
|
22
22
|
__export(src_exports, {
|
|
23
|
+
Actor: () => Actor,
|
|
23
24
|
BoundMachine: () => BoundMachine,
|
|
24
25
|
CANCEL: () => CANCEL,
|
|
25
26
|
META_KEY: () => META_KEY,
|
|
@@ -36,6 +37,7 @@ __export(src_exports, {
|
|
|
36
37
|
combineFactories: () => combineFactories,
|
|
37
38
|
compose: () => compose,
|
|
38
39
|
composeTyped: () => composeTyped,
|
|
40
|
+
createActor: () => createActor,
|
|
39
41
|
createAsyncMachine: () => createAsyncMachine,
|
|
40
42
|
createContext: () => createContext,
|
|
41
43
|
createCustomMiddleware: () => createCustomMiddleware,
|
|
@@ -66,6 +68,8 @@ __export(src_exports, {
|
|
|
66
68
|
discriminantCase: () => discriminantCase,
|
|
67
69
|
extendTransitions: () => extendTransitions,
|
|
68
70
|
forContext: () => forContext,
|
|
71
|
+
fromObservable: () => fromObservable,
|
|
72
|
+
fromPromise: () => fromPromise,
|
|
69
73
|
guard: () => guard,
|
|
70
74
|
guardAsync: () => guardAsync,
|
|
71
75
|
guarded: () => guarded,
|
|
@@ -98,6 +102,7 @@ __export(src_exports, {
|
|
|
98
102
|
runWithEnsemble: () => runWithEnsemble,
|
|
99
103
|
runWithRunner: () => runWithRunner,
|
|
100
104
|
setContext: () => setContext,
|
|
105
|
+
spawn: () => spawn,
|
|
101
106
|
state: () => state,
|
|
102
107
|
step: () => step,
|
|
103
108
|
stepAsync: () => stepAsync,
|
|
@@ -1807,6 +1812,176 @@ function forContext() {
|
|
|
1807
1812
|
};
|
|
1808
1813
|
}
|
|
1809
1814
|
|
|
1815
|
+
// src/actor.ts
|
|
1816
|
+
var _Actor = class _Actor {
|
|
1817
|
+
constructor(initialMachine) {
|
|
1818
|
+
this._observers = /* @__PURE__ */ new Set();
|
|
1819
|
+
this._queue = [];
|
|
1820
|
+
this._processing = false;
|
|
1821
|
+
this._state = initialMachine;
|
|
1822
|
+
this.ref = {
|
|
1823
|
+
send: (event) => this.dispatch(event)
|
|
1824
|
+
};
|
|
1825
|
+
this.send = new Proxy({}, {
|
|
1826
|
+
get: (_target, prop) => {
|
|
1827
|
+
return (...args) => {
|
|
1828
|
+
this.dispatch({ type: prop, args });
|
|
1829
|
+
};
|
|
1830
|
+
}
|
|
1831
|
+
});
|
|
1832
|
+
}
|
|
1833
|
+
/**
|
|
1834
|
+
* Registers a global inspector.
|
|
1835
|
+
*/
|
|
1836
|
+
static inspect(inspector) {
|
|
1837
|
+
_Actor._inspector = inspector;
|
|
1838
|
+
}
|
|
1839
|
+
/**
|
|
1840
|
+
* Returns the current immutable snapshot of the machine.
|
|
1841
|
+
*/
|
|
1842
|
+
getSnapshot() {
|
|
1843
|
+
return this._state;
|
|
1844
|
+
}
|
|
1845
|
+
/**
|
|
1846
|
+
* Subscribes to state changes.
|
|
1847
|
+
* @param observer Callback function to be invoked on every state change.
|
|
1848
|
+
* @returns Unsubscribe function.
|
|
1849
|
+
*/
|
|
1850
|
+
subscribe(observer) {
|
|
1851
|
+
this._observers.add(observer);
|
|
1852
|
+
return () => {
|
|
1853
|
+
this._observers.delete(observer);
|
|
1854
|
+
};
|
|
1855
|
+
}
|
|
1856
|
+
/**
|
|
1857
|
+
* Selects a slice of the state.
|
|
1858
|
+
*/
|
|
1859
|
+
select(selector) {
|
|
1860
|
+
return selector(this._state);
|
|
1861
|
+
}
|
|
1862
|
+
/**
|
|
1863
|
+
* Starts the actor.
|
|
1864
|
+
*/
|
|
1865
|
+
start() {
|
|
1866
|
+
return this;
|
|
1867
|
+
}
|
|
1868
|
+
/**
|
|
1869
|
+
* Stops the actor.
|
|
1870
|
+
*/
|
|
1871
|
+
stop() {
|
|
1872
|
+
this._observers.clear();
|
|
1873
|
+
}
|
|
1874
|
+
/**
|
|
1875
|
+
* Dispatches an event to the actor.
|
|
1876
|
+
* Handles both sync and async transitions.
|
|
1877
|
+
*/
|
|
1878
|
+
dispatch(event) {
|
|
1879
|
+
if (_Actor._inspector) {
|
|
1880
|
+
_Actor._inspector({
|
|
1881
|
+
type: "@actor/send",
|
|
1882
|
+
actor: this,
|
|
1883
|
+
event,
|
|
1884
|
+
snapshot: this._state
|
|
1885
|
+
});
|
|
1886
|
+
}
|
|
1887
|
+
if (this._processing) {
|
|
1888
|
+
this._queue.push(event);
|
|
1889
|
+
return;
|
|
1890
|
+
}
|
|
1891
|
+
this._processing = true;
|
|
1892
|
+
this._queue.push(event);
|
|
1893
|
+
this._flush();
|
|
1894
|
+
}
|
|
1895
|
+
_flush() {
|
|
1896
|
+
while (this._queue.length > 0) {
|
|
1897
|
+
const event = this._queue[0];
|
|
1898
|
+
this._queue.shift();
|
|
1899
|
+
const transitions = this._state;
|
|
1900
|
+
const fn = transitions[event.type];
|
|
1901
|
+
if (typeof fn !== "function") {
|
|
1902
|
+
console.warn(`[Actor] Transition '${String(event.type)}' not found.`);
|
|
1903
|
+
continue;
|
|
1904
|
+
}
|
|
1905
|
+
let result;
|
|
1906
|
+
try {
|
|
1907
|
+
result = fn.apply(this._state.context, event.args);
|
|
1908
|
+
} catch (error) {
|
|
1909
|
+
console.error(`[Actor] Error in transition '${String(event.type)}':`, error);
|
|
1910
|
+
continue;
|
|
1911
|
+
}
|
|
1912
|
+
if (result instanceof Promise) {
|
|
1913
|
+
result.then((nextState) => {
|
|
1914
|
+
this._state = nextState;
|
|
1915
|
+
this._notify();
|
|
1916
|
+
this._flush();
|
|
1917
|
+
}).catch((error) => {
|
|
1918
|
+
console.error(`[Actor] Async error in transition '${String(event.type)}':`, error);
|
|
1919
|
+
this._flush();
|
|
1920
|
+
});
|
|
1921
|
+
return;
|
|
1922
|
+
} else {
|
|
1923
|
+
this._state = result;
|
|
1924
|
+
this._notify();
|
|
1925
|
+
}
|
|
1926
|
+
}
|
|
1927
|
+
this._processing = false;
|
|
1928
|
+
}
|
|
1929
|
+
_notify() {
|
|
1930
|
+
const snapshot = this.getSnapshot();
|
|
1931
|
+
this._observers.forEach((obs) => obs(snapshot));
|
|
1932
|
+
}
|
|
1933
|
+
};
|
|
1934
|
+
// Global inspector
|
|
1935
|
+
_Actor._inspector = null;
|
|
1936
|
+
var Actor = _Actor;
|
|
1937
|
+
function createActor(machine) {
|
|
1938
|
+
return new Actor(machine);
|
|
1939
|
+
}
|
|
1940
|
+
function spawn(machine) {
|
|
1941
|
+
return createActor(machine);
|
|
1942
|
+
}
|
|
1943
|
+
function fromPromise(promiseFn) {
|
|
1944
|
+
const initial = { status: "pending", data: void 0, error: void 0 };
|
|
1945
|
+
const machine = createMachine(
|
|
1946
|
+
initial,
|
|
1947
|
+
(next2) => ({
|
|
1948
|
+
resolve(data) {
|
|
1949
|
+
return next2({ status: "resolved", data, error: void 0 });
|
|
1950
|
+
},
|
|
1951
|
+
reject(error) {
|
|
1952
|
+
return next2({ status: "rejected", error, data: void 0 });
|
|
1953
|
+
}
|
|
1954
|
+
})
|
|
1955
|
+
);
|
|
1956
|
+
const actor = createActor(machine);
|
|
1957
|
+
promiseFn().then((data) => actor.send.resolve(data)).catch((err) => actor.send.reject(err));
|
|
1958
|
+
return actor;
|
|
1959
|
+
}
|
|
1960
|
+
function fromObservable(observable) {
|
|
1961
|
+
const initial = { status: "active", value: void 0, error: void 0 };
|
|
1962
|
+
const machine = createMachine(
|
|
1963
|
+
initial,
|
|
1964
|
+
(next2) => ({
|
|
1965
|
+
next(value) {
|
|
1966
|
+
return next2({ status: "active", value, error: void 0 });
|
|
1967
|
+
},
|
|
1968
|
+
error(error) {
|
|
1969
|
+
return next2({ status: "error", error, value: void 0 });
|
|
1970
|
+
},
|
|
1971
|
+
complete() {
|
|
1972
|
+
return next2({ status: "done", value: void 0, error: void 0 });
|
|
1973
|
+
}
|
|
1974
|
+
})
|
|
1975
|
+
);
|
|
1976
|
+
const actor = createActor(machine);
|
|
1977
|
+
observable.subscribe(
|
|
1978
|
+
(val) => actor.send.next(val),
|
|
1979
|
+
(err) => actor.send.error(err),
|
|
1980
|
+
() => actor.send.complete()
|
|
1981
|
+
);
|
|
1982
|
+
return actor;
|
|
1983
|
+
}
|
|
1984
|
+
|
|
1810
1985
|
// src/index.ts
|
|
1811
1986
|
function createMachine(context, fnsOrFactory) {
|
|
1812
1987
|
if (typeof fnsOrFactory === "function") {
|