1
- import { createMachine , interpret } from 'xstate'
1
+ import { Interpreter , MachineConfig , StateMachine , StateValue , State as XState , createMachine , interpret } from 'xstate'
2
2
3
3
export default {
4
4
fetch : ( req , env ) => {
@@ -9,7 +9,7 @@ export default {
9
9
'Access-Control-Allow-Credentials' : 'true' ,
10
10
'Access-Control-Allow-Methods' : 'GET, POST, PUT, DELETE, OPTIONS' ,
11
11
'Access-Control-Allow-Headers' : 'Content-Type, Authorization, Cookie, X-Forwarded-Proto, X-Forwarded-For' ,
12
- 'Access-Control-Max-Age' : 86400 ,
12
+ 'Access-Control-Max-Age' : ' 86400' ,
13
13
} ,
14
14
} )
15
15
}
@@ -22,6 +22,19 @@ export default {
22
22
}
23
23
24
24
export class State {
25
+ state
26
+ env
27
+ /** @type {MachineConfig } */
28
+ machineDefinition
29
+ /** @type {StateValue } */
30
+ machineState
31
+ /** @type {StateMachine } */
32
+ machine
33
+ /** @type {Interpreter } */
34
+ service
35
+ /** @type {XState } */
36
+ serviceState
37
+
25
38
constructor ( state , env ) {
26
39
this . state = state
27
40
this . env = env
@@ -33,6 +46,9 @@ export class State {
33
46
} )
34
47
}
35
48
49
+ /**
50
+ * @param {StateValue|undefined } state
51
+ */
36
52
startMachine ( state ) {
37
53
this . machine = createMachine ( this . machineDefinition )
38
54
this . service = interpret ( this . machine )
@@ -80,6 +96,9 @@ export class State {
80
96
if ( this . machineDefinition ) this . startMachine ( )
81
97
}
82
98
99
+ /**
100
+ * @param {MachineConfig } machineDefinition
101
+ */
83
102
async update ( machineDefinition ) {
84
103
// Don't update if the new definition is empty or hasn't changed
85
104
if ( ! machineDefinition || machineDefinition === this . machineDefinition ) return
@@ -88,6 +107,9 @@ export class State {
88
107
this . startMachine ( this . machineState )
89
108
}
90
109
110
+ /**
111
+ * @param {Request } req
112
+ */
91
113
async fetch ( req ) {
92
114
let { user, redirect, method, origin, pathSegments, search, json } = await this . env . CTX . fetch ( req ) . then ( ( res ) => res . json ( ) )
93
115
if ( redirect ) return Response . redirect ( redirect )
0 commit comments