add '!' suffix to a bunch of functions that were not side-effect free
This commit is contained in:
parent
7c28a86913
commit
c8c6d3080d
|
@ -9,7 +9,7 @@
|
|||
(enable-console-print!)
|
||||
|
||||
(defn say [text]
|
||||
(browserchannel/send-data {:msg text}))
|
||||
(browserchannel/send-data! {:msg text}))
|
||||
|
||||
(defn toggle-element [elem]
|
||||
(dom/toggle-attr! elem :disabled (not (dom/attr elem :disabled))))
|
||||
|
|
|
@ -16,17 +16,17 @@
|
|||
{:on-open
|
||||
(fn [session-id request]
|
||||
(println "client" session-id "connected")
|
||||
(browserchannel/send-data-to-all {:msg (str "client " session-id " connected")}))
|
||||
(browserchannel/send-data-to-all! {:msg (str "client " session-id " connected")}))
|
||||
|
||||
:on-close
|
||||
(fn [session-id request reason]
|
||||
(println "client" session-id "disconnected. reason: " reason)
|
||||
(browserchannel/send-data-to-all {:msg (str "client " session-id " disconnected. reason: " reason)}))
|
||||
(browserchannel/send-data-to-all! {:msg (str "client " session-id " disconnected. reason: " reason)}))
|
||||
|
||||
:on-receive
|
||||
(fn [session-id request m]
|
||||
(println "client" session-id "sent" m)
|
||||
(browserchannel/send-data-to-all m))})
|
||||
(browserchannel/send-data-to-all! m))})
|
||||
|
||||
(def app-routes
|
||||
(routes
|
||||
|
|
|
@ -124,7 +124,7 @@
|
|||
(.setLevel level)
|
||||
(.addHandler (or f #(js/console.log %)))))
|
||||
|
||||
(defn send-data
|
||||
(defn send-data!
|
||||
"sends data to the server over the forward channel. context can
|
||||
contain optional callback functions:
|
||||
|
||||
|
|
|
@ -150,12 +150,12 @@
|
|||
(set-error-mode! listeners-agent :continue)
|
||||
|
||||
|
||||
(defn add-listener
|
||||
(defn add-listener!
|
||||
[session-id event-key f]
|
||||
(send-off listeners-agent
|
||||
update-in [session-id event-key] #(conj (or % []) f)))
|
||||
|
||||
(defn notify-listeners
|
||||
(defn notify-listeners!
|
||||
[session-id request event-key & data]
|
||||
(send-off listeners-agent
|
||||
(fn [listeners]
|
||||
|
@ -539,11 +539,11 @@
|
|||
(doseq [[id [string {:keys [on-error] :as context}]] remaining]
|
||||
(if on-error (on-error))))
|
||||
; finally raise the session close event
|
||||
(notify-listeners id request :close message)
|
||||
(notify-listeners! id request :close message)
|
||||
nil ;; the agent will no longer wrap a session
|
||||
))
|
||||
|
||||
(defn- handle-old-session-reconnect
|
||||
(defn- handle-old-session-reconnect!
|
||||
[req old-session-id old-session-agent old-array-id]
|
||||
(log/trace old-session-id ": old session reconnect")
|
||||
(send-off old-session-agent
|
||||
|
@ -555,7 +555,7 @@
|
|||
|
||||
;; creates a session agent wrapping session data and
|
||||
;; adds the session to sessions
|
||||
(defn- create-session-agent
|
||||
(defn- create-session-agent!
|
||||
[req events options]
|
||||
(let [{initial-rid "RID" ;; identifier for forward channel
|
||||
app-version "CVER" ;; client can specify a custom app-version
|
||||
|
@ -564,7 +564,7 @@
|
|||
;; when a client specifies and old session id then that old one
|
||||
;; needs to be removed
|
||||
(if-let [old-session-agent (@sessions old-session-id)]
|
||||
(handle-old-session-reconnect req old-session-id old-session-agent old-array-id))
|
||||
(handle-old-session-reconnect! req old-session-id old-session-agent old-array-id))
|
||||
(let [id (uuid)
|
||||
details {:address (:remote-addr req)
|
||||
:headers (:headers req)
|
||||
|
@ -597,8 +597,8 @@
|
|||
(send-off session-agent refresh-session-timeout)
|
||||
;; register application-level browserchannel session events
|
||||
(let [{:keys [on-open on-close on-receive]} events]
|
||||
(if on-close (add-listener id :close on-close))
|
||||
(if on-receive (add-listener id :map on-receive))
|
||||
(if on-close (add-listener! id :close on-close))
|
||||
(if on-receive (add-listener! id :map on-receive))
|
||||
(if on-open (on-open id req)))
|
||||
session-agent)))
|
||||
|
||||
|
@ -613,20 +613,19 @@
|
|||
|
||||
|
||||
|
||||
;; convience function to send data to a session
|
||||
;; the data will be queued until there is a backchannel to send it
|
||||
;; over
|
||||
(defn- send-map
|
||||
[session-id m context]
|
||||
;; convience function to send arbitrary clojure data structures to a session
|
||||
;; the data will be queued until there is a backchannel to send it over
|
||||
(defn- send-raw-data!
|
||||
[session-id data context]
|
||||
(when-let [session-agent (get @sessions session-id)]
|
||||
(let [string (json/generate-string m)]
|
||||
(let [string (json/generate-string data)]
|
||||
(send-off session-agent
|
||||
#(-> %
|
||||
(queue-string string context)
|
||||
flush-buffer))
|
||||
string)))
|
||||
|
||||
(defn send-data
|
||||
(defn send-data!
|
||||
"sends data to the client identified by session-id over the backchannel.
|
||||
if there is currently no available backchannel for this client, the data
|
||||
is queued until one is available. context can contain optional callback
|
||||
|
@ -639,16 +638,16 @@
|
|||
on-error - when there was an error (of any kind) sending the data"
|
||||
[session-id data & [context]]
|
||||
(if data
|
||||
(send-map session-id (encode-map data) context)))
|
||||
(send-raw-data! session-id (encode-map data) context)))
|
||||
|
||||
(defn send-data-to-all
|
||||
(defn send-data-to-all!
|
||||
"sends data to all currently connected clients over their backchannels.
|
||||
context can contain optional callback functions which will be used for
|
||||
all the data sent. see send-data for a description of the different
|
||||
callback functions available."
|
||||
[data & [context]]
|
||||
(doseq [[session-id _] @sessions]
|
||||
(send-data session-id data context)))
|
||||
(send-data! session-id data context)))
|
||||
|
||||
(defn connected?
|
||||
"returns true if a client with the given session-id is currently connected."
|
||||
|
@ -656,7 +655,8 @@
|
|||
(contains? @sessions session-id))
|
||||
|
||||
(defn disconnect!
|
||||
"forcefully closes/disconnects the client session identified by session-id.
|
||||
"forcefully and instantly closes/disconnects the client session
|
||||
identified by session-id.
|
||||
NOTE: the client code in net.thegeez.browserchannel.client will treat
|
||||
this as an error and may try to reconnect. if you do not wish this
|
||||
to happen, use close! instead."
|
||||
|
@ -672,7 +672,7 @@
|
|||
it is possible the session won't be closed right away if e.g. the
|
||||
client does not currently have an active backchannel."
|
||||
[session-id & [reason]]
|
||||
(send-map session-id ["stop"] {:on-sent #(disconnect! session-id reason)}))
|
||||
(send-raw-data! session-id ["stop"] {:on-sent #(disconnect! session-id reason)}))
|
||||
|
||||
(defn get-status
|
||||
"returns connection status info about the client identified by session-id"
|
||||
|
@ -751,7 +751,7 @@
|
|||
[req session-agent events options]
|
||||
(let [[session-agent is-new-session] (if session-agent
|
||||
[session-agent false]
|
||||
[(create-session-agent req events options) true])
|
||||
[(create-session-agent! req events options) true])
|
||||
;; maps contains whatever the messages to the server
|
||||
maps (get-maps req)]
|
||||
;; if maps were received in this request, we should forward to listeners
|
||||
|
@ -761,7 +761,7 @@
|
|||
session-agent)
|
||||
(doseq [m maps]
|
||||
(let [decoded (decode-map m)]
|
||||
(notify-listeners (:id @session-agent) req :map decoded))))
|
||||
(notify-listeners! (:id @session-agent) req :map decoded))))
|
||||
(if is-new-session
|
||||
;; first post after a new session is a message with the session
|
||||
;; details.
|
||||
|
|
|
@ -163,7 +163,7 @@
|
|||
back-resp (app (->new-backchannel-request session-id))]
|
||||
(wait-for-agent-send-offs)
|
||||
; 3. send data to client along backchannel
|
||||
(send-data session-id {:foo "bar"})
|
||||
(send-data! session-id {:foo "bar"})
|
||||
(wait-for-agent-send-offs)
|
||||
; 4. get async response so far from the backchannel
|
||||
(let [async-resp @async-output
|
||||
|
@ -186,8 +186,8 @@
|
|||
back-resp (app (->new-backchannel-request session-id))]
|
||||
(wait-for-agent-send-offs)
|
||||
; 3. send data to client along backchannel
|
||||
(send-data session-id {:foo "bar"})
|
||||
(send-data session-id "hello, world")
|
||||
(send-data! session-id {:foo "bar"})
|
||||
(send-data! session-id "hello, world")
|
||||
(wait-for-agent-send-offs)
|
||||
; 4. get async response so far from the backchannel
|
||||
(let [async-resp @async-output
|
||||
|
@ -210,7 +210,7 @@
|
|||
session-id (get-session-id create-resp)]
|
||||
(wait-for-agent-send-offs)
|
||||
; 2. send data to client. no backchannel yet, so it should be queued in a buffer
|
||||
(send-data session-id {:foo "bar"})
|
||||
(send-data! session-id {:foo "bar"})
|
||||
(wait-for-agent-send-offs)
|
||||
; 3. backchannel request (long request, async response).
|
||||
; queued buffer of messages is flushed to backchannel when it first opens
|
||||
|
@ -254,7 +254,7 @@
|
|||
(let [back-resp (app (->new-backchannel-request session-id))]
|
||||
(wait-for-agent-send-offs)
|
||||
; 6. send data to client along backchannel
|
||||
(send-data session-id {:foo "bar"})
|
||||
(send-data! session-id {:foo "bar"})
|
||||
(wait-for-agent-send-offs)
|
||||
; 7. get async response so far from the backchannel
|
||||
(let [async-resp @async-output
|
||||
|
@ -670,7 +670,7 @@
|
|||
queued-str (pr-str (encode-map str-to-send))]
|
||||
(wait-for-agent-send-offs)
|
||||
; 2. send data to client. no backchannel is active, so data will be queued in the buffer
|
||||
(send-data session-id str-to-send)
|
||||
(send-data! session-id str-to-send)
|
||||
(wait-for-agent-send-offs)
|
||||
; 3. forwardchannel request to send data from client to server
|
||||
(let [forward-resp (app (->new-forwardchannel-request session-id 0 42) :events events)
|
||||
|
@ -706,10 +706,10 @@
|
|||
; 3. queue up a bunch of data to be sent. no backchannel has been created yet,
|
||||
; so this will all sit in the buffer and get flushed once the backchannel
|
||||
; is opened
|
||||
(send-data session-id :first-queued)
|
||||
(send-data session-id :second-queued)
|
||||
(send-data session-id "fail")
|
||||
(send-data session-id :fourth-queued)
|
||||
(send-data! session-id :first-queued)
|
||||
(send-data! session-id :second-queued)
|
||||
(send-data! session-id "fail")
|
||||
(send-data! session-id :fourth-queued)
|
||||
; 4. backchannel request (long request, async response)
|
||||
(let [back-resp (app (->new-backchannel-request session-id) :options options)]
|
||||
(wait-for-agent-send-offs)
|
||||
|
|
Reference in a new issue