 
 (comment
   (count (:state @*cache))
+  (refresh-async!)
   (refresh-now! db/database))

            [mount.core :as mount]
            [territory-bro.congregation :as congregation]
            [territory-bro.db :as db]
            [territory-bro.event-store :as event-store]
            [territory-bro.gis-user :as gis-user]
            [territory-bro.poller :as poller])
  (:import (com.google.common.util.concurrent ThreadFactoryBuilder)
           (java.util.concurrent Executors ScheduledExecutorService TimeUnit)))

(mount/defstate *cache
  :start (atom {:last-event nil

  "Calculates the current state from all events, including uncommitted ones,
   but does not update the cache (it could cause dirty reads to others)."
  [conn]
  (:state (apply-new-events @*cache conn)))

;; TODO
(declare refresh-async!)
(def db-admin-injections
  {:dispatch! (fn [event]
                (prn :dispatch! event)
                (dispatch-transient-event! event)
                (refresh-async!))
   :migrate-tenant-schema! (fn [args]
                             (prn :migrate-tenant-schema! args))
   :ensure-gis-user-present! (fn [args]
                               (prn :ensure-gis-user-present! args))
   :ensure-gis-user-absent! (fn [args]
                              (prn :ensure-gis-user-absent! args))})

(mount/defstate process-managers
  :start (poller/create (fn []
                          (db-admin/process-pending-changes! (cached-state) db-admin-injections)))
  :stop (poller/shutdown! process-managers))

(mount/defstate refresher
  :start (poller/create (fn []
                          (db/with-db [conn {}]
                            (refresh-now! conn))
                          (poller/trigger! process-managers)))
  :stop (poller/shutdown! refresher))

(defn refresh-async! []
