updates for recent views changes

This commit is contained in:
Gered 2016-05-29 19:31:11 -04:00
parent 0423e35e95
commit 8657491a37
2 changed files with 38 additions and 25 deletions

View file

@ -1,4 +1,6 @@
(ns reagent-data-views.browserchannel.server
(:import
(clojure.lang Atom))
(:require
[clojure.tools.logging :as log]
[net.thegeez.browserchannel.server :as browserchannel]
@ -13,29 +15,38 @@
(defn init-views!
"initializes the views system and adds browserchannel-specific configuration
to it to enable the necessary hooks into reagent-data-views.
you should call this *instead* of views.core/init!. all of the same
options can be used. see views.core/init! for more information.
this function acts as a direct replacement to calling views.core/init!, so
are able to initialize both views and reagent-data-views by calling this
function. the arguments and return value are the same as in views.core/init!
so see that function for more information.
an additional option :context-fn can be specified which is a function
one additional option :context-fn can be specified which is a function
that accepts an initial context map created by reagent-data-views and
allows your application to add any information necessary to the context
passed to various view system functions (such as auth-fn, namespace-fn, etc)."
[views & [options]]
(views/init! views views-send-fn options)
(server/set-context-fn! (:context-fn options)))
([^Atom view-system options]
(let [options (-> options
(assoc :send-fn views-send-fn))]
(views/init! view-system options)
(server/set-context-fn! view-system (:context-fn options))))
([options]
(init-views! (atom {}) options)))
(def middleware
"clj-browserchannel server-side event middleware. this should be included in the
middleware list provided to wrap-browserchannel."
(defn ->middleware
"returns clj-browserchannel server-side event middleware for injecting
reagent-data-views handling into the clj-browserchannel client session
lifecycle handling. simply include the returned middleware map in your
Ring handler's wrap-browserchannel options."
[^Atom view-system]
{:on-receive
(fn [handler]
(fn [client-id request data]
(if-not (server/on-receive! client-id data {:request request})
(if-not (server/on-receive! view-system client-id data {:request request})
; only pass along receive events for data not intended for the views system
(handler client-id request data))))
:on-close
(fn [handler]
(fn [client-id request reason]
(server/on-close! client-id {:request request})
(server/on-close! view-system client-id {:request request})
(handler client-id request reason)))})

View file

@ -1,44 +1,46 @@
(ns reagent-data-views.server.core
(:import
(clojure.lang Atom))
(:require
[clojure.tools.logging :as log]
[views.core :as views]
[reagent-data-views.utils :refer [relevant-event?]]))
(defn- handle-subscriptions!
[client-id view-sig context]
[^Atom view-system client-id view-sig context]
(log/trace client-id "subscribing to" view-sig)
(views/subscribe! view-sig client-id context))
(views/subscribe! view-system view-sig client-id context))
(defn- handle-unsubscriptions!
[client-id view-sig context]
[^Atom view-system client-id view-sig context]
(log/trace client-id "unsubscribing from" view-sig)
(views/unsubscribe! view-sig client-id context))
(views/unsubscribe! view-system view-sig client-id context))
(defn- update-context
[existing-context]
(if-let [context-fn (get-in @views/view-system [:reagent-data-views :context-fn])]
[^Atom view-system existing-context]
(if-let [context-fn (get-in @view-system [:reagent-data-views :context-fn])]
(context-fn existing-context)
existing-context))
(defn on-close!
[client-id context]
[^Atom view-system client-id context]
(log/trace client-id "on-close, unsubscribing from all views")
(views/unsubscribe-all! client-id))
(views/unsubscribe-all! view-system client-id))
(defn on-receive!
[client-id data context]
[^Atom view-system client-id data context]
(when (relevant-event? data)
(let [context (update-context context)
(let [context (update-context view-system context)
[event view-sig] data
; for safety, since this is otherwise coming in un-altered from clients
view-sig (dissoc view-sig :namespace)]
(condp = event
:views/subscribe (handle-subscriptions! client-id view-sig context)
:views/unsubscribe (handle-unsubscriptions! client-id view-sig context)
:views/subscribe (handle-subscriptions! view-system client-id view-sig context)
:views/unsubscribe (handle-unsubscriptions! view-system client-id view-sig context)
(log/warn client-id "unrecognized event" event "-- full received data:" data))
; indicating that we handled the received event
true)))
(defn set-context-fn!
[f]
(swap! views/view-system assoc-in [:reagent-data-views :context-fn] f))
[^Atom view-system f]
(swap! view-system assoc-in [:reagent-data-views :context-fn] f))