Skip to content

Commit 00e75ca

Browse files
committed
refactoring: less repetition
1 parent 2cf4f87 commit 00e75ca

File tree

3 files changed

+34
-45
lines changed

3 files changed

+34
-45
lines changed

Clojure-Websockets/src/clj/birdwatch/communicator/component.clj

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616
(start [component] (log/info "Starting Communicator Component")
1717
(let [{:keys [ch-recv send-fn ajax-post-fn ajax-get-or-ws-handshake-fn connected-uids]}
1818
(sente/make-channel-socket! {:packer packer :user-id-fn ws/user-id-fn})
19-
event-handler (ws/make-event-handler (:query channels) (:tweet-missing channels) (:register-percolation channels))
19+
event-handler (ws/make-handler (:query channels) (:tweet-missing channels) (:register-perc channels))
2020
chsk-router (sente/start-chsk-router! ch-recv event-handler)]
21-
(ws/run-percolation-matches-loop (:percolation-matches channels) send-fn connected-uids)
2221
(ws/run-users-count-loop send-fn connected-uids)
23-
(ws/run-tweet-stats-loop send-fn connected-uids (:tweet-count channels))
24-
(ws/run-missing-tweet-loop (:missing-tweet-found channels) send-fn)
25-
(ws/run-query-results-loop (:query-results channels) send-fn)
22+
(ws/send-loop (:perc-matches channels) (ws/perc-matches connected-uids send-fn))
23+
(ws/send-loop (:tweet-count channels) (ws/tweet-stats connected-uids send-fn))
24+
(ws/send-loop (:query-results channels) (ws/relay-msg :tweet/prev-chunk :result send-fn))
25+
(ws/send-loop (:missing-tweet-found channels) (ws/relay-msg :tweet/missing-tweet :tweet send-fn))
2626
(assoc component :ajax-post-fn ajax-post-fn
2727
:ajax-get-or-ws-handshake-fn ajax-get-or-ws-handshake-fn
2828
:chsk-router chsk-router)))
@@ -43,8 +43,8 @@
4343
:persistence (chan)
4444
:rt-persistence (chan)
4545
:tweet-count (chan)
46-
:register-percolation (chan)
47-
:percolation-matches (chan)))
46+
:register-perc (chan)
47+
:perc-matches (chan)))
4848
(stop [component] (log/info "Stop Communicator Channels Component")
4949
(assoc component :query nil :query-results nil :tweet-missing nil :missing-tweet-found nil
5050
:persistence nil :rt-persistence nil :tweet-count nil)))

Clojure-Websockets/src/clj/birdwatch/communicator/websockets.clj

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
(log/info "Connected:" (:remote-addr req) uid)
1414
uid))
1515

16-
(defn make-event-handler [query-chan tweet-missing-chan register-percolation-chan]
17-
"creates event handler function for the websocket connection"
16+
(defn make-handler [query-chan tweet-missing-chan register-percolation-chan]
17+
"create event handler function for the websocket connection"
1818
(fn [{:as ev-msg :keys [event ?reply-fn]}]
1919
(match event
2020
[:cmd/percolate params] (put! register-percolation-chan params)
@@ -23,39 +23,28 @@
2323
[:chsk/ws-ping] () ; currently just do nothing with ping (no logging either)
2424
:else (log/debug "Unmatched event:" (pp/pprint event)))))
2525

26-
(defn run-percolation-matches-loop [percolation-matches-chan chsk-send! connected-uids]
27-
"runs loop for delivering percolation matches to interested clients"
28-
(go-loop []
29-
(let [[t matches subscriptions] (<! percolation-matches-chan)]
30-
(doseq [uid (:any @connected-uids)]
31-
(when (contains? matches (get subscriptions uid))
32-
(chsk-send! uid [:tweet/new t]))))
33-
(recur)))
26+
(defn send-loop [channel f]
27+
"run loop, call f with message on channel"
28+
(go-loop [] (let [msg (<! channel)] (f msg)) (recur)))
29+
30+
(defn tweet-stats [uids chsk-send!]
31+
"send stats about number of indexed tweets to all connected clients"
32+
(fn [msg] (doseq [uid (:any @uids)] (chsk-send! uid [:stats/total-tweet-count msg]))))
33+
34+
(defn perc-matches [uids chsk-send!]
35+
"deliver percolation matches to interested clients"
36+
(fn [msg] (let [[t matches subscriptions] msg]
37+
(doseq [uid (:any @uids)]
38+
(when (contains? matches (get subscriptions uid))
39+
(chsk-send! uid [:tweet/new t]))))))
40+
41+
(defn relay-msg [msg-type msg-key chsk-send!]
42+
"send query result chunks back to client"
43+
(fn [msg] (chsk-send! (:uid msg) [msg-type (msg-key msg)])))
3444

3545
(defn run-users-count-loop [chsk-send! connected-uids]
3646
"runs loop for sending stats about number of connected users to all connected clients"
37-
(go-loop []
38-
(<! (timeout 2000))
47+
(go-loop [] (<! (timeout 2000))
3948
(let [uids (:any @connected-uids)]
4049
(doseq [uid uids] (chsk-send! uid [:stats/users-count (count uids)])))
4150
(recur)))
42-
43-
(defn run-tweet-stats-loop [chsk-send! uids tweet-count-chan]
44-
"runs loop for sending stats about number of indexed tweets to all connected clients"
45-
(go-loop []
46-
(let [tweet-count (<! tweet-count-chan)]
47-
(doseq [uid (:any @uids)] (chsk-send! uid [:stats/total-tweet-count tweet-count])))
48-
(recur)))
49-
50-
(defn run-missing-tweet-loop [missing-tweet-found-chan chsk-send!]
51-
"runs loop for sending missing tweet back to client"
52-
(go-loop [] (let [msg (<! missing-tweet-found-chan)]
53-
(chsk-send! (:uid msg) [:tweet/missing-tweet (:tweet msg)]))
54-
(recur)))
55-
56-
(defn run-query-results-loop [query-results-chan chsk-send!]
57-
"runs loop for sending query result chunks back to client"
58-
(go-loop []
59-
(let [res (<! query-results-chan)]
60-
(chsk-send! (:uid res) [:tweet/prev-chunk (:result res)]))
61-
(recur)))

Clojure-Websockets/src/clj/birdwatch/switchboard.clj

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
[com.stuartsierra.component :as component]
66
[clojure.core.async :as async :refer [chan mult tap pipe]]))
77

8-
;; This component is the central switchboard for information flow in this application.
9-
;; The individual channel components come together like wiring harnesses in a car. One for the engine,
10-
;; one for the AC, one for the soundsystem and so on.
8+
;;;; This component is the central switchboard for information flow in this application.
9+
;;;; The individual channel components come together like wiring harnesses in a car. One for the engine,
10+
;;;; one for the AC, one for the soundsystem and so on.
1111

1212
(defrecord Switchboard [comm-chans tc-chans pers-chans perc-chans]
1313
component/Lifecycle
@@ -16,10 +16,10 @@
1616
(tap tweets-mult (:percolation perc-chans)) ; Tweets are distributed to multiple channels
1717
(tap tweets-mult (:persistence pers-chans)) ; through tapping the mult created above
1818
(tap tweets-mult (:rt-persistence pers-chans))
19-
;; connect channels 1 on 1. here, it would be easy to add message logging
19+
;; Connect channels 1 on 1. Here, it would be easy to add message logging.
2020
(pipe (:tweet-count pers-chans) (:tweet-count comm-chans))
21-
(pipe (:register-percolation comm-chans) (:register-percolation perc-chans))
22-
(pipe (:percolation-matches perc-chans) (:percolation-matches comm-chans))
21+
(pipe (:register-perc comm-chans) (:register-percolation perc-chans))
22+
(pipe (:percolation-matches perc-chans) (:perc-matches comm-chans))
2323
(pipe (:tweet-missing comm-chans) (:tweet-missing pers-chans))
2424
(pipe (:missing-tweet-found pers-chans) (:missing-tweet-found comm-chans))
2525
(pipe (:query comm-chans) (:query pers-chans))

0 commit comments

Comments
 (0)