@@ -12,7 +12,7 @@ import type {
12
12
} from "@zag-js/core"
13
13
import { createScope , INIT_STATE , MachineStatus } from "@zag-js/core"
14
14
import { compact , isFunction , isString , toArray , warn , ensure } from "@zag-js/utils"
15
- import { type Accessor , createMemo , mergeProps , onCleanup , onMount } from "solid-js"
15
+ import { type Accessor , createMemo , mergeProps , onCleanup , onMount , untrack } from "solid-js"
16
16
import { createBindable } from "./bindable"
17
17
import { createRefs } from "./refs"
18
18
import { createTrack } from "./track"
@@ -239,39 +239,41 @@ export function useMachine<T extends MachineSchema>(
239
239
} )
240
240
241
241
const send = ( event : any ) => {
242
- if ( status !== MachineStatus . Started ) return
243
-
244
- previousEventRef . current = eventRef . current
245
- eventRef . current = event
246
-
247
- let currentState = state . get ( )
248
-
249
- const transitions =
250
- // @ts -ignore
251
- machine . states [ currentState ] . on ?. [ event . type ] ??
252
- // @ts -ignore
253
- machine . on ?. [ event . type ]
254
-
255
- const transition = choose ( transitions )
256
- if ( ! transition ) return
257
-
258
- // save current transition
259
- transitionRef . current = transition
260
- const target = transition . target ?? currentState
261
-
262
- debug ( "transition" , event . type , transition . target || currentState , `(${ transition . actions } )` )
263
-
264
- const changed = target !== currentState
265
- if ( changed ) {
266
- // state change is high priority
267
- state . set ( target )
268
- } else if ( transition . reenter && ! changed ) {
269
- // reenter will re-invoke the current state
270
- state . invoke ( currentState , currentState )
271
- } else {
272
- // call transition actions
273
- action ( transition . actions )
274
- }
242
+ queueMicrotask ( ( ) => {
243
+ if ( status !== MachineStatus . Started ) return
244
+
245
+ previousEventRef . current = eventRef . current
246
+ eventRef . current = event
247
+
248
+ let currentState = untrack ( ( ) => state . get ( ) )
249
+
250
+ const transitions =
251
+ // @ts -ignore
252
+ machine . states [ currentState ] . on ?. [ event . type ] ??
253
+ // @ts -ignore
254
+ machine . on ?. [ event . type ]
255
+
256
+ const transition = choose ( transitions )
257
+ if ( ! transition ) return
258
+
259
+ // save current transition
260
+ transitionRef . current = transition
261
+ const target = transition . target ?? currentState
262
+
263
+ debug ( "transition" , event . type , transition . target || currentState , `(${ transition . actions } )` )
264
+
265
+ const changed = target !== currentState
266
+ if ( changed ) {
267
+ // state change is high priority
268
+ state . set ( target )
269
+ } else if ( transition . reenter && ! changed ) {
270
+ // reenter will re-invoke the current state
271
+ state . invoke ( currentState , currentState )
272
+ } else {
273
+ // call transition actions
274
+ action ( transition . actions )
275
+ }
276
+ } )
275
277
}
276
278
277
279
machine . watch ?.( getParams ( ) )
0 commit comments