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 (ns reagent-data-views.browserchannel.server
(:import
(clojure.lang Atom))
(:require (:require
[clojure.tools.logging :as log] [clojure.tools.logging :as log]
[net.thegeez.browserchannel.server :as browserchannel] [net.thegeez.browserchannel.server :as browserchannel]
@ -13,29 +15,38 @@
(defn init-views! (defn init-views!
"initializes the views system and adds browserchannel-specific configuration "initializes the views system and adds browserchannel-specific configuration
to it to enable the necessary hooks into reagent-data-views. to it to enable the necessary hooks into reagent-data-views.
you should call this *instead* of views.core/init!. all of the same this function acts as a direct replacement to calling views.core/init!, so
options can be used. see views.core/init! for more information. 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 that accepts an initial context map created by reagent-data-views and
allows your application to add any information necessary to the context allows your application to add any information necessary to the context
passed to various view system functions (such as auth-fn, namespace-fn, etc)." passed to various view system functions (such as auth-fn, namespace-fn, etc)."
[views & [options]] ([^Atom view-system options]
(views/init! views views-send-fn options) (let [options (-> options
(server/set-context-fn! (:context-fn 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 (defn ->middleware
"clj-browserchannel server-side event middleware. this should be included in the "returns clj-browserchannel server-side event middleware for injecting
middleware list provided to wrap-browserchannel." 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 {:on-receive
(fn [handler] (fn [handler]
(fn [client-id request data] (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 ; only pass along receive events for data not intended for the views system
(handler client-id request data)))) (handler client-id request data))))
:on-close :on-close
(fn [handler] (fn [handler]
(fn [client-id request reason] (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)))}) (handler client-id request reason)))})

View file

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