From 356cffabd3493232f99e04aabdf957fffe52d14f Mon Sep 17 00:00:00 2001 From: Jeremy Dormitzer Date: Thu, 18 Jan 2018 22:12:27 -0500 Subject: [PATCH] On first sidebar open render already-fetched hits --- src/looped_in/background.cljs | 30 +++++++++++++++--------------- src/looped_in/sidebar.cljs | 26 +++++++++++++++++++++----- 2 files changed, 36 insertions(+), 20 deletions(-) diff --git a/src/looped_in/background.cljs b/src/looped_in/background.cljs index 63c0654..f71d90a 100644 --- a/src/looped_in/background.cljs +++ b/src/looped_in/background.cljs @@ -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 @@ ( url - (hn/fetch-submission) - ( url + (hn/fetch-submission) + ( 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 (promise (go @hits)) + "fetchItems" (do (log/debug "received fetch items message") + (channel->promise + (go (clj->js ( js/browser (.-tabs) diff --git a/src/looped_in/sidebar.cljs b/src/looped_in/sidebar.cljs index f5dabc1..d2b22b7 100644 --- a/src/looped_in/sidebar.cljs +++ b/src/looped_in/sidebar.cljs @@ -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) + (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) (