Use lein instead of custom build
This commit is contained in:
parent
9868cc3612
commit
e01876ee47
17
project.clj
17
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"}}}}}]})
|
||||
|
40
src/looped_in/background.cljs
Normal file
40
src/looped_in/background.cljs
Normal 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))}))))
|
@ -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))))
|
21
src/looped_in/promises.cljs
Normal file
21
src/looped_in/promises.cljs
Normal 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))
|
@ -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"}}}}))
|
@ -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))}))))
|
Loading…
Reference in New Issue
Block a user