#Can `self` be available in a guard the way actions is?

1 messages · Page 1 of 1 (latest)

unreal lava
#

Have a use case where it would be useful to check te state of itself to determine if it can run an action, but TS is saying self isnt available, though it's available in the actions. Is it possible to get access to self in the gurad?

white wyvern
#

cc. @keen hawk I know you had thoughts on this

unreal lava
#

this would be quite useful if possible! is there a workaround? not having this will force me to put this state in context, just so it can be available in a guard, when it would be better modeled as a specific state in the machine

white wyvern
#

Depends on your use-case: can you share an example of how you'd like to check the state in a guard?

unreal lava
#

i have a parallel machine where one state is called Auth and it determines if person is logged in or not, then i have another state called List which on a SAVE event should go to a Saving state but only if the Auth state matches LoggedIn, otherwise the List state should not transition

white wyvern
#

Ahhhh okay

unreal lava
#

im faced with putting the auth state in to context instead of as it's own parallel state just so i can check its value

white wyvern
#

I'm glad I asked. Would this be helpful? https://stately.ai/docs/guards#in-state-guards

A guard is a condition function that the machine checks when it goes through an event. If the condition is true, the machine follows the transition to the next state. If the condition is false, the machine follows the rest of the conditions to the next state.

#
on: {
  event: {
    guard: stateIn('#state1');
  },
  anotherEvent: {
    guard: stateIn({ form: 'submitting' })
  }
}
unreal lava
#

oh exactly

#

yes

#

though sometimes i want to combine the value with a value in context

#

thats why i just wish i had self but yeah thatll do the job

white wyvern
#
guard: and([stateIn(...), yourCustomGuard])```
unreal lava
#

👍

#

ty!

white wyvern
#

(array)

#

No problem!

unreal lava
#

that function is typed i assume ?

#

stateIn

white wyvern
#

Not exactly - difficult to type that