Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What signals does the machine accept given the current state? #13

Open
ikitommi opened this issue Apr 15, 2022 · 3 comments
Open

What signals does the machine accept given the current state? #13

ikitommi opened this issue Apr 15, 2022 · 3 comments

Comments

@ikitommi
Copy link

XState has an api to ask which signals are allowed given the current state:

const { initialState } = lightMachine;

console.log(initialState.nextEvents);
// => ['TIMER', 'EMERGENCY']

This is really important as this information can be used to enable/disable & hide/show action buttons on uis based on their current availability.

I propose clj-statecharts to have a function of machine state -> State, e.g.

(def fsm
  (fsm/machine
   {:id :nested
    :initial :a
    :on {:top {:target :a
               :meta {:ui {:text "restart"}}}}
    :states {:a {:on {:a {:target :a
                          :meta {:ui {:text "nada"}}}
                      :b {:target :b
                          :meta {:ui {:text "goto b"}}}}}
             :b {:on {:c :c}}
             :c {:on {:c :c}}}}))

(fsm/next fsm (fsm/initialize fsm))
;{:top {:target :a
;       :meta {:ui {:text "restart"}}}
; :a {:target :a
;     :meta {:ui {:text "nada"}}}
; :b {:target :b
;     :meta {:ui {:text "goto b"}}}}
@lucywang000
Copy link
Owner

I can see the rationale, but atm I myself doesn't have a use case for that. So patches welcome.

Some considerations:

  1. allowed transitions could be from either the leaf state itself, or any of its ancestors.
  2. for transitions withs guards, the guard is going to be evaluated, but I guess this is fine since guards are for most of the time are just pure functions of current state.

@ikitommi
Copy link
Author

Thanks. I added this into my preprocessor for clj-statecharts, I can now enrich and precompute things I need, so not in an urgent need of this now at library level. If you want to close this, ok for me. Happy to do a PR at some point, when needs and impls have setteled. Cheers.

@ikitommi
Copy link
Author

e.g. have a protocol to both hide the impl and add missing things. Not doing much yet and not sure where it evolves from here.

(defprotocol FSM
  (-id [this])
  (-initial [this])
  (-states [this])
  (-next [this state])
  (-form [this])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants