@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
|
@@ -20,10 +20,13 @@ 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,
|
|
26
27
|
MachineBase: () => MachineBase,
|
|
28
|
+
MachineExclude: () => MachineExclude,
|
|
29
|
+
MachineUnion: () => MachineUnion,
|
|
27
30
|
MiddlewareBuilder: () => MiddlewareBuilder,
|
|
28
31
|
MultiMachineBase: () => MultiMachineBase,
|
|
29
32
|
action: () => action,
|
|
@@ -36,6 +39,7 @@ __export(src_exports, {
|
|
|
36
39
|
combineFactories: () => combineFactories,
|
|
37
40
|
compose: () => compose,
|
|
38
41
|
composeTyped: () => composeTyped,
|
|
42
|
+
createActor: () => createActor,
|
|
39
43
|
createAsyncMachine: () => createAsyncMachine,
|
|
40
44
|
createContext: () => createContext,
|
|
41
45
|
createCustomMiddleware: () => createCustomMiddleware,
|
|
@@ -66,6 +70,8 @@ __export(src_exports, {
|
|
|
66
70
|
discriminantCase: () => discriminantCase,
|
|
67
71
|
extendTransitions: () => extendTransitions,
|
|
68
72
|
forContext: () => forContext,
|
|
73
|
+
fromObservable: () => fromObservable,
|
|
74
|
+
fromPromise: () => fromPromise,
|
|
69
75
|
guard: () => guard,
|
|
70
76
|
guardAsync: () => guardAsync,
|
|
71
77
|
guarded: () => guarded,
|
|
@@ -83,6 +89,8 @@ __export(src_exports, {
|
|
|
83
89
|
isPipelineConfig: () => isPipelineConfig,
|
|
84
90
|
isState: () => isState,
|
|
85
91
|
logState: () => logState,
|
|
92
|
+
machineExclude: () => machineExclude,
|
|
93
|
+
machineUnion: () => machineUnion,
|
|
86
94
|
matchMachine: () => matchMachine,
|
|
87
95
|
mergeContext: () => mergeContext,
|
|
88
96
|
metadata: () => metadata,
|
|
@@ -98,6 +106,7 @@ __export(src_exports, {
|
|
|
98
106
|
runWithEnsemble: () => runWithEnsemble,
|
|
99
107
|
runWithRunner: () => runWithRunner,
|
|
100
108
|
setContext: () => setContext,
|
|
109
|
+
spawn: () => spawn,
|
|
101
110
|
state: () => state,
|
|
102
111
|
step: () => step,
|
|
103
112
|
stepAsync: () => stepAsync,
|
|
@@ -1558,6 +1567,104 @@ function withDebugging(machine) {
|
|
|
1558
1567
|
return withTimeTravel(withSnapshot(withHistory(machine)));
|
|
1559
1568
|
}
|
|
1560
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
|
+
|
|
1561
1668
|
// src/utils.ts
|
|
1562
1669
|
function isState(machine, machineClass) {
|
|
1563
1670
|
return machine instanceof machineClass;
|
|
@@ -1807,6 +1914,176 @@ function forContext() {
|
|
|
1807
1914
|
};
|
|
1808
1915
|
}
|
|
1809
1916
|
|
|
1917
|
+
// src/actor.ts
|
|
1918
|
+
var _Actor = class _Actor {
|
|
1919
|
+
constructor(initialMachine) {
|
|
1920
|
+
this._observers = /* @__PURE__ */ new Set();
|
|
1921
|
+
this._queue = [];
|
|
1922
|
+
this._processing = false;
|
|
1923
|
+
this._state = initialMachine;
|
|
1924
|
+
this.ref = {
|
|
1925
|
+
send: (event) => this.dispatch(event)
|
|
1926
|
+
};
|
|
1927
|
+
this.send = new Proxy({}, {
|
|
1928
|
+
get: (_target, prop) => {
|
|
1929
|
+
return (...args) => {
|
|
1930
|
+
this.dispatch({ type: prop, args });
|
|
1931
|
+
};
|
|
1932
|
+
}
|
|
1933
|
+
});
|
|
1934
|
+
}
|
|
1935
|
+
/**
|
|
1936
|
+
* Registers a global inspector.
|
|
1937
|
+
*/
|
|
1938
|
+
static inspect(inspector) {
|
|
1939
|
+
_Actor._inspector = inspector;
|
|
1940
|
+
}
|
|
1941
|
+
/**
|
|
1942
|
+
* Returns the current immutable snapshot of the machine.
|
|
1943
|
+
*/
|
|
1944
|
+
getSnapshot() {
|
|
1945
|
+
return this._state;
|
|
1946
|
+
}
|
|
1947
|
+
/**
|
|
1948
|
+
* Subscribes to state changes.
|
|
1949
|
+
* @param observer Callback function to be invoked on every state change.
|
|
1950
|
+
* @returns Unsubscribe function.
|
|
1951
|
+
*/
|
|
1952
|
+
subscribe(observer) {
|
|
1953
|
+
this._observers.add(observer);
|
|
1954
|
+
return () => {
|
|
1955
|
+
this._observers.delete(observer);
|
|
1956
|
+
};
|
|
1957
|
+
}
|
|
1958
|
+
/**
|
|
1959
|
+
* Selects a slice of the state.
|
|
1960
|
+
*/
|
|
1961
|
+
select(selector) {
|
|
1962
|
+
return selector(this._state);
|
|
1963
|
+
}
|
|
1964
|
+
/**
|
|
1965
|
+
* Starts the actor.
|
|
1966
|
+
*/
|
|
1967
|
+
start() {
|
|
1968
|
+
return this;
|
|
1969
|
+
}
|
|
1970
|
+
/**
|
|
1971
|
+
* Stops the actor.
|
|
1972
|
+
*/
|
|
1973
|
+
stop() {
|
|
1974
|
+
this._observers.clear();
|
|
1975
|
+
}
|
|
1976
|
+
/**
|
|
1977
|
+
* Dispatches an event to the actor.
|
|
1978
|
+
* Handles both sync and async transitions.
|
|
1979
|
+
*/
|
|
1980
|
+
dispatch(event) {
|
|
1981
|
+
if (_Actor._inspector) {
|
|
1982
|
+
_Actor._inspector({
|
|
1983
|
+
type: "@actor/send",
|
|
1984
|
+
actor: this,
|
|
1985
|
+
event,
|
|
1986
|
+
snapshot: this._state
|
|
1987
|
+
});
|
|
1988
|
+
}
|
|
1989
|
+
if (this._processing) {
|
|
1990
|
+
this._queue.push(event);
|
|
1991
|
+
return;
|
|
1992
|
+
}
|
|
1993
|
+
this._processing = true;
|
|
1994
|
+
this._queue.push(event);
|
|
1995
|
+
this._flush();
|
|
1996
|
+
}
|
|
1997
|
+
_flush() {
|
|
1998
|
+
while (this._queue.length > 0) {
|
|
1999
|
+
const event = this._queue[0];
|
|
2000
|
+
this._queue.shift();
|
|
2001
|
+
const transitions = this._state;
|
|
2002
|
+
const fn = transitions[event.type];
|
|
2003
|
+
if (typeof fn !== "function") {
|
|
2004
|
+
console.warn(`[Actor] Transition '${String(event.type)}' not found.`);
|
|
2005
|
+
continue;
|
|
2006
|
+
}
|
|
2007
|
+
let result;
|
|
2008
|
+
try {
|
|
2009
|
+
result = fn.apply(this._state.context, event.args);
|
|
2010
|
+
} catch (error) {
|
|
2011
|
+
console.error(`[Actor] Error in transition '${String(event.type)}':`, error);
|
|
2012
|
+
continue;
|
|
2013
|
+
}
|
|
2014
|
+
if (result instanceof Promise) {
|
|
2015
|
+
result.then((nextState) => {
|
|
2016
|
+
this._state = nextState;
|
|
2017
|
+
this._notify();
|
|
2018
|
+
this._flush();
|
|
2019
|
+
}).catch((error) => {
|
|
2020
|
+
console.error(`[Actor] Async error in transition '${String(event.type)}':`, error);
|
|
2021
|
+
this._flush();
|
|
2022
|
+
});
|
|
2023
|
+
return;
|
|
2024
|
+
} else {
|
|
2025
|
+
this._state = result;
|
|
2026
|
+
this._notify();
|
|
2027
|
+
}
|
|
2028
|
+
}
|
|
2029
|
+
this._processing = false;
|
|
2030
|
+
}
|
|
2031
|
+
_notify() {
|
|
2032
|
+
const snapshot = this.getSnapshot();
|
|
2033
|
+
this._observers.forEach((obs) => obs(snapshot));
|
|
2034
|
+
}
|
|
2035
|
+
};
|
|
2036
|
+
// Global inspector
|
|
2037
|
+
_Actor._inspector = null;
|
|
2038
|
+
var Actor = _Actor;
|
|
2039
|
+
function createActor(machine) {
|
|
2040
|
+
return new Actor(machine);
|
|
2041
|
+
}
|
|
2042
|
+
function spawn(machine) {
|
|
2043
|
+
return createActor(machine);
|
|
2044
|
+
}
|
|
2045
|
+
function fromPromise(promiseFn) {
|
|
2046
|
+
const initial = { status: "pending", data: void 0, error: void 0 };
|
|
2047
|
+
const machine = createMachine(
|
|
2048
|
+
initial,
|
|
2049
|
+
(next2) => ({
|
|
2050
|
+
resolve(data) {
|
|
2051
|
+
return next2({ status: "resolved", data, error: void 0 });
|
|
2052
|
+
},
|
|
2053
|
+
reject(error) {
|
|
2054
|
+
return next2({ status: "rejected", error, data: void 0 });
|
|
2055
|
+
}
|
|
2056
|
+
})
|
|
2057
|
+
);
|
|
2058
|
+
const actor = createActor(machine);
|
|
2059
|
+
promiseFn().then((data) => actor.send.resolve(data)).catch((err) => actor.send.reject(err));
|
|
2060
|
+
return actor;
|
|
2061
|
+
}
|
|
2062
|
+
function fromObservable(observable) {
|
|
2063
|
+
const initial = { status: "active", value: void 0, error: void 0 };
|
|
2064
|
+
const machine = createMachine(
|
|
2065
|
+
initial,
|
|
2066
|
+
(next2) => ({
|
|
2067
|
+
next(value) {
|
|
2068
|
+
return next2({ status: "active", value, error: void 0 });
|
|
2069
|
+
},
|
|
2070
|
+
error(error) {
|
|
2071
|
+
return next2({ status: "error", error, value: void 0 });
|
|
2072
|
+
},
|
|
2073
|
+
complete() {
|
|
2074
|
+
return next2({ status: "done", value: void 0, error: void 0 });
|
|
2075
|
+
}
|
|
2076
|
+
})
|
|
2077
|
+
);
|
|
2078
|
+
const actor = createActor(machine);
|
|
2079
|
+
observable.subscribe(
|
|
2080
|
+
(val) => actor.send.next(val),
|
|
2081
|
+
(err) => actor.send.error(err),
|
|
2082
|
+
() => actor.send.complete()
|
|
2083
|
+
);
|
|
2084
|
+
return actor;
|
|
2085
|
+
}
|
|
2086
|
+
|
|
1810
2087
|
// src/index.ts
|
|
1811
2088
|
function createMachine(context, fnsOrFactory) {
|
|
1812
2089
|
if (typeof fnsOrFactory === "function") {
|