Skip to content

Instantly share code, notes, and snippets.

@jebberjeb
Created November 9, 2018 20:36
Show Gist options
  • Save jebberjeb/2274d8187365119ee79ccca05883d698 to your computer and use it in GitHub Desktop.
Save jebberjeb/2274d8187365119ee79ccca05883d698 to your computer and use it in GitHub Desktop.
(def jp-fsm {:init :uninitialized
:entity-type :journalpost
:state-field :journalpost/status
:states {:uninitialized {:transitions [{:event :oprettet
:target :udkast}]}
:udkast {:entities {:journalnote '...}
:transitions [{:event :lock
:target :laast}
{:event :update}]}
:laast {:transitions [{:event :open
:target :udkast}]}}})
;; read-only is like journalized or approved or whatever
;; no actions are allowed
;; Any time the state of the top-level entity changes, you reinitialize the
;; state machines of all nested entities. For example:
;; * approve sag
;; * sag status -> faerdig (has sub-fsm of jp-fsm-read-only
;; * rerun all jp state machine init
;; * if state of jp is udkast, since jp-fsm-read-only doesn't have a state for
;; udkast, jp status is set to start state (:read-only)
(def jp-fsm-read-only
{:start-state :read-only
:entity-type :journalpost
:state-field :journalpost/status
:states {:read-only {}}})
(reg-action :sag/approve (fn [sag e] ...)) ;; set sag status
(def sag-fsm {:entity-type :sag
:state-field :sag/status
:transitions [{:event :relate-sag}]
:start-state nil
:states {;; Or, use top level :start-state attribute
nil {:transtions [{:event :oprettet
:target :aaben}]}
:aaben {;; The transitions in jp-fsm are available when
;; sag is in an aaben state.
;; Bad name!
:sub-state-machines #{jp-fsm}
:tranistions [{:event :update-sag}
{:event :sag/approve
;; Return commands, or return an updated sag
:commands (fn [sag e] ...)
;; Can commands fn here assoc-in journalpost status?
;; Or should that be handled by nested state machine?
#_#_ => [{:type :enitity
:data new-sag}
{:type :workzone}]
}]}
:faerdig {:sub-state-machines #{jp-fsm-read-only}
:transitions [{:event :finish
:target :afgjort}
{:event :reopen
:target :aaben}]}
:afgjort {:sub-state-machines #{jp-fsm-read-only}}}})
{:sag :aaben :post :udkast} => :lock => [:sag/journalposter index :journalpost/status]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment