|
1 | 1 | (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]]) |
3 | 3 | (:require [om.core :as om :include-macros true]
|
4 |
| - [om.dom :as dom :include-macros true] |
5 | 4 | [cljs-om.util :as util]
|
6 | 5 | [cljs-om.timeseries :as ts]
|
7 |
| - [cljs-om.ajax :as ajax] |
| 6 | + [cljs-om.tweets :as tweets] |
8 | 7 | [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]])) |
16 | 9 |
|
17 | 10 | (enable-console-print!)
|
18 | 11 |
|
|
23 | 16 | (om/root ui/search-view app-state {:target (. js/document (getElementById "search"))})
|
24 | 17 | (om/root ui/sort-buttons-view app-state {:target (. js/document (getElementById "sort-buttons"))})
|
25 | 18 |
|
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 |
| - |
55 | 19 | (def cloud-elem (. js/document (getElementById "wordCloud")))
|
56 | 20 | (def cloud-w (aget cloud-elem "offsetWidth"))
|
57 | 21 | (def word-cloud (.WordCloud js/BirdWatch cloud-w (* cloud-w 0.7) 250 (fn [e]) "#wordCloud"))
|
58 | 22 |
|
59 | 23 | (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) |
60 | 25 |
|
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)) |
77 | 27 | (def prev-tweets-chan (chan 100000))
|
78 | 28 | (def combined-tweets-chan (chan 1))
|
79 | 29 |
|
| 30 | +(defn fwd [from to ms] |
| 31 | + (go-loop [] |
| 32 | + (put! to (<! from)) |
| 33 | + (<! (timeout ms)) |
| 34 | + (recur))) |
80 | 35 |
|
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) |
90 | 38 |
|
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)) |
94 | 40 |
|
95 | 41 | (def ajax-results-chan (chan))
|
96 | 42 | (go-loop []
|
|
100 | 46 | (<! (timeout 1000))
|
101 | 47 | (recur)))
|
102 | 48 |
|
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) |
0 commit comments