 (ns counter.core
   (:require [rum.core :as rum]
-            [scrum.core :as scrum]))
+            [scrum.core :as scrum]
+            [goog.dom :as dom]))
 
 ;;
 ;; define controller & action handlers

(defmulti control (fn [action] action))

(defmethod control :init []
  initial-state)

(defmethod control :inc [_ _ counter]
  (inc counter))

(defmethod control :dec [_ _ counter]
  (dec counter))


;;



;;
;; define effects handler
;;

(defn local-storage [r c {:keys [op key value on-success]}]
  (case op
    :get
    (->> (name key)
         js/localStorage.getItem
         (scrum/dispatch! r c on-success))
    :set
    (-> (name key)
        (js/localStorage.setItem value))))


;;
;; start up
;;

;; create Reconciler instance
(defonce reconciler
  (scrum/reconciler
    {:state (atom {})
     :controllers {:counter control}
     :effect-handlers {:local-storage local-storage}}))

;; initialize controllers
(defonce init-ctrl (scrum/broadcast-sync! reconciler :init))

;; load from localStorage
(defonce load (scrum/dispatch-sync! reconciler :counter :load :count))

;; render
(rum/mount (Counter reconciler)
           (dom/getElement "app"))
