From e01876ee47115982b87308bb39a4c8829ff4f714 Mon Sep 17 00:00:00 2001 From: Jeremy Dormitzer Date: Wed, 10 Jan 2018 18:47:33 -0500 Subject: [PATCH] Use lein instead of custom build --- project.clj | 17 ++++++++-- src/looped_in/background.cljs | 40 ++++++++++++++++++++++++ {src_cljs => src}/looped_in/content.cljs | 27 +++++++--------- {src_cljs => src}/looped_in/logging.cljs | 0 src/looped_in/promises.cljs | 21 +++++++++++++ src_clj/looped_in/build.clj | 12 ------- src_cljs/looped_in/background.cljs | 14 --------- 7 files changed, 86 insertions(+), 45 deletions(-) create mode 100644 src/looped_in/background.cljs rename {src_cljs => src}/looped_in/content.cljs (68%) rename {src_cljs => src}/looped_in/logging.cljs (100%) create mode 100644 src/looped_in/promises.cljs delete mode 100644 src_clj/looped_in/build.clj delete mode 100644 src_cljs/looped_in/background.cljs diff --git a/project.clj b/project.clj index 35354be..ee068ac 100644 --- a/project.clj +++ b/project.clj @@ -6,7 +6,18 @@ :dependencies [[org.clojure/clojure "1.8.0"] [org.clojure/clojurescript "1.9.946"] [org.clojure/core.async "0.3.465"] + [org.clojure/core.match "0.3.0-alpha5"] [cljs-ajax "0.7.3"]] - :source-paths ["src_clj"] - :main looped-in.build - :aliases {"build" ["run"]}) + :plugins [[lein-cljsbuild "1.1.7"]] + :cljsbuild {:builds [{:source-paths ["src"] + :compiler {:optimizations :simple + :source-map true + :pretty-print true + :output-dir "ext/js/generated/out" + :modules + {:content + {:output-to "ext/js/generated/content.js" + :entries #{"looped-in.content"}} + :background + {:output-to "ext/js/generated/background.js" + :entries #{"looped-in.background"}}}}}]}) diff --git a/src/looped_in/background.cljs b/src/looped_in/background.cljs new file mode 100644 index 0000000..e8dda52 --- /dev/null +++ b/src/looped_in/background.cljs @@ -0,0 +1,40 @@ +(ns looped-in.background + (:require [clojure.core.match :refer [match]] + [cljs.core.async :refer [go chan >! promise]])) + +(enable-console-print!) + +(def browser-action (.-browserAction js/browser)) + +(defn fetch-submission + "Fetches submissions from Hacker News by `url`" + [url] + (let [response-chan (chan)] + (GET "https://hn.algolia.com/api/v1/search" + {:params {"query" url + "hitsPerPage" 1000 + "restrictSearchableAttributes" "url"} + :handler (fn [res] (go (>! response-chan res))) + :error-handler (fn [err] (log/error "Error fetching HN stories:" + (clj->js err)))}) + response-chan)) + +(defn handle-message + "Handles messages from the content script" + [msg] + (match (.-type msg) + "fetchData" (channel->promise (fetch-submission (.-url msg))) + x (log/warn "Ignoring unknown message type" x))) + +(-> js/browser + (.-runtime) + (.-onMessage) + (.addListener handle-message + #_(fn [msg] + (prn msg) + (.setBadgeBackgroundColor browser-action #js {:color "#232323"}) + (.setBadgeText browser-action + #js {:text (str (.-numComments msg))})))) diff --git a/src_cljs/looped_in/content.cljs b/src/looped_in/content.cljs similarity index 68% rename from src_cljs/looped_in/content.cljs rename to src/looped_in/content.cljs index 79485f8..883f712 100644 --- a/src_cljs/looped_in/content.cljs +++ b/src/looped_in/content.cljs @@ -1,24 +1,12 @@ (ns looped-in.content (:require-macros [cljs.core.async.macros :refer [go]]) (:require [ajax.core :refer [GET]] - [cljs.core.async :as async :refer [chan !]] - [looped-in.logging :as log])) + [cljs.core.async :as async :refer [go channel]])) (enable-console-print!) -(defn fetch-submission - "Fetches submissions from Hacker News by `url`" - [url] - (let [response-chan (chan)] - (GET "https://hn.algolia.com/api/v1/search" - {:params {"query" url - "hitsPerPage" 1000 - "restrictSearchableAttributes" "url"} - :handler (fn [res] (go (>! response-chan res))) - :error-handler (fn [err] (log/error "Error fetching HN stories:" - (clj->js err)))}) - response-chan)) - (defn filter-response "Filters a response from hn.algolia.com to give just the relevant results" ;; TODO implement a cache by URL and timestamp that caches the results of the @@ -47,9 +35,16 @@ (.-runtime) (.sendMessage #js {:numComments num-comments})))) -(let [current-url (-> js/window (.-location) (.-href)) +#_(let [current-url (-> js/window (.-location) (.-href)) sub-chan (fetch-submission current-url)] (go (->> ( js/window (.-location) (.-href)) + results-chan (-> js/browser + (.-runtime) + (.sendMessage #js {:type "fetchData" :url current-url}) + (promise->channel))] + (go (log/debug "results:" (! promise + "Returns a Promise that resolves by taking from the channel" + [channel] + (js/Promise. (fn [resolve] + (go (let [result (clj->js (channel + "Returns a channel that is put into when the Promise resolves" + [p] + (let [channel (chan)] + (-> p + (.then (fn [result] + (go (>! channel result)))) + (.err (fn [err] + (log/error "Error resolving Promise:" err)))) + channel)) diff --git a/src_clj/looped_in/build.clj b/src_clj/looped_in/build.clj deleted file mode 100644 index 3fc3ac1..0000000 --- a/src_clj/looped_in/build.clj +++ /dev/null @@ -1,12 +0,0 @@ -(ns looped-in.build - (:require [cljs.build.api :as cljs])) - -(defn -main [& args] - (cljs/build "src_cljs" {:optimizations :simple - :source-map true - :pretty-print true - :output-dir "ext/js/generated/out" - :modules {:content {:output-to "ext/js/generated/content.js" - :entries #{"looped-in.content"}} - :background {:output-to "ext/js/generated/background.js" - :entries #{"looped-in.background"}}}})) diff --git a/src_cljs/looped_in/background.cljs b/src_cljs/looped_in/background.cljs deleted file mode 100644 index bce6204..0000000 --- a/src_cljs/looped_in/background.cljs +++ /dev/null @@ -1,14 +0,0 @@ -(ns looped-in.background) - -(enable-console-print!) - -(def browser-action (.-browserAction js/browser)) - -(-> js/browser - (.-runtime) - (.-onMessage) - (.addListener (fn [msg] - (prn msg) - (.setBadgeBackgroundColor browser-action #js {:color "#232323"}) - (.setBadgeText browser-action - #js {:text (str (.-numComments msg))}))))