On first sidebar open render already-fetched hits

This commit is contained in:
Jeremy Dormitzer 2018-01-18 22:12:27 -05:00
parent d8f42b461c
commit 356cffabd3
No known key found for this signature in database
GPG Key ID: 04F17C0F5A32C320
2 changed files with 36 additions and 20 deletions

View File

@ -8,6 +8,7 @@
(enable-console-print!)
(def object-ids (atom []))
(def hits (atom []))
(defn url-path
"Returns a url without its protocol"
@ -22,9 +23,9 @@
(filter #(= (url-path (get % "url")) (url-path url)) hits)))
(defn sort-hits
"Sorts hits from hn.algolia.com by post date descending"
"Sorts hits from hn.algolia.com by points date descending"
[hits]
(sort-by #(get % "created_at_i") #(compare %2 %1) hits))
(sort-by #(get % "points") #(compare %2 %1) hits))
(defn total-num-comments
"Returns the total number of comments from some hits"
@ -43,18 +44,15 @@
(<!)
(first)
(.-url))
hits (-> url
(hn/fetch-submission)
(<!)
(filter-response url)
(sort-hits))
ids (map #(% "objectID") hits)
num-comments (total-num-comments hits)]
fetched-hits (-> url
(hn/fetch-submission)
(<!)
(filter-response url)
(sort-hits))
ids (map #(% "objectID") fetched-hits)
num-comments (total-num-comments fetched-hits)]
(reset! hits fetched-hits)
(reset! object-ids ids)
(-> js/browser
(.-runtime)
(.sendMessage (clj->js {:type "objectIds"
:ids @object-ids})))
(set-badge-text! (str num-comments)))))
(defn handle-browser-action [tab]
@ -65,8 +63,10 @@
(defn handle-message [msg sender respond]
(case (.-type msg)
"fetchItems" (channel->promise
(go (clj->js (<! (hn/fetch-items @object-ids)))))))
"hits" (channel->promise (go @hits))
"fetchItems" (do (log/debug "received fetch items message")
(channel->promise
(go (clj->js (<! (hn/fetch-items @object-ids))))))))
(-> js/browser
(.-tabs)

View File

@ -65,6 +65,7 @@
"Returns initial sidebar state"
[]
{:items ()
:hits ()
:depth []
:loading false})
@ -73,6 +74,7 @@
[msg state]
(case (:type msg)
:items (assoc state :items (:items msg))
:hits (assoc state :hits (:hits msg))
:loading (assoc state :loading (:loading msg))
state))
@ -81,8 +83,9 @@
[dispatch-message state]
(log/debug state)
(if (:loading state)
(let [current-item (get-in-items (:items state) (:depth state))]
(components/loader)
(map #(components/card (:title %)) (:hits state))
#_(let [current-item (get-in-items (:items state) (:depth state))]
(if (> (count current-item) 1)
(map #(components/card (:title %)) current-item)
()))))
@ -94,7 +97,9 @@
[$sidebar-dom]
(let [$container (dom/getElement "sidebarContent")]
(dom/removeChildren $container)
(apply dom/append $container $sidebar-dom)))
(if (seqable? $sidebar-dom)
(apply dom/append $container $sidebar-dom)
(dom/append $container $sidebar-dom))))
(defn run-render-loop
"Runs the model-update-view loop"
@ -116,6 +121,17 @@
(defn handle-close-button [e]
(.postMessage js/window.parent (clj->js {:type "closeSidebar"}) "*"))
(defn fetch-hits
"Fetch hits in the Algolia API matching the URL"
[]
(go (-> js/browser
(.-runtime)
(.sendMessage (clj->js {:type "hits"}))
(promise->channel)
(<!)
(array-seq)
((fn [hits] (map obj->clj hits))))))
(defn fetch-items
"Fetch items matching the URL"
[]
@ -135,10 +151,10 @@
(events/listen (dom/getElement "closeSidebar") "click" handle-close-button)
(let [initial-state (update-state {:type :loading :loading true} (model))]
(run-render-loop initial-state)
(go (-> (fetch-items)
(go (-> (fetch-hits)
(<!)
(#(update-state {:type :items
:items %} initial-state))
(#(update-state {:type :hits
:hits %} initial-state))
(#(update-state {:type :loading
:loading false} %))
(run-render-loop)))))