@typeonce/effect-machine 0.38.1 → 0.40.0
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 +52 -41
- package/dist/Machine.d.ts +62 -88
- package/dist/Machine.d.ts.map +1 -1
- package/dist/Machine.js +11 -24
- package/dist/Machine.js.map +1 -1
- package/dist/internal/machine/atom.d.ts +1 -1
- package/dist/internal/machine/atom.d.ts.map +1 -1
- package/dist/internal/machine/atom.js +1 -1
- package/dist/internal/machine/atom.js.map +1 -1
- package/dist/internal/machine/cluster.d.ts.map +1 -1
- package/dist/internal/machine/cluster.js +2 -2
- package/dist/internal/machine/cluster.js.map +1 -1
- package/dist/internal/machine/clusterProtocol.d.ts +1 -1
- package/dist/internal/machine/clusterProtocol.d.ts.map +1 -1
- package/dist/internal/machine/clusterProtocol.js +0 -1
- package/dist/internal/machine/clusterProtocol.js.map +1 -1
- package/dist/internal/machine/declaration.d.ts +10 -3
- package/dist/internal/machine/declaration.d.ts.map +1 -1
- package/dist/internal/machine/declaration.js +47 -16
- package/dist/internal/machine/declaration.js.map +1 -1
- package/dist/internal/machine/errors.d.ts +15 -1
- package/dist/internal/machine/errors.d.ts.map +1 -1
- package/dist/internal/machine/errors.js +10 -0
- package/dist/internal/machine/errors.js.map +1 -1
- package/dist/internal/machine/initialDeclaration.d.ts +2 -2
- package/dist/internal/machine/initialDeclaration.d.ts.map +1 -1
- package/dist/internal/machine/initialDeclaration.js +8 -9
- package/dist/internal/machine/initialDeclaration.js.map +1 -1
- package/dist/internal/machine/machine.d.ts +26 -76
- package/dist/internal/machine/machine.d.ts.map +1 -1
- package/dist/internal/machine/machine.js +21 -16
- package/dist/internal/machine/machine.js.map +1 -1
- package/dist/internal/machine/planner.d.ts +216 -31
- package/dist/internal/machine/planner.d.ts.map +1 -1
- package/dist/internal/machine/planner.js +6 -12
- package/dist/internal/machine/planner.js.map +1 -1
- package/dist/internal/machine/process.d.ts +5 -9
- package/dist/internal/machine/process.d.ts.map +1 -1
- package/dist/internal/machine/process.js +19 -32
- package/dist/internal/machine/process.js.map +1 -1
- package/dist/internal/testing/machine/arbitrary.d.ts +1 -1
- package/dist/internal/testing/machine/arbitrary.d.ts.map +1 -1
- package/dist/internal/testing/machine/arbitrary.js +1 -1
- package/dist/internal/testing/machine/arbitrary.js.map +1 -1
- package/dist/internal/testing/machine/finiteModel.d.ts +1 -1
- package/dist/internal/testing/machine/finiteModel.d.ts.map +1 -1
- package/dist/internal/testing/machine/finiteModel.js +10 -13
- package/dist/internal/testing/machine/finiteModel.js.map +1 -1
- package/dist/internal/testing/machine/runtime.d.ts +1 -1
- package/dist/internal/testing/machine/runtime.d.ts.map +1 -1
- package/dist/internal/testing/machine/runtime.js +1 -1
- package/dist/internal/testing/machine/runtime.js.map +1 -1
- package/dist/internal/testing/machine/verification.d.ts.map +1 -1
- package/dist/internal/testing/machine/verification.js +1 -1
- package/dist/internal/testing/machine/verification.js.map +1 -1
- package/dist/testing/MachineTest.d.ts +8 -8
- package/dist/testing/MachineTest.d.ts.map +1 -1
- package/dist/testing/MachineTest.js +7 -7
- package/dist/unstable/cluster/ClusterMachine.d.ts +3 -3
- package/dist/unstable/cluster/ClusterMachine.d.ts.map +1 -1
- package/dist/unstable/cluster/ClusterMachine.js +1 -1
- package/dist/unstable/reactivity/AtomMachine.d.ts +4 -4
- package/dist/unstable/reactivity/AtomMachine.d.ts.map +1 -1
- package/dist/unstable/reactivity/AtomMachine.js +3 -3
- package/docs/agent-guide.md +23 -30
- package/docs/effect-atom-react.md +2 -2
- package/docs/machine-review.md +5 -6
- package/docs/root-api.md +27 -19
- package/package.json +4 -4
- package/src/Machine.ts +125 -163
- package/src/internal/machine/atom.ts +5 -8
- package/src/internal/machine/cluster.ts +3 -3
- package/src/internal/machine/clusterProtocol.ts +1 -1
- package/src/internal/machine/declaration.ts +56 -17
- package/src/internal/machine/errors.ts +22 -1
- package/src/internal/machine/initialDeclaration.ts +10 -12
- package/src/internal/machine/machine.ts +85 -465
- package/src/internal/machine/planner.ts +84 -190
- package/src/internal/machine/process.ts +80 -279
- package/src/internal/testing/machine/arbitrary.ts +1 -1
- package/src/internal/testing/machine/finiteModel.ts +8 -12
- package/src/internal/testing/machine/runtime.ts +1 -1
- package/src/internal/testing/machine/verification.ts +1 -1
- package/src/testing/MachineTest.ts +8 -8
- package/src/unstable/cluster/ClusterMachine.ts +3 -3
- package/src/unstable/reactivity/AtomMachine.ts +4 -4
- package/dist/internal/machine/targetReference.d.ts +0 -15
- package/dist/internal/machine/targetReference.d.ts.map +0 -1
- package/dist/internal/machine/targetReference.js +0 -41
- package/dist/internal/machine/targetReference.js.map +0 -1
- package/src/internal/machine/targetReference.ts +0 -55
|
@@ -4,9 +4,7 @@
|
|
|
4
4
|
* @since 0.4.0
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
import * as Cause from "effect/Cause"
|
|
8
7
|
import * as Effect from "effect/Effect"
|
|
9
|
-
import type * as Schema from "effect/Schema"
|
|
10
8
|
import type { Enqueue, InitialEvent as MachineInitialEvent, Machine, MachineTarget } from "../../Machine.js"
|
|
11
9
|
import { makeCollector, type RuntimeCommand } from "./command.js"
|
|
12
10
|
import {
|
|
@@ -38,7 +36,9 @@ import {
|
|
|
38
36
|
snapshotFromConfigurationAtPath
|
|
39
37
|
} from "./configuration.js"
|
|
40
38
|
import * as Construction from "./construction.js"
|
|
41
|
-
import { InfiniteTransitionError, MachineSchemaDecodeError,
|
|
39
|
+
import { failPlanning, InfiniteTransitionError, MachineSchemaDecodeError, toStartupFailure } from "./errors.js"
|
|
40
|
+
import { StoppedError } from "./errors.js"
|
|
41
|
+
import type { PlanningError, StartupError } from "./errors.js"
|
|
42
42
|
import { type CapturedStateConfig, toImpl } from "./implementation.js"
|
|
43
43
|
import { isDataInitializer } from "./initialDeclaration.js"
|
|
44
44
|
import { getStateInitializeValues, makeStateInitializeBuilder } from "./initialization.js"
|
|
@@ -770,7 +770,7 @@ const makeTransitionContext = <
|
|
|
770
770
|
StateId extends Machine.StateIdentifier<States>,
|
|
771
771
|
EventTag extends Machine.TagOf<Events[number]>
|
|
772
772
|
>(
|
|
773
|
-
machine: Machine
|
|
773
|
+
machine: Machine.Any,
|
|
774
774
|
configuration: ActiveConfiguration,
|
|
775
775
|
path: string,
|
|
776
776
|
event: Machine.EventByTag<Events, EventTag>,
|
|
@@ -1728,58 +1728,20 @@ export const getFinalOutputEffect = <
|
|
|
1728
1728
|
})
|
|
1729
1729
|
)
|
|
1730
1730
|
|
|
1731
|
-
export const isFinal =
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
InitialE = never,
|
|
1740
|
-
InitialR = never,
|
|
1741
|
-
FinalStates extends Machine.StateIdentifier<States> = never,
|
|
1742
|
-
Output = never,
|
|
1743
|
-
OutputStates extends Machine.StateIdentifier<States> = never
|
|
1744
|
-
>(
|
|
1745
|
-
machine: Machine<
|
|
1746
|
-
States,
|
|
1747
|
-
Events,
|
|
1748
|
-
Input,
|
|
1749
|
-
UnhandledStates,
|
|
1750
|
-
E,
|
|
1751
|
-
R,
|
|
1752
|
-
InitialE,
|
|
1753
|
-
InitialR,
|
|
1754
|
-
FinalStates,
|
|
1755
|
-
Output,
|
|
1756
|
-
Emits,
|
|
1757
|
-
OutputStates
|
|
1758
|
-
>,
|
|
1759
|
-
state: Machine.Snapshot<States>
|
|
1760
|
-
): state is Machine.SnapshotContainingFinal<States, FinalStates> => isFinalState(machine, state)
|
|
1761
|
-
|
|
1762
|
-
export const planInitialSync = <
|
|
1763
|
-
const States extends Machine.StateSchemas,
|
|
1764
|
-
const Events extends ReadonlyArray<Machine.TaggedSchema>,
|
|
1765
|
-
const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [],
|
|
1766
|
-
const Input extends Schema.Top = typeof Schema.Void,
|
|
1767
|
-
UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>,
|
|
1768
|
-
E = never,
|
|
1769
|
-
R = never,
|
|
1770
|
-
InitialE = never,
|
|
1771
|
-
InitialR = never,
|
|
1772
|
-
FinalStates extends Machine.StateIdentifier<States> = never,
|
|
1773
|
-
Output = never
|
|
1774
|
-
>(
|
|
1775
|
-
machine: Machine<States, Events, Input, UnhandledStates, E, R, InitialE, InitialR, FinalStates, Output, Emits>,
|
|
1776
|
-
...args: [...Machine.InputArgs<Input>]
|
|
1731
|
+
export const isFinal = (
|
|
1732
|
+
machine: Machine.Any,
|
|
1733
|
+
state: Machine.Snapshot<any>
|
|
1734
|
+
): state is Machine.SnapshotContainingFinal<any, any> => isFinalState(machine, state)
|
|
1735
|
+
|
|
1736
|
+
export const planInitialSync = (
|
|
1737
|
+
machine: Machine.Any,
|
|
1738
|
+
...args: ReadonlyArray<unknown>
|
|
1777
1739
|
) => {
|
|
1778
1740
|
const inputArgs = machine.input === undefined
|
|
1779
1741
|
? args
|
|
1780
1742
|
: args.length === 0
|
|
1781
1743
|
? (decodeInputSync(machine, machine.input, undefined), args)
|
|
1782
|
-
: [decodeInputSync(machine, machine.input, args[0])]
|
|
1744
|
+
: [decodeInputSync(machine, machine.input, args[0])]
|
|
1783
1745
|
const initial = machine.initial(...inputArgs)
|
|
1784
1746
|
const emptyConfiguration: ActiveConfiguration = {
|
|
1785
1747
|
active: new Set(),
|
|
@@ -1789,12 +1751,12 @@ export const planInitialSync = <
|
|
|
1789
1751
|
}
|
|
1790
1752
|
const rootResolution = resolveInitialTarget(machine, emptyConfiguration, initial, InitialEvent)
|
|
1791
1753
|
const configuration = normalizeTargetConfigurationSync(machine, emptyConfiguration, rootResolution.target)
|
|
1792
|
-
const startingState = snapshotFromConfiguration
|
|
1754
|
+
const startingState = snapshotFromConfiguration(machine, configuration)
|
|
1793
1755
|
const initialEntryPaths = getInitialEntryPaths(machine, configuration)
|
|
1794
1756
|
const commands = rootResolution.commands
|
|
1795
1757
|
const raisedEvents = rootResolution.raisedEvents
|
|
1796
1758
|
const emittedEvents = rootResolution.emittedEvents
|
|
1797
|
-
const entry = collectStateActions
|
|
1759
|
+
const entry = collectStateActions(
|
|
1798
1760
|
machine,
|
|
1799
1761
|
configuration,
|
|
1800
1762
|
initialEntryPaths,
|
|
@@ -1806,7 +1768,7 @@ export const planInitialSync = <
|
|
|
1806
1768
|
configuration,
|
|
1807
1769
|
InitialEvent,
|
|
1808
1770
|
[...entry.commands],
|
|
1809
|
-
[...raisedEvents, ...entry.raisedEvents]
|
|
1771
|
+
[...raisedEvents, ...entry.raisedEvents],
|
|
1810
1772
|
[...emittedEvents, ...entry.emittedEvents],
|
|
1811
1773
|
rootResolution.transitions.length === 0 ?
|
|
1812
1774
|
[] :
|
|
@@ -1817,7 +1779,7 @@ export const planInitialSync = <
|
|
|
1817
1779
|
commands,
|
|
1818
1780
|
raisedEvents: [
|
|
1819
1781
|
...raisedEvents
|
|
1820
|
-
]
|
|
1782
|
+
],
|
|
1821
1783
|
emittedEvents,
|
|
1822
1784
|
exitPaths: [],
|
|
1823
1785
|
entryPaths: [],
|
|
@@ -1829,14 +1791,14 @@ export const planInitialSync = <
|
|
|
1829
1791
|
const planned = {
|
|
1830
1792
|
startingState,
|
|
1831
1793
|
initialEntryPaths,
|
|
1832
|
-
state: snapshotFromConfiguration
|
|
1794
|
+
state: snapshotFromConfiguration(machine, settled.next),
|
|
1833
1795
|
commands: [
|
|
1834
1796
|
...commands,
|
|
1835
1797
|
...settled.commands
|
|
1836
1798
|
],
|
|
1837
|
-
emittedEvents: settled.emittedEvents
|
|
1799
|
+
emittedEvents: settled.emittedEvents,
|
|
1838
1800
|
microsteps: settled.microsteps.map((step) => ({
|
|
1839
|
-
next: snapshotFromConfiguration
|
|
1801
|
+
next: snapshotFromConfiguration(machine, step.next),
|
|
1840
1802
|
event: step.event,
|
|
1841
1803
|
transitions: step.transitions,
|
|
1842
1804
|
commands: step.commands,
|
|
@@ -1852,47 +1814,21 @@ export const planInitialSync = <
|
|
|
1852
1814
|
: { ...planned, done: false as const, output: undefined }
|
|
1853
1815
|
}
|
|
1854
1816
|
|
|
1855
|
-
export const enabled =
|
|
1856
|
-
|
|
1857
|
-
|
|
1858
|
-
|
|
1859
|
-
const Input extends Schema.Top = typeof Schema.Void,
|
|
1860
|
-
UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>,
|
|
1861
|
-
E = never,
|
|
1862
|
-
R = never,
|
|
1863
|
-
InitialE = never,
|
|
1864
|
-
InitialR = never,
|
|
1865
|
-
FinalStates extends Machine.StateIdentifier<States> = never,
|
|
1866
|
-
Output = never,
|
|
1867
|
-
OutputStates extends Machine.StateIdentifier<States> = never
|
|
1868
|
-
>(
|
|
1869
|
-
machine: Machine<
|
|
1870
|
-
States,
|
|
1871
|
-
Events,
|
|
1872
|
-
Input,
|
|
1873
|
-
UnhandledStates,
|
|
1874
|
-
E,
|
|
1875
|
-
R,
|
|
1876
|
-
InitialE,
|
|
1877
|
-
InitialR,
|
|
1878
|
-
FinalStates,
|
|
1879
|
-
Output,
|
|
1880
|
-
Emits,
|
|
1881
|
-
OutputStates
|
|
1882
|
-
>,
|
|
1883
|
-
state: Machine.Snapshot<States>
|
|
1884
|
-
): ReadonlyArray<Machine.TagOf<Events[number]>> => {
|
|
1817
|
+
export const enabled = (
|
|
1818
|
+
machine: Machine.Any,
|
|
1819
|
+
state: Machine.Snapshot<any>
|
|
1820
|
+
): ReadonlyArray<Machine.TagOf<any>> => {
|
|
1885
1821
|
if (isFinalState(machine, state)) {
|
|
1886
1822
|
return []
|
|
1887
1823
|
}
|
|
1888
1824
|
const configuration = normalizeConfiguration(machine, state)
|
|
1889
|
-
const tags: Array<Machine.TagOf<
|
|
1825
|
+
const tags: Array<Machine.TagOf<any>> = []
|
|
1890
1826
|
const seen = new Set<PropertyKey>()
|
|
1891
1827
|
for (const path of getCandidatePaths(machine, configuration)) {
|
|
1892
1828
|
for (const tag of Reflect.ownKeys(toImpl(machine).handlers[path]?.on ?? {})) {
|
|
1893
1829
|
if (!seen.has(tag)) {
|
|
1894
1830
|
seen.add(tag)
|
|
1895
|
-
tags.push(tag
|
|
1831
|
+
tags.push(tag)
|
|
1896
1832
|
}
|
|
1897
1833
|
}
|
|
1898
1834
|
}
|
|
@@ -1910,24 +1846,11 @@ const canSync = (
|
|
|
1910
1846
|
return selectEventTransitions(machine, configuration, decodedEvent as any).length > 0
|
|
1911
1847
|
}
|
|
1912
1848
|
|
|
1913
|
-
const microstep =
|
|
1914
|
-
|
|
1915
|
-
const Events extends ReadonlyArray<Machine.TaggedSchema>,
|
|
1916
|
-
const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [],
|
|
1917
|
-
const Input extends Schema.Top = typeof Schema.Void,
|
|
1918
|
-
UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>,
|
|
1919
|
-
E = never,
|
|
1920
|
-
R = never,
|
|
1921
|
-
InitialE = never,
|
|
1922
|
-
InitialR = never,
|
|
1923
|
-
FinalStates extends Machine.StateIdentifier<States> = never,
|
|
1924
|
-
Output = never,
|
|
1925
|
-
Context = never
|
|
1926
|
-
>(
|
|
1927
|
-
machine: Machine<States, Events, Input, UnhandledStates, E, R, InitialE, InitialR, FinalStates, Output, Emits>,
|
|
1849
|
+
const microstep = (
|
|
1850
|
+
machine: Machine.Any,
|
|
1928
1851
|
state: ActiveConfiguration,
|
|
1929
|
-
event: Machine.LifecycleEvent<
|
|
1930
|
-
selections: ReadonlyArray<SelectedTransition<
|
|
1852
|
+
event: Machine.LifecycleEvent<any>,
|
|
1853
|
+
selections: ReadonlyArray<SelectedTransition<any, any, any, any>>
|
|
1931
1854
|
) => {
|
|
1932
1855
|
if (selections.length === 0) {
|
|
1933
1856
|
return {
|
|
@@ -1945,10 +1868,10 @@ const microstep = <
|
|
|
1945
1868
|
}
|
|
1946
1869
|
|
|
1947
1870
|
const activeSelections = removePreemptedAncestorSelections(selections)
|
|
1948
|
-
const evaluatedTransitions: Array<EvaluatedTransition<
|
|
1871
|
+
const evaluatedTransitions: Array<EvaluatedTransition<any, Machine.EventOf<any>, any, any, any>> = []
|
|
1949
1872
|
for (const selection of activeSelections) {
|
|
1950
1873
|
evaluatedTransitions.push(
|
|
1951
|
-
collectEvaluatedTransition
|
|
1874
|
+
collectEvaluatedTransition(
|
|
1952
1875
|
machine,
|
|
1953
1876
|
state,
|
|
1954
1877
|
selection
|
|
@@ -1994,7 +1917,7 @@ const microstep = <
|
|
|
1994
1917
|
]
|
|
1995
1918
|
for (const transition of targetApplicationOrder) {
|
|
1996
1919
|
if (transition.target !== undefined) {
|
|
1997
|
-
stateAfterTransition = normalizeTargetConfigurationSync
|
|
1920
|
+
stateAfterTransition = normalizeTargetConfigurationSync(
|
|
1998
1921
|
machine,
|
|
1999
1922
|
stateAfterTransition,
|
|
2000
1923
|
transition.target
|
|
@@ -2029,14 +1952,14 @@ const microstep = <
|
|
|
2029
1952
|
const exitPaths = sortExitPaths(machine, sortedTransitions.flatMap((transition) => transition.exitPaths))
|
|
2030
1953
|
const entryPaths = sortEntryPaths(machine, sortedTransitions.flatMap((transition) => transition.entryPaths))
|
|
2031
1954
|
stateAfterTransition = captureHistory(machine, state, stateAfterTransition, exitPaths)
|
|
2032
|
-
const exit = collectStateActions
|
|
1955
|
+
const exit = collectStateActions(
|
|
2033
1956
|
machine,
|
|
2034
1957
|
state,
|
|
2035
1958
|
exitPaths,
|
|
2036
1959
|
event,
|
|
2037
1960
|
"exit"
|
|
2038
1961
|
)
|
|
2039
|
-
const entry = collectStateActions
|
|
1962
|
+
const entry = collectStateActions(
|
|
2040
1963
|
machine,
|
|
2041
1964
|
stateAfterTransition,
|
|
2042
1965
|
entryPaths,
|
|
@@ -2050,7 +1973,7 @@ const microstep = <
|
|
|
2050
1973
|
transitions: retainedTransitions,
|
|
2051
1974
|
commands: [...exit.commands, ...transitionActions, ...entry.commands],
|
|
2052
1975
|
raisedEvents: [...exit.raisedEvents, ...transitionRaisedEvents, ...entry.raisedEvents] as ReadonlyArray<
|
|
2053
|
-
Machine.EventOf<
|
|
1976
|
+
Machine.EventOf<any>
|
|
2054
1977
|
>,
|
|
2055
1978
|
emittedEvents: [...exit.emittedEvents, ...transitionEmittedEvents, ...entry.emittedEvents],
|
|
2056
1979
|
exitPaths,
|
|
@@ -2060,26 +1983,14 @@ const microstep = <
|
|
|
2060
1983
|
}
|
|
2061
1984
|
}
|
|
2062
1985
|
|
|
2063
|
-
const settle =
|
|
2064
|
-
|
|
2065
|
-
const Events extends ReadonlyArray<Machine.TaggedSchema>,
|
|
2066
|
-
const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [],
|
|
2067
|
-
const Input extends Schema.Top = typeof Schema.Void,
|
|
2068
|
-
UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>,
|
|
2069
|
-
E = never,
|
|
2070
|
-
R = never,
|
|
2071
|
-
InitialE = never,
|
|
2072
|
-
InitialR = never,
|
|
2073
|
-
FinalStates extends Machine.StateIdentifier<States> = never,
|
|
2074
|
-
Output = never
|
|
2075
|
-
>(
|
|
2076
|
-
machine: Machine<States, Events, Input, UnhandledStates, E, R, InitialE, InitialR, FinalStates, Output, Emits>,
|
|
1986
|
+
const settle = (
|
|
1987
|
+
machine: Machine.Any,
|
|
2077
1988
|
state: ActiveConfiguration,
|
|
2078
|
-
event: Machine.LifecycleEvent<
|
|
1989
|
+
event: Machine.LifecycleEvent<any>,
|
|
2079
1990
|
commands: Array<RuntimeCommand>,
|
|
2080
|
-
raisedEvents: Array<Machine.EventOf<
|
|
1991
|
+
raisedEvents: Array<Machine.EventOf<any>>,
|
|
2081
1992
|
emittedEvents: Array<unknown>,
|
|
2082
|
-
microsteps: Array<SettlingMicrostep<ActiveConfiguration, Machine.EventOf<
|
|
1993
|
+
microsteps: Array<SettlingMicrostep<ActiveConfiguration, Machine.EventOf<any>, any, any>>
|
|
2083
1994
|
) => {
|
|
2084
1995
|
let currentState = state
|
|
2085
1996
|
let currentEvent = event
|
|
@@ -2087,7 +1998,7 @@ const settle = <
|
|
|
2087
1998
|
let iterations = 0
|
|
2088
1999
|
let raisedEventIndex = 0
|
|
2089
2000
|
let completedTerminal = false
|
|
2090
|
-
let finalOutput:
|
|
2001
|
+
let finalOutput: any | undefined = undefined
|
|
2091
2002
|
const pendingCompletions: Array<{ readonly path: string; readonly output: unknown }> = []
|
|
2092
2003
|
|
|
2093
2004
|
while (true) {
|
|
@@ -2108,14 +2019,14 @@ const settle = <
|
|
|
2108
2019
|
while (pendingCompletions.length > 0 && !currentState.active.has(pendingCompletions[0]!.path)) {
|
|
2109
2020
|
pendingCompletions.shift()
|
|
2110
2021
|
}
|
|
2111
|
-
const done = selectDoneTransitions
|
|
2022
|
+
const done = selectDoneTransitions(
|
|
2112
2023
|
machine,
|
|
2113
2024
|
currentState,
|
|
2114
2025
|
currentEvent,
|
|
2115
2026
|
pendingCompletions.length === 0 ? [] : [pendingCompletions.shift()!]
|
|
2116
2027
|
)
|
|
2117
2028
|
if (done.length > 0) {
|
|
2118
|
-
const doneStep: SettlingMicrostep<ActiveConfiguration, Machine.EventOf<
|
|
2029
|
+
const doneStep: SettlingMicrostep<ActiveConfiguration, Machine.EventOf<any>, any, any> = microstep(
|
|
2119
2030
|
machine,
|
|
2120
2031
|
currentState,
|
|
2121
2032
|
currentEvent,
|
|
@@ -2135,15 +2046,15 @@ const settle = <
|
|
|
2135
2046
|
throw new Error("Machine reached a terminal configuration without a completed root output")
|
|
2136
2047
|
}
|
|
2137
2048
|
completedTerminal = true
|
|
2138
|
-
finalOutput = currentState.outputs.get(root) as
|
|
2049
|
+
finalOutput = currentState.outputs.get(root) as any
|
|
2139
2050
|
break
|
|
2140
2051
|
}
|
|
2141
2052
|
|
|
2142
2053
|
const always = shouldRunAlways
|
|
2143
|
-
? selectAlwaysTransitions
|
|
2054
|
+
? selectAlwaysTransitions(machine, currentState, currentEvent)
|
|
2144
2055
|
: []
|
|
2145
2056
|
if (always.length > 0) {
|
|
2146
|
-
const alwaysStep: SettlingMicrostep<ActiveConfiguration, Machine.EventOf<
|
|
2057
|
+
const alwaysStep: SettlingMicrostep<ActiveConfiguration, Machine.EventOf<any>, any, any> = microstep(
|
|
2147
2058
|
machine,
|
|
2148
2059
|
currentState,
|
|
2149
2060
|
currentEvent,
|
|
@@ -2168,10 +2079,10 @@ const settle = <
|
|
|
2168
2079
|
// internal queue, so decoding it again here only repeats schema work.
|
|
2169
2080
|
const raisedEvent = raisedEventValue
|
|
2170
2081
|
currentEvent = raisedEvent
|
|
2171
|
-
const raisedSelections = selectEventTransitions
|
|
2082
|
+
const raisedSelections = selectEventTransitions(
|
|
2172
2083
|
machine,
|
|
2173
2084
|
currentState,
|
|
2174
|
-
raisedEvent
|
|
2085
|
+
raisedEvent
|
|
2175
2086
|
)
|
|
2176
2087
|
if (raisedSelections.length === 0) {
|
|
2177
2088
|
shouldRunAlways = true
|
|
@@ -2198,28 +2109,16 @@ const settle = <
|
|
|
2198
2109
|
microsteps
|
|
2199
2110
|
}
|
|
2200
2111
|
return completedTerminal
|
|
2201
|
-
? { ...result, done: true as const, output: finalOutput as
|
|
2112
|
+
? { ...result, done: true as const, output: finalOutput as any }
|
|
2202
2113
|
: { ...result, done: false as const, output: undefined }
|
|
2203
2114
|
}
|
|
2204
2115
|
|
|
2205
|
-
const macrostepConfiguration =
|
|
2206
|
-
|
|
2207
|
-
const Events extends ReadonlyArray<Machine.TaggedSchema>,
|
|
2208
|
-
const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [],
|
|
2209
|
-
const Input extends Schema.Top = typeof Schema.Void,
|
|
2210
|
-
UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>,
|
|
2211
|
-
E = never,
|
|
2212
|
-
R = never,
|
|
2213
|
-
InitialE = never,
|
|
2214
|
-
InitialR = never,
|
|
2215
|
-
FinalStates extends Machine.StateIdentifier<States> = never,
|
|
2216
|
-
Output = never
|
|
2217
|
-
>(
|
|
2218
|
-
machine: Machine<States, Events, Input, UnhandledStates, E, R, InitialE, InitialR, FinalStates, Output, Emits>,
|
|
2116
|
+
const macrostepConfiguration = (
|
|
2117
|
+
machine: Machine.Any,
|
|
2219
2118
|
configuration: ActiveConfiguration,
|
|
2220
|
-
event: Machine.EventOf<
|
|
2119
|
+
event: Machine.EventOf<any> | InvocationEvent.InvocationEvent
|
|
2221
2120
|
) => {
|
|
2222
|
-
const decodedEvent = InvocationEvent.isInvocationEvent(event) ? event : decodeEventSync
|
|
2121
|
+
const decodedEvent = InvocationEvent.isInvocationEvent(event) ? event : decodeEventSync(machine, event)
|
|
2223
2122
|
if (isActiveFinalConfiguration(machine, configuration)) {
|
|
2224
2123
|
const completed = completeConfigurationSync(machine, configuration, decodedEvent as any)
|
|
2225
2124
|
const root = getRootPath(machine, completed.configuration)
|
|
@@ -2233,16 +2132,16 @@ const macrostepConfiguration = <
|
|
|
2233
2132
|
emittedEvents: [],
|
|
2234
2133
|
microsteps: [],
|
|
2235
2134
|
done: true as const,
|
|
2236
|
-
output: completed.configuration.outputs.get(root) as
|
|
2135
|
+
output: completed.configuration.outputs.get(root) as any
|
|
2237
2136
|
}
|
|
2238
2137
|
}
|
|
2239
2138
|
|
|
2240
2139
|
const selections = InvocationEvent.isInvocationEvent(decodedEvent)
|
|
2241
|
-
? selectInvocationTransition
|
|
2242
|
-
: selectEventTransitions
|
|
2140
|
+
? selectInvocationTransition(machine, configuration, decodedEvent)
|
|
2141
|
+
: selectEventTransitions(
|
|
2243
2142
|
machine,
|
|
2244
2143
|
configuration,
|
|
2245
|
-
decodedEvent
|
|
2144
|
+
decodedEvent
|
|
2246
2145
|
)
|
|
2247
2146
|
if (selections.length === 0) {
|
|
2248
2147
|
return {
|
|
@@ -2303,40 +2202,26 @@ const snapshotMacrostep = <
|
|
|
2303
2202
|
: { ...planned, done: false, output: undefined }
|
|
2304
2203
|
}
|
|
2305
2204
|
|
|
2306
|
-
const macrostep =
|
|
2307
|
-
|
|
2308
|
-
|
|
2309
|
-
|
|
2310
|
-
const Input extends Schema.Top = typeof Schema.Void,
|
|
2311
|
-
UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>,
|
|
2312
|
-
E = never,
|
|
2313
|
-
R = never,
|
|
2314
|
-
InitialE = never,
|
|
2315
|
-
InitialR = never,
|
|
2316
|
-
FinalStates extends Machine.StateIdentifier<States> = never,
|
|
2317
|
-
Output = never
|
|
2318
|
-
>(
|
|
2319
|
-
machine: Machine<States, Events, Input, UnhandledStates, E, R, InitialE, InitialR, FinalStates, Output, Emits>,
|
|
2320
|
-
state: Machine.Snapshot<States>,
|
|
2321
|
-
event: Machine.EventOf<Events>
|
|
2205
|
+
const macrostep = (
|
|
2206
|
+
machine: Machine.Any,
|
|
2207
|
+
state: Machine.Snapshot<any>,
|
|
2208
|
+
event: Machine.EventOf<any>
|
|
2322
2209
|
) => {
|
|
2323
|
-
const configuration = normalizeConfigurationSync
|
|
2210
|
+
const configuration = normalizeConfigurationSync(machine, state)
|
|
2324
2211
|
const settled = macrostepConfiguration(machine, configuration, event)
|
|
2325
|
-
return snapshotMacrostep
|
|
2212
|
+
return snapshotMacrostep(machine, settled)
|
|
2326
2213
|
}
|
|
2327
2214
|
|
|
2328
2215
|
export const planSync = macrostep
|
|
2329
2216
|
|
|
2330
2217
|
export const planConfiguration = macrostepConfiguration
|
|
2331
2218
|
|
|
2332
|
-
const planningEffect = <A>(thunk: () => A): Effect.Effect<A,
|
|
2219
|
+
const planningEffect = <A>(thunk: () => A): Effect.Effect<A, PlanningError> =>
|
|
2333
2220
|
Effect.suspend(() => {
|
|
2334
2221
|
try {
|
|
2335
2222
|
return Effect.succeed(thunk())
|
|
2336
2223
|
} catch (error) {
|
|
2337
|
-
return error
|
|
2338
|
-
? Effect.fail(error)
|
|
2339
|
-
: Effect.die(error)
|
|
2224
|
+
return failPlanning(error)
|
|
2340
2225
|
}
|
|
2341
2226
|
})
|
|
2342
2227
|
|
|
@@ -2349,25 +2234,34 @@ const schemaEffect = <A>(thunk: () => A): Effect.Effect<A, MachineSchemaDecodeEr
|
|
|
2349
2234
|
}
|
|
2350
2235
|
})
|
|
2351
2236
|
|
|
2352
|
-
export
|
|
2237
|
+
export function can(
|
|
2238
|
+
machine: Machine.Any
|
|
2239
|
+
): (state: Machine.Snapshot<any>, event: unknown) => Effect.Effect<boolean, MachineSchemaDecodeError>
|
|
2240
|
+
export function can(
|
|
2241
|
+
machine: Machine.Any,
|
|
2242
|
+
state: Machine.Snapshot<any>,
|
|
2243
|
+
event: unknown
|
|
2244
|
+
): Effect.Effect<boolean, MachineSchemaDecodeError>
|
|
2245
|
+
export function can(
|
|
2246
|
+
...args: readonly [Machine.Any] | readonly [Machine.Any, Machine.Snapshot<any>, unknown]
|
|
2247
|
+
):
|
|
2248
|
+
| ((state: Machine.Snapshot<any>, event: unknown) => Effect.Effect<boolean, MachineSchemaDecodeError>)
|
|
2249
|
+
| Effect.Effect<boolean, MachineSchemaDecodeError>
|
|
2250
|
+
{
|
|
2353
2251
|
const query = (state: Machine.Snapshot<any>, event: unknown) => schemaEffect(() => canSync(args[0], state, event))
|
|
2354
2252
|
return args.length === 1 ? query : query(args[1], args[2])
|
|
2355
2253
|
}
|
|
2356
2254
|
|
|
2357
2255
|
export const plan = (machine: Machine.Any, state: Machine.Snapshot<any>, event: unknown) =>
|
|
2358
|
-
planningEffect(() => planSync(machine
|
|
2256
|
+
planningEffect(() => planSync(machine, state, event))
|
|
2359
2257
|
|
|
2360
2258
|
export const planInitial = (
|
|
2361
2259
|
machine: Machine.Any,
|
|
2362
2260
|
...args: ReadonlyArray<unknown>
|
|
2363
|
-
): Effect.Effect<
|
|
2261
|
+
): Effect.Effect<ReturnType<typeof planInitialSync>, PlanningError | StartupError> =>
|
|
2364
2262
|
Effect.try({
|
|
2365
|
-
try: () =>
|
|
2366
|
-
catch:
|
|
2367
|
-
return error instanceof InfiniteTransitionError || error instanceof MachineSchemaDecodeError
|
|
2368
|
-
? error
|
|
2369
|
-
: new StartupError({ cause: Cause.die(error) })
|
|
2370
|
-
}
|
|
2263
|
+
try: () => planInitialSync(machine, ...args),
|
|
2264
|
+
catch: toStartupFailure
|
|
2371
2265
|
})
|
|
2372
2266
|
|
|
2373
2267
|
// Captured constructors are private planner inputs. Public callbacks receive root data
|