qmuntal / stateless

Go library for creating finite state machines
BSD 2-Clause "Simplified" License
898 stars 47 forks source link

How to get the super state of hierarchical state? #58

Closed kaundinya5 closed 1 year ago

kaundinya5 commented 1 year ago

I have states called in_review,verifying and waiting_for_decisions that are all a substate of processing. I want the user to only know that it's in processing if it's in any of these substates. I know that I can do IsInState and verify if it's processing. But is there a way to directly get the super state for all of the substates?

qmuntal commented 1 year ago

IsInState (and IsInStateCtx) should return true if the current state is equal to, or a substate of, the supplied state, so if I understand correctly it will already work for your use-case.

kaundinya5 commented 1 year ago

But I want to show the super state to the user(I have other super states too).Is there a function like StateMachine.State that return the super state and not precise current state? Essentially I don't want to write a Switch case to check if the state I'm in is part of a super state or not using IsInState

qmuntal commented 1 year ago

@kaundinya5 IMO writing a switch case that customizes what you want to display to your clients is not a bad solution. I would rather not grow stateless trying to cover the visualization needs of each use case, but just provide the foundations so users can do it themselves.

kaundinya5 commented 1 year ago

Ah, okay. Do let me know if I can create a PR with this functionality.