 (defn get-event-listener-key [k]
   (get-in @state [:ech k :listener-key]))
 
-(defn swap-event-channel! [k [listener-key channel]]
+(defn setup-event-channel! [k [listener-key channel]]
   (swap! state assoc-in [:ech k]
          {:channel channel :listener-key listener-key}))
 
-(defonce setup-event-channels!
-  (do
-    (swap-event-channel! :env-keyboard-key (poly/listen-put! js/window :key))
-    (swap-event-channel! :env-mouse-click (poly/listen-put! js/window :mouse-click))
-    (swap-event-channel! :env-mouse-move (poly/listen-put! js/window :mouse-move))
-    true))
+(defn setup-event-channels! []
+  (setup-event-channel! :env-keyboard-key (poly/listen-put! js/window :key))
+  (setup-event-channel! :env-mouse-click (poly/listen-put! js/window :mouse-click))
+  (setup-event-channel! :env-mouse-move (poly/listen-put! js/window :mouse-move)))
+
+(defn teardown-event-channel! [k]
+  (poly/unlisten! (get-event-listener-key k) (get-event-channel k))
+  (swap! state assoc-in [:ech k] nil))
+
+(defn teardown-event-channels! []
+  (map teardown-event-channel! (keys (:ech @state))))
 
 
 ;; -----------------------------------------------------------------------------
 ;; Event Handlers
 
 (defn on-env-frames-per-second [fps]
-  (swap! state assoc-in [:env :frames-per-second] fps))
+  (swap! state assoc-in [:env :frames-per-second] fps)
+  (get-in @state [:app :measure-fps?]))
 
 (defn on-env-keyboard-key [m]
-  (console/info (:ion.poly.core/keyword m))
-  (inspect m)
-  (condp = (:ion.poly.core/keyword m)
+  (console/info (:poly/keyword m))
+  ;(inspect m)
+  (condp = (:poly/keyword m)
     :a :>> #(klang/hide!)
     :q :>> #(klang/show!)
     nil))

;; -----------------------------------------------------------------------------
;; App Div

(defonce app-div (poly/get-element :app))


;; -----------------------------------------------------------------------------

        mouse-x (:client-x mouse-move)
        mouse-y (:client-y mouse-move)
        mouse-pos (str "[" mouse-x ":" mouse-y "]")]
    (set! (.-innerText (app-div))
          (string/join ", " [app-name fps env-time timestamp mouse-pos]))))

(declare request-simple-animation)

(defn simple-animation-cycle [timestamp]
  (when (get-in @state [:app :simple-animation?])
    (request-simple-animation)
    (animate timestamp @state)))

(def request-simple-animation (partial poly/request-animation-frame
                                       simple-animation-cycle))

(defn setup-simple-animation! []
  (swap! state assoc-in [:app :simple-animation?] true)
  (request-simple-animation))

(defn teardown-simple-animation! []
  (swap! state assoc-in [:app :simple-animation?] false))


;; -----------------------------------------------------------------------------
