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