clean up init! and how view-system options are set

This commit is contained in:
Gered 2016-05-21 18:19:21 -04:00
parent 4458ba3ea3
commit 06f275399a

View file

@ -360,56 +360,75 @@
[hints] [hints]
((:put-hints-fn @view-system) hints)) ((:put-hints-fn @view-system) hints))
(defn- get-views-map
[views]
(map vector (map id views) views))
(defn add-views! (defn add-views!
"Add a collection of views to the system." "Add a collection of views to the system."
[views] [views]
(swap! view-system update-in [:views] (fnil into {}) (map vector (map id views) views))) (swap! view-system update-in [:views] (fnil into {}) (get-views-map views)))
(defn set-send-fn! (def default-options
"Sets a function that sends view data to a subscriber whenever a view it "Default options used to initialize the views system via init!"
is subscribed to has refreshed data." {
[f] ; interval in milliseconds at which the refresh watcher thread will
(swap! view-system assoc :send-fn f)) ; check for any queued up hints and dispatch relevant view refresh
; updates to the worker threads.
:refresh-interval 1000
(defn set-put-hints-fn! ; the number of refresh worker threads that poll for view refresh
"Sets a function that adds hints to the view system. The function set is intended ; requests and dispatch updated view data to subscribers.
to be used by other implementations of IView." :worker-threads 8
[f]
(swap! view-system assoc :put-hints-fn f))
(defn set-auth-fn! ; a function that adds hints to the view system. this function will be used
"Sets a function that authorizes view subscriptions. If authorization fails ; by other libraries that implement IView. this function must be set for
(the function returns false), the subscription is not processed." ; normal operation of the views system. the default function provided
[f] ; will trigger relevant view refreshes immediately.
(swap! view-system assoc :auth-fn f)) ; (fn [hints] ... )
:put-hints-fn (fn [hints] (refresh-views! hints))
(defn set-namespace-fn! ; a function that authorizes view subscriptions. should return true if the
"Sets a function that returns a namespace to use for view subscriptions." ; subscription is authorized. if not set, no view subscriptions will require
[f] ; any authorization.
(swap! view-system assoc :namespace-fn f)) ; (fn [subscriber-key view-sig context] ... )
:auth-fn nil
; a function that returns a namespace to use for view subscriptions
; (fn [subscriber-key view-sig context] ... )
:namespace-fn nil
; interval in milliseconds at which a logger will write view system
; statistics to the log. if not set, the logger will be disabled.
:stats-log-interval nil
})
(defn init! (defn init!
"Initializes the view system for use with some basic defaults that can be "Initializes the view system for use with the list of views provided.
overridden as needed. Many applications may want to ignore this function
and instead manually initialize the view system themselves. Some of the send-fn is a function that sends view refresh data to subscribers. it is
defaults set by this function are only appropriate for non-distributed of the form: (fn [subscriber-key [view-sig view-data]] ... )
configurations."
[& {:keys [refresh-interval worker-threads send-fn put-hints-fn auth-fn namespace-fn views stats-log-interval] options is a map of options to configure the view system with. See
:or {refresh-interval 1000 views.core/default-options for a description of the available options
worker-threads 4 and the defaults that will be used for any options not provided in
put-hints-fn #(refresh-views! %)}}] the call to init!."
(if send-fn (set-send-fn! send-fn)) [views send-fn & [options]]
(if put-hints-fn (set-put-hints-fn! put-hints-fn)) (let [options (merge default-options options)]
(if auth-fn (set-auth-fn! auth-fn)) (reset! view-system
(if namespace-fn (set-namespace-fn! namespace-fn)) {:views (into {} (get-views-map views))
(if views (add-views! views)) :send-fn send-fn
(when stats-log-interval :put-hints-fn (:put-hints-fn options)
:auth-fn (:auth-fn options)
:namespace-fn (:namespace-fn options)})
(start-update-watcher! (:refresh-interval options)
(:worker-threads options))
(when-let [stats-log-interval (:stats-log-interval options)]
(swap! view-system assoc :logging? true) (swap! view-system assoc :logging? true)
(start-logger! stats-log-interval)) (start-logger! stats-log-interval))))
(start-update-watcher! refresh-interval worker-threads))
(defn shutdown! (defn shutdown!
"Closes the view system down, terminating all worker threads and clearing "Shuts the view system down, terminating all worker threads and clearing
all view subscriptions and data." all view subscriptions and data."
[] []
(stop-update-watcher!) (stop-update-watcher!)