Use lein instead of custom build

This commit is contained in:
Jeremy Dormitzer 2018-01-10 18:47:33 -05:00
parent 9868cc3612
commit e01876ee47
No known key found for this signature in database
GPG Key ID: 04F17C0F5A32C320
7 changed files with 86 additions and 45 deletions

View File

@ -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"}}}}}]})

View File

@ -0,0 +1,40 @@
(ns looped-in.background
(:require [clojure.core.match :refer [match]]
[cljs.core.async :refer [go chan >! <!]]
[ajax.core :refer [GET]]
[looped-in.logging :as log]
[looped-in.promises :refer [channel->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))}))))

View File

@ -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 <!]]
[looped-in.logging :as log]
[looped-in.promises :refer [promise->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 (->> (<! sub-chan)
(filter-response current-url)
(sort-hits)
(handle-hits))))
(let [current-url (-> js/window (.-location) (.-href))
results-chan (-> js/browser
(.-runtime)
(.sendMessage #js {:type "fetchData" :url current-url})
(promise->channel))]
(go (log/debug "results:" (<! results-chan))))

View File

@ -0,0 +1,21 @@
(ns looped-in.promises
(:require [cljs.core.async :refer [go chan >! <!]]
[looped-in.logging :as log]))
(defn channel->promise
"Returns a Promise that resolves by taking from the channel"
[channel]
(js/Promise. (fn [resolve]
(go (let [result (clj->js (<! channel))]
(resolve result))))))
(defn promise->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))

View File

@ -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"}}}}))

View File

@ -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))}))))