1- import type { BaseSchema , Bindable , BindableContext , BindableRefs , GuardFn , MachineConfig , Service } from "@zag-js/core"
1+ import type {
2+ ActionsOrFn ,
3+ BaseSchema ,
4+ Bindable ,
5+ BindableContext ,
6+ BindableRefs ,
7+ ChooseFn ,
8+ ComputedFn ,
9+ EffectsOrFn ,
10+ GuardFn ,
11+ MachineConfig ,
12+ Params ,
13+ Service ,
14+ } from "@zag-js/core"
215import { createScope } from "@zag-js/core"
316import { isFunction , isString , toArray , warn } from "@zag-js/utils"
417import { useLayoutEffect , useMemo , useRef } from "react"
@@ -59,7 +72,7 @@ export function useMachine<T extends BaseSchema>(
5972
6073 const refs : BindableRefs < T > = useRefs ( machine . refs ?.( { prop, context : ctx } ) ?? { } )
6174
62- const getParams = ( ) : any => ( {
75+ const getParams = ( ) : Params < T > => ( {
6376 state,
6477 context : ctx ,
6578 event : {
@@ -76,9 +89,12 @@ export function useMachine<T extends BaseSchema>(
7689 computed,
7790 flush,
7891 scope,
92+ choose,
7993 } )
8094
81- const action = ( strs : T [ "action" ] [ ] ) => {
95+ const action = ( keys : ActionsOrFn < T > | undefined ) => {
96+ const strs = isFunction ( keys ) ? keys ( getParams ( ) ) : keys
97+ if ( ! strs ) return
8298 const fns = strs . map ( ( s ) => {
8399 const fn = machine . implementations ?. actions ?. [ s ]
84100 if ( ! fn ) warn ( `[zag-js] No implementation found for action "${ JSON . stringify ( s ) } "` )
@@ -89,12 +105,14 @@ export function useMachine<T extends BaseSchema>(
89105 }
90106 }
91107
92- const guard = ( str : T [ "guard" ] | GuardFn ) => {
108+ const guard = ( str : T [ "guard" ] | GuardFn < T > ) => {
93109 if ( isFunction ( str ) ) return str ( getParams ( ) )
94110 return machine . implementations ?. guards ?. [ str ] ( getParams ( ) )
95111 }
96112
97- const effect = ( strs : T [ "effect" ] [ ] ) => {
113+ const effect = ( keys : EffectsOrFn < T > | undefined ) => {
114+ const strs = isFunction ( keys ) ? keys ( getParams ( ) ) : keys
115+ if ( ! strs ) return
98116 const fns = strs . map ( ( s ) => {
99117 const fn = machine . implementations ?. effects ?. [ s ]
100118 if ( ! fn ) warn ( `[zag-js] No implementation found for effect "${ JSON . stringify ( s ) } "` )
@@ -108,17 +126,28 @@ export function useMachine<T extends BaseSchema>(
108126 return ( ) => cleanups . forEach ( ( fn ) => fn ?.( ) )
109127 }
110128
111- const computed = ( key : keyof T [ "computed" ] ) => {
112- return machine . computed ?. [ key ] ( {
113- context : ctx as any ,
114- event : eventRef . current ,
115- prop,
116- refs,
117- scope,
118- computed : computed as any ,
129+ const choose : ChooseFn < T > = ( transitions ) => {
130+ return toArray ( transitions ) . find ( ( t ) => {
131+ let result = ! t . guard
132+ if ( isString ( t . guard ) ) result = ! ! guard ( t . guard )
133+ else if ( isFunction ( t . guard ) ) result = t . guard ( getParams ( ) )
134+ return result
119135 } )
120136 }
121137
138+ const computed : ComputedFn < T > = ( key ) => {
139+ return (
140+ machine . computed ?. [ key ] ( {
141+ context : ctx as any ,
142+ event : eventRef . current ,
143+ prop,
144+ refs,
145+ scope,
146+ computed : computed as any ,
147+ } ) ?? ( { } as any )
148+ )
149+ }
150+
122151 const state = useBindable ( ( ) => ( {
123152 defaultValue : machine . initialState ( { prop } ) ,
124153 onChange ( nextState , prevState ) {
@@ -134,27 +163,27 @@ export function useMachine<T extends BaseSchema>(
134163 // exit actions
135164 if ( prevState ) {
136165 // @ts -ignore
137- action ( machine . states [ prevState ] ?. exit ?? [ ] )
166+ action ( machine . states [ prevState ] ?. exit )
138167 }
139168
140169 // transition actions
141- action ( transitionRef . current ?. actions ?? [ ] )
170+ action ( transitionRef . current ?. actions )
142171
143172 // enter effect
144173 // @ts -ignore
145- const cleanup = effect ( machine . states [ nextState ] ?. effects ?? [ ] )
146- effects . current . set ( nextState as string , cleanup )
174+ const cleanup = effect ( machine . states [ nextState ] ?. effects )
175+ if ( cleanup ) effects . current . set ( nextState as string , cleanup )
147176
148177 // root entry actions
149178 if ( prevState === "__init__" ) {
150- action ( machine . entry ?? [ ] )
151- const cleanup = effect ( machine . effects ?? [ ] )
152- effects . current . set ( "__init__" , cleanup )
179+ action ( machine . entry )
180+ const cleanup = effect ( machine . effects )
181+ if ( cleanup ) effects . current . set ( "__init__" , cleanup )
153182 }
154183
155184 // enter actions
156185 // @ts -ignore
157- action ( machine . states [ nextState ] ?. entry ?? [ ] )
186+ action ( machine . states [ nextState ] ?. entry )
158187 } ,
159188 } ) )
160189
@@ -165,7 +194,7 @@ export function useMachine<T extends BaseSchema>(
165194 fns . forEach ( ( fn ) => fn ?.( ) )
166195 effects . current = new Map ( )
167196 transitionRef . current = null
168- action ( machine . exit ?? [ ] )
197+ action ( machine . exit )
169198 }
170199 } , [ ] )
171200
@@ -187,13 +216,7 @@ export function useMachine<T extends BaseSchema>(
187216 // @ts -ignore
188217 machine . on ?. [ event . type ]
189218
190- const transition = toArray ( transitions ) . find ( ( t ) => {
191- let result = ! t . guard
192- if ( isString ( t . guard ) ) result = ! ! guard ( t . guard )
193- else if ( isFunction ( t . guard ) ) result = t . guard ( getParams ( ) )
194- return result
195- } )
196-
219+ const transition = choose ( transitions )
197220 if ( ! transition ) return
198221
199222 // save current transition
0 commit comments