+
(defsc TrafficLight [this {:ui/keys [color]}]
+ {:query [:ui/color]
+ :initial-state {:ui/color "green"}
+ :ident (fn [] [:component/id ::TrafficLight])
+ :statechart (statechart {}
+ (state {:id :state/running}
+ (on :event/unmount :state/exit)
+ (transition {:event :event/toggle}
+ (script {:expr (fn [_ {:keys [blink-mode?]}]
+ [(ops/assign :blink-mode? (not blink-mode?))])}))
+
+ (state {:id :state/green}
+ (on-entry {} (script {:expr (fn [_ _] [(fops/assoc-alias :color "green")])}))
+ (send-after {:delay 2000
+ :id :gty
+ :event :timeout})
+ (transition {:event :timeout
+ :target :state/yellow}))
+ (state {:id :state/yellow}
+ (on-entry {} (script {:expr (fn [_ _] [(fops/assoc-alias :color "yellow")])}))
+ (send-after {:delay 500
+ :id :ytr
+ :event :timeout})
+ (transition {:event :timeout
+ :target :state/red}))
+ (state {:id :state/red}
+ (on-entry {} (script {:expr (fn [_ _] [(fops/assoc-alias :color "red")])}))
+ (send-after {:delay 2000
+ :id :rtg
+ :event :timeout})
+ (transition {:cond (fn [_ {:keys [blink-mode?]}]
+ (boolean blink-mode?))
+ :event :timeout
+ :target :state/black})
+ (transition {:event :timeout
+ :target :state/green}))
+ (state {:id :state/black}
+ (on-entry {} (script {:expr (fn [_ _] [(fops/assoc-alias :color "black")])}))
+ (send-after {:delay 500
+ :id :otr
+ :event :timeout})
+ (transition {:event :timeout
+ :target :state/red})))
+ (final {:id :state/exit}))
+ :use-hooks? true}
+ (let [{:keys [send! local-data]} (sch/use-statechart this {:data {:fulcro/aliases {:color [:actor/component :ui/color]}}})]
+ (dom/div {}
+ (dom/div {:style {:backgroundColor color
+ :width "20px"
+ :height "20px"}})
+ (dom/button {:onClick (fn [] (send! :event/toggle))}
+ (if (:blink-mode? local-data) "Blink" "Regulate")))))
+