add namespace-fn for selecting view-sig namespaces on view subs/unsubs
This commit is contained in:
parent
202ec3995a
commit
eddcfa0929
|
@ -13,6 +13,7 @@
|
||||||
;; :send-fn (fn [subscriber-key data] ...)
|
;; :send-fn (fn [subscriber-key data] ...)
|
||||||
;; :put-hints-fn (fn [hints] ... )
|
;; :put-hints-fn (fn [hints] ... )
|
||||||
;; :auth-fn (fn [view-sig subscriber-key context] ...)
|
;; :auth-fn (fn [view-sig subscriber-key context] ...)
|
||||||
|
;; :namespace-fn (fn [view-sig subscriber-key context] ...)
|
||||||
;;
|
;;
|
||||||
;; :hashes {view-sig hash, ...}
|
;; :hashes {view-sig hash, ...}
|
||||||
;; :subscribed {subscriber-key #{view-sig, ...}}
|
;; :subscribed {subscriber-key #{view-sig, ...}}
|
||||||
|
@ -46,10 +47,13 @@
|
||||||
(def refresh-queue (ArrayBlockingQueue. refresh-queue-size))
|
(def refresh-queue (ArrayBlockingQueue. refresh-queue-size))
|
||||||
|
|
||||||
(defn ->view-sig
|
(defn ->view-sig
|
||||||
[namespace view-id parameters]
|
([namespace view-id parameters]
|
||||||
{:namespace namespace
|
{:namespace namespace
|
||||||
:view-id view-id
|
:view-id view-id
|
||||||
:parameters parameters})
|
:parameters parameters})
|
||||||
|
([view-id parameters]
|
||||||
|
{:view-id view-id
|
||||||
|
:parameters parameters}))
|
||||||
|
|
||||||
(defn- send-view-data!
|
(defn- send-view-data!
|
||||||
[subscriber-key {:keys [namespace view-id parameters] :as view-sig} data]
|
[subscriber-key {:keys [namespace view-id parameters] :as view-sig} data]
|
||||||
|
@ -65,6 +69,12 @@
|
||||||
; so do not disallow access to any subscription
|
; so do not disallow access to any subscription
|
||||||
true))
|
true))
|
||||||
|
|
||||||
|
(defn- get-namespace
|
||||||
|
[view-sig subscriber-key context]
|
||||||
|
(if-let [namespace-fn (:namespace-fn @view-system)]
|
||||||
|
(namespace-fn view-sig subscriber-key context)
|
||||||
|
(:namespace view-sig)))
|
||||||
|
|
||||||
(defn- subscribe-view!
|
(defn- subscribe-view!
|
||||||
[view-system view-sig subscriber-key]
|
[view-system view-sig subscriber-key]
|
||||||
(-> view-system
|
(-> view-system
|
||||||
|
@ -76,9 +86,10 @@
|
||||||
(update-in view-system [:hashes view-sig] #(or % data-hash))) ;; see note #1 in NOTES.md
|
(update-in view-system [:hashes view-sig] #(or % data-hash))) ;; see note #1 in NOTES.md
|
||||||
|
|
||||||
(defn subscribe!
|
(defn subscribe!
|
||||||
[namespace view-id parameters subscriber-key & [context]]
|
[{:keys [namespace view-id parameters] :as view-sig} subscriber-key context]
|
||||||
(when-let [view (get-in @view-system [:views view-id])]
|
(when-let [view (get-in @view-system [:views view-id])]
|
||||||
(let [view-sig (->view-sig namespace view-id parameters)]
|
(let [namespace (get-namespace view-sig subscriber-key context)
|
||||||
|
view-sig (->view-sig namespace view-id parameters)]
|
||||||
(if (authorized-subscription? view-sig subscriber-key context)
|
(if (authorized-subscription? view-sig subscriber-key context)
|
||||||
(do
|
(do
|
||||||
(swap! view-system subscribe-view! view-sig subscriber-key)
|
(swap! view-system subscribe-view! view-sig subscriber-key)
|
||||||
|
@ -132,10 +143,11 @@
|
||||||
view-system))
|
view-system))
|
||||||
|
|
||||||
(defn unsubscribe!
|
(defn unsubscribe!
|
||||||
[namespace view-id parameters subscriber-key]
|
[{:keys [namespace view-id parameters] :as view-sig} subscriber-key context]
|
||||||
(swap! view-system
|
(swap! view-system
|
||||||
(fn [vs]
|
(fn [vs]
|
||||||
(let [view-sig (->view-sig namespace view-id parameters)]
|
(let [namespace (get-namespace view-sig subscriber-key context)
|
||||||
|
view-sig (->view-sig namespace view-id parameters)]
|
||||||
(-> vs
|
(-> vs
|
||||||
(remove-from-subscribed view-sig subscriber-key)
|
(remove-from-subscribed view-sig subscriber-key)
|
||||||
(remove-from-subscribers view-sig subscriber-key)
|
(remove-from-subscribers view-sig subscriber-key)
|
||||||
|
@ -319,19 +331,25 @@
|
||||||
[f]
|
[f]
|
||||||
(swap! view-system assoc :auth-fn f))
|
(swap! view-system assoc :auth-fn f))
|
||||||
|
|
||||||
|
(defn set-namespace-fn!
|
||||||
|
"Sets a function that returns a namespace to use for view subscriptions."
|
||||||
|
[f]
|
||||||
|
(swap! view-system assoc :namespace-fn f))
|
||||||
|
|
||||||
(defn init!
|
(defn init!
|
||||||
"Initializes the view system for use with some basic defaults that can be
|
"Initializes the view system for use with some basic defaults that can be
|
||||||
overridden as needed. Many applications may want to ignore this function
|
overridden as needed. Many applications may want to ignore this function
|
||||||
and instead manually initialize the view system themselves. Some of the
|
and instead manually initialize the view system themselves. Some of the
|
||||||
defaults set by this function are only appropriate for non-distributed
|
defaults set by this function are only appropriate for non-distributed
|
||||||
configurations."
|
configurations."
|
||||||
[& {:keys [refresh-interval worker-threads send-fn put-hints-fn auth-fn views]
|
[& {:keys [refresh-interval worker-threads send-fn put-hints-fn auth-fn namespace-fn views]
|
||||||
:or {refresh-interval 1000
|
:or {refresh-interval 1000
|
||||||
worker-threads 4
|
worker-threads 4
|
||||||
put-hints-fn #(refresh-views! %)}}]
|
put-hints-fn #(refresh-views! %)}}]
|
||||||
(if send-fn (set-send-fn! send-fn))
|
(if send-fn (set-send-fn! send-fn))
|
||||||
(if put-hints-fn (set-put-hints-fn! put-hints-fn))
|
(if put-hints-fn (set-put-hints-fn! put-hints-fn))
|
||||||
(if auth-fn (set-auth-fn! auth-fn))
|
(if auth-fn (set-auth-fn! auth-fn))
|
||||||
|
(if namespace-fn (set-namespace-fn! namespace-fn))
|
||||||
(if views (add-views! views))
|
(if views (add-views! views))
|
||||||
(start-update-watcher! refresh-interval worker-threads))
|
(start-update-watcher! refresh-interval worker-threads))
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue