diff --git a/src/looped_in/main.cljs b/src/looped_in/main.cljs index a846d5c..cb51492 100644 --- a/src/looped_in/main.cljs +++ b/src/looped_in/main.cljs @@ -21,13 +21,37 @@ (close! response-chan))}) response-chan)) -(defn url-path [url] +(defn fetch-item + "Fetches items from Hacker News by `id`" + [id] + (let [response-chan (chan)] + (GET (str "https://hn.algolia.com/api/v1/items/" id) + {:handler (fn [res] (go (>! response-chan res))) + :error-handler (fn [err] + (log/error (str "Error fetching item " id ":") err) + (close! response-chan))}) + response-chan)) + +(defn fetch-items-for-hits [hits] + (let [chans (map (fn [hit] + (prn (hit "objectID")) + (fetch-item (hit "objectID"))) + hits)] + (go-loop [[channel & rest] chans + acc []] + (if (nil? channel) + acc + (recur rest (conj acc ( js/browser (.-tabs) (.query #js {:active true - :currentWindow true})) - query-chan (promise->channel query-p) - tab (first (> response - (filter-response url) - (sort-hits)) + (go (let [url (-> js/browser + (.-tabs) + (.query #js {:active true :currentWindow true}) + (promise->channel) + ( url + (fetch-submission) + ( js/browser (.-tabs)