|
7 | 7 | (enable-console-print!)
|
8 | 8 |
|
9 | 9 | (defn sort-by [key]
|
| 10 | + "sorting function, initially comparing specified key and, if equal, favors higher ID" |
10 | 11 | (fn [x y]
|
11 | 12 | (if (not (= (key x) (key y)))
|
12 | 13 | (> (key x) (key y))
|
13 | 14 | (> (:id x) (:id y)))))
|
14 | 15 |
|
15 |
| -(def app-state (atom {:count 0 |
16 |
| - :tweets-map {} |
17 |
| - :rt-since-startup {} |
18 |
| - :by-followers (sorted-set-by (sort-by :followers_count)) |
19 |
| - :by-retweets (sorted-set-by (sort-by :retweet_count)) |
20 |
| - :by-rt-since-startup (sorted-set-by (sort-by :count)) |
21 |
| - :by-favorites (sorted-set-by (sort-by :favorite_count)) |
22 |
| - :by-id (sorted-set-by >) |
23 |
| - :n 10 |
24 |
| - :sorted :by-followers})) |
| 16 | +(def initial-state {:count 0 |
| 17 | + :tweets-map {} |
| 18 | + :rt-since-startup {} |
| 19 | + :by-followers (sorted-set-by (sort-by :followers_count)) |
| 20 | + :by-retweets (sorted-set-by (sort-by :retweet_count)) |
| 21 | + :by-rt-since-startup (sorted-set-by (sort-by :count)) |
| 22 | + :by-favorites (sorted-set-by (sort-by :favorite_count)) |
| 23 | + :by-id (sorted-set-by >) |
| 24 | + :n 10 |
| 25 | + :sorted :by-followers |
| 26 | + :search "*" |
| 27 | + :stream nil}) |
25 | 28 |
|
26 |
| -(om/root |
27 |
| - ui/tweets-view |
28 |
| - app-state |
29 |
| - {:target (. js/document (getElementById "tweet-frame"))}) |
| 29 | +(def app-state (atom initial-state)) |
30 | 30 |
|
31 |
| -(om/root |
32 |
| - ui/count-view |
33 |
| - app-state |
34 |
| - {:target (. js/document (getElementById "tweet-count"))}) |
35 |
| - |
36 |
| -(om/root |
37 |
| - ui/sort-buttons-view |
38 |
| - app-state |
39 |
| - {:target (. js/document (getElementById "sort-buttons"))}) |
| 31 | +(om/root ui/tweets-view app-state {:target (. js/document (getElementById "tweet-frame"))}) |
| 32 | +(om/root ui/count-view app-state {:target (. js/document (getElementById "tweet-count"))}) |
| 33 | +(om/root ui/search-view app-state {:target (. js/document (getElementById "search"))}) |
| 34 | +(om/root ui/sort-buttons-view app-state {:target (. js/document (getElementById "sort-buttons"))}) |
40 | 35 |
|
41 | 36 | (defn add-to-tweets-map [tweet]
|
42 |
| - (swap! app-state assoc-in [:tweets-map (keyword (:id_str tweet))] (util/format-tweet tweet))) |
| 37 | + (swap! app-state assoc-in [:tweets-map (keyword (:id_str tweet))] (util/format-tweet tweet))) |
43 | 38 |
|
44 | 39 | (defn mod-sort-set [app-key fun set-key val rt]
|
45 | 40 | (swap! app-state assoc app-key (fun (app-key @app-state) {set-key val :id (:id_str rt)})))
|
|
48 | 43 | "handles original, retweeted tweet"
|
49 | 44 | (if (contains? tweet :retweeted_status)
|
50 | 45 | (let [rt (:retweeted_status tweet)
|
51 |
| - prev ((keyword (:id_str rt)) (:tweets-map @app-state)) |
52 |
| - prev-rt-count ((keyword (:id_str rt)) (:rt-since-startup @app-state))] |
| 46 | + rt-id (keyword (:id_str rt)) |
| 47 | + prev (rt-id (:tweets-map @app-state)) |
| 48 | + prev-rt-count (rt-id (:rt-since-startup @app-state))] |
53 | 49 | (if (not (nil? prev))
|
54 | 50 | (do
|
55 | 51 | (mod-sort-set :by-retweets disj :retweet_count (:retweet_count prev) rt)
|
|
58 | 54 | (do
|
59 | 55 | (if (not (nil? prev-rt-count))
|
60 | 56 | (mod-sort-set :by-rt-since-startup disj :count prev-rt-count rt))
|
61 |
| - (swap! app-state assoc-in [:rt-since-startup (keyword (:id_str rt))] |
62 |
| - (inc ((keyword (:id_str rt)) (:rt-since-startup @app-state)))) |
63 |
| - (mod-sort-set :by-rt-since-startup conj :count ((keyword (:id_str rt)) (:rt-since-startup @app-state)) rt))) |
| 57 | + (swap! app-state assoc-in [:rt-since-startup rt-id] |
| 58 | + (inc (rt-id (:rt-since-startup @app-state)))) |
| 59 | + (mod-sort-set :by-rt-since-startup conj :count (rt-id (:rt-since-startup @app-state)) rt))) |
64 | 60 | (add-to-tweets-map rt)
|
65 | 61 | (mod-sort-set :by-retweets conj :retweet_count (:retweet_count rt) rt)
|
66 | 62 | (mod-sort-set :by-favorites conj :favorite_count (:favorite_count rt) rt))))
|
|
80 | 76 | (let [tweet (js->clj (JSON/parse (.-data e)) :keywordize-keys true)]
|
81 | 77 | (add-tweet tweet)))
|
82 | 78 |
|
83 |
| -(def stream (js/EventSource. "/tweetFeed?q=*")) |
84 |
| -(.addEventListener stream |
85 |
| - "message" |
86 |
| - (fn [e] (receive-sse e)) |
87 |
| - false) |
| 79 | +(defn start-search [search] |
| 80 | + "initiate new search by starting SSE stream" |
| 81 | + (let [s (if (= search "") "*" search)] |
| 82 | + (if (not (nil? (:stream @app-state))) (.close (:stream @app-state))) |
| 83 | + (reset! app-state initial-state) |
| 84 | + (swap! app-state assoc :search s) |
| 85 | + (aset js/window "location" "hash" (js/encodeURIComponent s)) |
| 86 | + (swap! app-state assoc :stream (js/EventSource. (str "/tweetFeed?q=" s))) |
| 87 | + (.addEventListener (:stream @app-state) "message" (fn [e] (receive-sse e)) false))) |
| 88 | + |
| 89 | +(start-search (subs (js/decodeURIComponent (aget js/window "location" "hash")) 2)) |
0 commit comments