(ns maximgb.re-state.example.basic
(:require [re-frame.core :as rf]
[reagent.core :as reagent]
[maximgb.re-state.core :as rs])) (1)
(rs/def-machine basic-machine {:id :basic-machine
:initial :one
:states {:one {:on {:click :two}}
:two {:on {:click :three}}
:three {:on {:click :one}}}}) (2)
(defn state-cycler [] (3)
(let [controller (rs/interpreter-start! (rs/interpreter! basic-machine)) (4)
state-sub (rs/isubscribe-state controller)] (5)
(fn []
[:div
"Current state is: "
[:div {:style {:display :inline-block
:width "5em"}}
@state-sub]
[:button
{:on-click #(rs/interpreter-send! controller :click)} (6)
"Next state"]])))
(defn -main []
(reagent/render [:div
[:div "State cycler component, press \"Next state\" button to cycle states."]
[state-cycler]]
(.getElementById js/document "app"))) (7)
(.addEventListener js/window "load" -main)