Skip to content

Commit 0c2b77d

Browse files
committed
tweets handling in separate namespace
1 parent eddda8c commit 0c2b77d

File tree

13 files changed

+496
-573
lines changed

13 files changed

+496
-573
lines changed

cljs-om/src/cljs_om/ajax.cljs

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
(ns cljs-om.ajax
2-
(:require-macros [cljs.core.async.macros :refer [go go-loop]])
32
(:require [goog.events :as events]
4-
[cljs.core.async :as async
5-
:refer [<! >! chan close! sliding-buffer put! alts! timeout]])
3+
[cljs.core.async :as async :refer [put!]])
64
(:import [goog.net XhrIo]
75
goog.net.EventType
86
[goog.events EventType]))
97

10-
118
(defn error-handler [err] (print err))
129
(defn handler [payload]
1310
(put! ajax-results-chan payload))

cljs-om/src/cljs_om/core.cljs

+14-90
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
11
(ns cljs-om.core
2-
(:require-macros [cljs.core.async.macros :refer [go go-loop]])
2+
(:require-macros [cljs.core.async.macros :refer [go-loop]])
33
(:require [om.core :as om :include-macros true]
4-
[om.dom :as dom :include-macros true]
54
[cljs-om.util :as util]
65
[cljs-om.timeseries :as ts]
7-
[cljs-om.ajax :as ajax]
6+
[cljs-om.tweets :as tweets]
87
[cljs-om.ui :as ui]
9-
[goog.events :as events]
10-
[cljs-om.wordcount :as wc]
11-
[cljs.core.async :as async
12-
:refer [<! >! chan close! sliding-buffer put! alts! timeout]])
13-
(:import [goog.net XhrIo]
14-
goog.net.EventType
15-
[goog.events EventType]))
8+
[cljs.core.async :as async :refer [<! chan put! alts! timeout]]))
169

1710
(enable-console-print!)
1811

@@ -23,74 +16,27 @@
2316
(om/root ui/search-view app-state {:target (. js/document (getElementById "search"))})
2417
(om/root ui/sort-buttons-view app-state {:target (. js/document (getElementById "sort-buttons"))})
2518

26-
(defn add-to-tweets-map [tweets-map tweet]
27-
(swap! app-state assoc-in [tweets-map (keyword (:id_str tweet))] (util/format-tweet tweet)))
28-
29-
(defn mod-sort-set [app-key fun set-key val rt]
30-
(swap! app-state assoc app-key (fun (app-key @app-state) {set-key val :id (:id_str rt)})))
31-
32-
(defn add-rt-status [tweet]
33-
"handles original, retweeted tweet"
34-
(if (contains? tweet :retweeted_status)
35-
(let [state @app-state
36-
rt (:retweeted_status tweet)
37-
rt-id (keyword (:id_str rt))
38-
prev (rt-id (:retweets state))
39-
prev-rt-count (rt-id (:rt-since-startup state))]
40-
(when (not (nil? prev))
41-
(mod-sort-set :by-retweets disj :retweet_count (:retweet_count prev) rt)
42-
(mod-sort-set :by-favorites disj :favorite_count (:favorite_count prev) rt))
43-
(if (not (nil? prev-rt-count))
44-
(mod-sort-set :by-rt-since-startup disj :count prev-rt-count rt))
45-
(swap! app-state assoc-in [:rt-since-startup rt-id]
46-
(inc prev-rt-count))
47-
(mod-sort-set :by-rt-since-startup conj :count (inc prev-rt-count) rt)
48-
(if (> (:retweet_count rt) (:retweet_count prev))
49-
(swap! app-state assoc-in [:retweets (keyword (:id_str rt))] (util/format-tweet rt))
50-
(swap! app-state assoc-in [:retweets :latest] rt)
51-
)
52-
(mod-sort-set :by-retweets conj :retweet_count (:retweet_count rt) rt)
53-
(mod-sort-set :by-favorites conj :favorite_count (:favorite_count rt) rt))))
54-
5519
(def cloud-elem (. js/document (getElementById "wordCloud")))
5620
(def cloud-w (aget cloud-elem "offsetWidth"))
5721
(def word-cloud (.WordCloud js/BirdWatch cloud-w (* cloud-w 0.7) 250 (fn [e]) "#wordCloud"))
5822

5923
(js/setInterval #(ts/update ts/graph-with-legend) 5000)
24+
(js/setInterval #(.updateBarchart js/BirdWatch (clj->js (take 25 (:words-sorted-by-count @app-state)))) 1000)
6025

61-
(defn add-tweet [tweet]
62-
"increment counter, add tweet to tweets map and to sorted sets by id and by followers"
63-
(let [state @app-state]
64-
(swap! app-state assoc :count (inc (:count state)))
65-
(add-to-tweets-map :tweets-map tweet)
66-
(add-rt-status tweet)
67-
(wc/process-tweet app-state (:text tweet))
68-
(swap! app-state assoc :by-followers (conj (:by-followers state)
69-
{:followers_count (:followers_count (:user tweet))
70-
:id (:id_str tweet)}))
71-
(swap! app-state assoc :by-id (conj (:by-id state) (:id_str tweet)))
72-
(. word-cloud (redraw (clj->js (take 250 (:words-sorted-by-count state)))))
73-
; (.updateBarchart js/BirdWatch (clj->js (take 25 (:words-sorted-by-count state))))
74-
))
75-
76-
(def tweets-chan (chan 100000))
26+
(def tweets-chan (chan 10000))
7727
(def prev-tweets-chan (chan 100000))
7828
(def combined-tweets-chan (chan 1))
7929

30+
(defn fwd [from to ms]
31+
(go-loop []
32+
(put! to (<! from))
33+
(<! (timeout ms))
34+
(recur)))
8035

81-
(go-loop []
82-
(put! combined-tweets-chan (<! tweets-chan))
83-
(<! (timeout 0))
84-
(recur))
85-
86-
(go-loop []
87-
(put! combined-tweets-chan (<! prev-tweets-chan))
88-
(<! (timeout 10))
89-
(recur))
36+
(fwd tweets-chan combined-tweets-chan 0)
37+
(fwd prev-tweets-chan combined-tweets-chan 10)
9038

91-
(go-loop []
92-
(add-tweet (<! combined-tweets-chan))
93-
(recur))
39+
(go-loop [] (tweets/add-tweet (<! combined-tweets-chan) app-state word-cloud) (recur))
9440

9541
(def ajax-results-chan (chan))
9642
(go-loop []
@@ -100,26 +46,4 @@
10046
(<! (timeout 1000))
10147
(recur)))
10248

103-
(defn receive-sse [e]
104-
"callback, called for each item (tweet) received by SSE stream"
105-
(let [tweet (js->clj (JSON/parse (.-data e)) :keywordize-keys true)]
106-
(put! tweets-chan tweet)))
107-
108-
(defn start-search [search]
109-
"initiate new search by starting SSE stream"
110-
(let [s (if (= search "") "*" search)]
111-
(if (not (nil? (:stream @app-state))) (.close (:stream @app-state)))
112-
(reset! app-state (util/initial-state))
113-
(swap! app-state assoc :search s)
114-
(aset js/window "location" "hash" (js/encodeURIComponent s))
115-
(swap! app-state assoc :stream (js/EventSource. (str "/tweetFeed?q=" s)))
116-
(.addEventListener (:stream @app-state) "message" (fn [e] (receive-sse e)) false)
117-
(ajax/prev-search "*" 500 0 ajax-results-chan)
118-
(ajax/prev-search "*" 500 500 ajax-results-chan)
119-
(ajax/prev-search "*" 500 1000 ajax-results-chan)
120-
(ajax/prev-search "*" 500 1500 ajax-results-chan)
121-
(ajax/prev-search "*" 500 2000 ajax-results-chan)
122-
))
123-
124-
(start-search (util/search-hash))
125-
49+
(tweets/start-search app-state (util/search-hash) tweets-chan ajax-results-chan)

cljs-om/src/cljs_om/tweets.cljs

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
(ns cljs-om.tweets
2+
(:require [cljs-om.util :as util]
3+
[cljs-om.timeseries :as ts]
4+
[cljs-om.ajax :as ajax]
5+
[cljs-om.ui :as ui]
6+
[cljs-om.wordcount :as wc]
7+
[cljs.core.async :as async :refer [put!]]))
8+
9+
(enable-console-print!)
10+
11+
(defn add-to-tweets-map [app tweets-map tweet]
12+
(swap! app assoc-in [tweets-map (keyword (:id_str tweet))] (util/format-tweet tweet)))
13+
14+
(defn mod-sort-set [app app-key fun set-key val rt]
15+
(swap! app assoc app-key (fun (app-key @app) {set-key val :id (:id_str rt)})))
16+
17+
(defn add-rt-status [app tweet]
18+
"handles original, retweeted tweet"
19+
(if (contains? tweet :retweeted_status)
20+
(let [state @app
21+
rt (:retweeted_status tweet)
22+
rt-id (keyword (:id_str rt))
23+
prev (rt-id (:retweets state))
24+
prev-rt-count (rt-id (:rt-since-startup state))]
25+
(when (not (nil? prev))
26+
(mod-sort-set app :by-retweets disj :retweet_count (:retweet_count prev) rt)
27+
(mod-sort-set app :by-favorites disj :favorite_count (:favorite_count prev) rt))
28+
(if (not (nil? prev-rt-count))
29+
(mod-sort-set app :by-rt-since-startup disj :count prev-rt-count rt))
30+
(swap! app assoc-in [:rt-since-startup rt-id]
31+
(inc prev-rt-count))
32+
(mod-sort-set app :by-rt-since-startup conj :count (inc prev-rt-count) rt)
33+
(if (> (:retweet_count rt) (:retweet_count prev))
34+
(swap! app assoc-in [:retweets (keyword (:id_str rt))] (util/format-tweet rt))
35+
(swap! app assoc-in [:retweets :latest] rt)
36+
)
37+
(mod-sort-set app :by-retweets conj :retweet_count (:retweet_count rt) rt)
38+
(mod-sort-set app :by-favorites conj :favorite_count (:favorite_count rt) rt))))
39+
40+
(defn add-tweet [tweet app word-cloud]
41+
"increment counter, add tweet to tweets map and to sorted sets by id and by followers"
42+
(let [state @app]
43+
(swap! app assoc :count (inc (:count state)))
44+
(add-to-tweets-map app :tweets-map tweet)
45+
(add-rt-status app tweet)
46+
(wc/process-tweet app (:text tweet))
47+
(swap! app assoc :by-followers (conj (:by-followers state)
48+
{:followers_count (:followers_count (:user tweet))
49+
:id (:id_str tweet)}))
50+
(swap! app assoc :by-id (conj (:by-id state) (:id_str tweet)))
51+
(. word-cloud (redraw (clj->js (take 250 (:words-sorted-by-count state))))) ))
52+
53+
(defn receive-sse [tweets-chan e]
54+
"callback, called for each item (tweet) received by SSE stream"
55+
(let [tweet (js->clj (JSON/parse (.-data e)) :keywordize-keys true)]
56+
(put! tweets-chan tweet)))
57+
58+
(defn start-search [app search tweets-chan ajax-results-chan]
59+
"initiate new search by starting SSE stream"
60+
(let [s (if (= search "") "*" search)]
61+
(if (not (nil? (:stream @app))) (.close (:stream @app)))
62+
(reset! app (util/initial-state))
63+
(swap! app assoc :search s)
64+
(aset js/window "location" "hash" (js/encodeURIComponent s))
65+
(swap! app assoc :stream (js/EventSource. (str "/tweetFeed?q=" s)))
66+
(.addEventListener (:stream @app) "message" #(receive-sse tweets-chan %) false)
67+
(ajax/prev-search "*" 500 0 ajax-results-chan)
68+
(ajax/prev-search "*" 500 500 ajax-results-chan)
69+
(ajax/prev-search "*" 500 1000 ajax-results-chan)
70+
(ajax/prev-search "*" 500 1500 ajax-results-chan)
71+
(ajax/prev-search "*" 500 2000 ajax-results-chan)
72+
))

public/cljs/cljs_om.js

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/cljs/out/cljs_om/ajax.cljs

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
(ns cljs-om.ajax
2-
(:require-macros [cljs.core.async.macros :refer [go go-loop]])
32
(:require [goog.events :as events]
4-
[cljs.core.async :as async
5-
:refer [<! >! chan close! sliding-buffer put! alts! timeout]])
3+
[cljs.core.async :as async :refer [put!]])
64
(:import [goog.net XhrIo]
75
goog.net.EventType
86
[goog.events EventType]))
97

10-
118
(defn error-handler [err] (print err))
129
(defn handler [payload]
1310
(put! ajax-results-chan payload))

public/cljs/out/cljs_om/ajax.js

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/cljs/out/cljs_om/ajax.js.map

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)