Actions are side-effects that are executed on state transitions.
There are two kinds of actions:
Instead of define a transition target as simply a keyword, you need to use the full form:
{:on {:some-event {:target :some-state
:actions some-action}}}
The actions value can also be a vector, and the actions would be executed one by one.
{:on {:some-event {:target :some-state
:actions [action1 action2]}}}
Entry actions are defined on a state, and are executed whenever this state is entered. Similar for exit actions - they are executed whenever leaving the state.
{:states
{:state1 {:entry some-action-on-entry
:exit some-action-on-exit
:on {...}}}}
;; entry/exit can also be vector of actions
{:states
{:state1 {:entry [action1 action2]
:exit [action3 action4]
:on {...}}}}
The action function is invoked with two arguments: (state event)
Actions can update the context of the state machine.
(require '[statecharts.core :as fsm :refer [assign]])
(defn update-counter [state event]
(update state :counter inc))
{:states
{:state1 {:on {:some-event {:target :state2
:action (assign update-counter)}}}}}
Note the action is wrapped with statecharts.core/assign
. Without this it's return value is ignored and the state context is not changed.
The event
arg of update-counter would be {:type :some-event}
. Extra keys could be passed when calling fsm/transition
:
(let [event {:type :some-event
:k1 :v1
:k1 :v2}]
(fsm/transition machine current-state event))
And the event
argument passed to the update-counter
would have these :k1
:k2
keys etc.
_prev-state
in Action FunctionsDuring action execution time, _state
already points to the new
state after the transition.
The action functions could use the value under the _prev-state
key of the context
to access the previous state before the transition (e.g. for debugging, or
archiving some information for later analysis).
Please note:
_state
, the _prev-state
variable only exists during the transition and
would not be available after that.{:exec false}
, the actions would be returned
to the caller instead of being executed. In that case it would also have no
access to _prev-state
.{{< loadcode "samples/src/statecharts-samples/trigger_actions.clj" >}}
Can you improve this documentation?Edit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close