Skip to content

Commit

Permalink
Move to figwheel-main, headless chrome, fix js mistakes
Browse files Browse the repository at this point in the history
  • Loading branch information
oliyh committed Jul 25, 2019
1 parent 3aca887 commit 8ebc6cd
Show file tree
Hide file tree
Showing 13 changed files with 120 additions and 79 deletions.
28 changes: 10 additions & 18 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,22 @@ version: 2
jobs:
build:
docker:
- image: clojure:lein-2.8.1
- image: circleci/clojure:lein-2.9.0-browsers
steps:
- run: which java
- run: lein --version

- checkout

# Download and cache dependencies
- restore_cache:
key: v2-phantomjs-1.9.8
keys:
- v1-dependencies-{{ checksum "project.clj" }}
# fallback to using the latest cache if no exact match is found
- v1-dependencies-

- run:
name: Install phantomjs
command: |
if ! [ $(which phantomjs) ]; then
wget https://github.com/Medium/phantomjs/releases/download/v1.9.19/phantomjs-1.9.8-linux-x86_64.tar.bz2
tar xvjf phantomjs-1.9.8-linux-x86_64.tar.bz2
mv phantomjs-1.9.8-linux-x86_64/bin/phantomjs /usr/local/bin/phantomjs
fi
chmod ugo+x /usr/local/bin/phantomjs
- run: lein deps

- save_cache:
key: v2-phantomjs-1.9.8
paths:
- /usr/local/bin/phantomjs

- run: phantomjs --version
- ~/.m2
key: v1-dependencies-{{ checksum "project.clj" }}

- run: lein test
4 changes: 4 additions & 0 deletions .dir-locals.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
((nil
(cider-preferred-build-tool . "lein")
(cider-default-cljs-repl . figwheel-main)
(cider-figwheel-main-default-options . "dev")))
9 changes: 2 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,9 @@ All function/event signatures now take an optional instance-name as the first ar

## Development

`cider-jack-in-cljs` and choose figwheel
`cider-jack-in-clj&cljs`

```clojure
user> (dev)
;; visit http://localhost:3449/devcards/index.html
dev> (cljs)
cljs.user>
```
CLJS tests are available at http://localhost:9500/figwheel-extra-main/auto-testing

[![CircleCI](https://circleci.com/gh/oliyh/re-graph.svg?style=svg)](https://circleci.com/gh/oliyh/re-graph)

Expand Down
7 changes: 7 additions & 0 deletions dev-resources/public/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!DOCTYPE html>
<html>
<body>
<h1>Test host page</h1>
<script src="cljs-out/test-main.js" type="text/javascript"></script>
</body>
</html>
9 changes: 9 additions & 0 deletions dev.cljs.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
^{:watch-dirs ["test" "src"]
:open-url false
:auto-testing true}
{:main "re-graph.core"
:output-to "target/public/re-graph.js"
:preloads [devtools.preload]
:external-config {:devtools/config {:features-to-install [:formatters :hints]
:fn-symbol "F"
:print-config-overrides true}}}
41 changes: 9 additions & 32 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -15,46 +15,23 @@
[cljs-http "0.1.45"]
[clj-http "3.9.1"]
[stylefruits/gniazdo "1.1.1"]
[cheshire "5.8.1"]]
:plugins [[lein-cljsbuild "1.1.7"]
[lein-doo "0.1.10"]
[lein-figwheel "0.5.14"]]
[cheshire "5.8.1"]
[org.clojure/tools.logging "0.4.1"]]
:profiles {:provided {:dependencies [[org.clojure/clojure "1.9.0"]
[org.clojure/clojurescript "1.10.439"]]}
:dev {:source-paths ["dev"]
:resource-paths ["dev-resources"]
:exclusions [[org.clojure/tools.reader]]
:dependencies [[org.clojure/tools.reader "1.2.2"]
[figwheel-sidecar "0.5.16"]
[binaryage/devtools "0.9.10"]
[devcards "0.2.4"]
[day8.re-frame/test "0.1.5"]
[lein-doo "0.1.10"]
[com.bhauman/figwheel-main "0.2.1"]

;; gh-pages deploy
[leiningen-core "2.8.1"]]
:repl-options {:init-ns user
;:nrepl-middleware [cider.piggieback/wrap-cljs-repl]
}}}
:aliases {"test" ["do" ["clean"] ["test"] ["doo" "phantom" "test" "once"]]
"build-pages" ["do"
["run" "-m" "pages/build"]
["cljsbuild" "once" "pages"]]
"deploy-pages" ["run" "-m" "pages/push"]}
:cljsbuild {:builds [{:id "devcards"
:figwheel {:devcards true}
:source-paths ["src" "test"]
:compiler {:preloads [devtools.preload]
:main "re-graph.all-tests"
:asset-path "js/devcards"
:output-to "dev-resources/public/devcards/js/devcards.js"
:output-dir "dev-resources/public/devcards/js/devcards"
:source-map-timestamp true
:parallel-build true}}

{:id "test"
:source-paths ["src" "test"]
:compiler {:output-to "target/unit-test.js"
:main "re-graph.runner"
:optimizations :whitespace
:parallel-build true}}]})
:repl-options {:init-ns user}}}
:aliases {"fig" ["trampoline" "run" "-m" "figwheel.main"]
"fig:build" ["trampoline" "run" "-m" "figwheel.main" "-b" "dev" "-r"]
"fig:min" ["run" "-m" "figwheel.main" "-O" "advanced" "-bo" "dist"]
"fig:test" ["run" "-m" "figwheel.main" "-co" "test.cljs.edn" "-m" re-graph.test-runner]
"test" ["do" ["clean"] ["test"] ["fig:test"]]})
3 changes: 2 additions & 1 deletion src/re_graph/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
(:require [re-frame.core :as re-frame]
[re-graph.internals :as internals :refer [interceptors default-instance-name destroyed-instance]]
[re-frame.std-interceptors :as rfi]
[re-graph.logging :as log]
[clojure.string :as string]))

(re-frame/reg-event-fx
Expand Down Expand Up @@ -88,7 +89,7 @@
{:db (update-in db [:websocket :queue] conj dispatchable-event)}

:else
(internals/log
(log/error
(str
"Error creating subscription " subscription-id
" on instance " instance-name
Expand Down
27 changes: 13 additions & 14 deletions src/re_graph/internals.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
[re-frame.interceptor :refer [->interceptor get-coeffect assoc-coeffect update-coeffect enqueue]]
[re-frame.std-interceptors :as rfi]
[re-frame.interop :refer [empty-queue]]
[re-graph.logging :as log]
#?(:cljs [cljs-http.client :as http]
:clj [clj-http.client :as http])
#?(:cljs [clojure.core.async :as a]
:clj [clojure.core.async :refer [go] :as a])
:clj [clojure.core.async :refer [go] :as a])
#?(:clj [gniazdo.core :as ws])
#?(:clj [cheshire.core :as json]))
#?(:cljs (:require-macros [cljs.core.async.macros :refer [go]]))
Expand All @@ -19,17 +20,14 @@
(defn- cons-interceptor [ctx interceptor]
(update ctx :queue #(into (into empty-queue [interceptor]) %)))

(def log #?(:cljs js/console.error
:clj println))

(defn- encode [obj]
#?(:cljs (js/JSON.stringify (clj->js obj))
:cljs (json/encode obj)))

(defn- message->data [m]
#?(:cljs (-> (aget m "data")
(js/JSON.parse s)
(js->clj obj :keywordize-keys true))
(js/JSON.parse)
(js->clj :keywordize-keys true))
:clj (json/decode m keyword)))

(def re-graph-instance
Expand All @@ -56,9 +54,9 @@
(assoc-coeffect :event trimmed-event))

:default
(do (log "No default instance of re-graph found but no valid instance name was provided. Valid instance names are:" (keys re-graph)
"but was provided with" provided-instance-name
"handling event" event-name)
(do (log/error "No default instance of re-graph found but no valid instance name was provided. Valid instance names are:" (keys re-graph)
"but was provided with" provided-instance-name
"handling event" event-name)
ctx))))))

(def interceptors
Expand Down Expand Up @@ -116,7 +114,7 @@
(fn [{:keys [db] :as cofx} [subscription-id payload :as event]]
(if-let [callback-event (get-in db [:subscriptions (name subscription-id) :callback])]
{:dispatch (conj callback-event payload)}
(log "No callback-event found for subscription" subscription-id))))
(log/warn "No callback-event found for subscription" subscription-id))))

(re-frame/reg-event-db
::on-ws-complete
Expand Down Expand Up @@ -183,7 +181,7 @@
"error"
(re-frame/dispatch [::on-ws-data instance-name id {:errors payload}])

(log "Ignoring graphql-ws event " instance-name " - " type)))))
(log/debug "Ignoring graphql-ws event " instance-name " - " type)))))

(defn- on-open
([instance-name]
Expand All @@ -199,7 +197,7 @@

(defn- on-error [instance-name]
(fn [e]
(log "GraphQL websocket error" instance-name e)))
(log/warn "GraphQL websocket error" instance-name e)))

(re-frame/reg-event-fx
::reconnect-ws
Expand All @@ -211,7 +209,7 @@
(re-frame/reg-fx
::connect-ws
(fn [[instance-name ws-url]]
#?(:cljs (let [ws (js/WebSocket. url name)]
#?(:cljs (let [ws (js/WebSocket. ws-url "graphql-ws")]
(aset ws "onmessage" (on-ws-message instance-name))
(aset ws "onopen" (on-open instance-name ws))
(aset ws "onclose" (on-close instance-name))
Expand All @@ -220,7 +218,8 @@
:on-receive (on-ws-message instance-name)
:on-open (on-open instance-name)
:on-close (on-close instance-name)
:on-error (on-error instance-name)))))
:on-error (on-error instance-name)
:subprotocols ["graphql-ws"]))))

(re-frame/reg-fx
::disconnect-ws
Expand Down
50 changes: 50 additions & 0 deletions src/re_graph/logging.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
;; borrowed from https://gist.github.com/caskolkm/39d823f5bac7051d3062
(ns re-graph.logging
(:refer-clojure :exclude [time])
(:require #?(:clj [clojure.tools.logging :as log]
:cljs [goog.log :as glog]))
#?(:cljs (:import goog.debug.Console)))

#?(:cljs
(def logger
(glog/getLogger "app")))

#?(:cljs
(def levels {:severe goog.debug.Logger.Level.SEVERE
:warning goog.debug.Logger.Level.WARNING
:info goog.debug.Logger.Level.INFO
:config goog.debug.Logger.Level.CONFIG
:fine goog.debug.Logger.Level.FINE
:finer goog.debug.Logger.Level.FINER
:finest goog.debug.Logger.Level.FINEST}))

#?(:cljs
(defn log-to-console! []
(.setCapturing (goog.debug.Console.) true)))

#?(:cljs
(defn set-level! [level]
(.setLevel logger (get levels level (:info levels)))))

(defn fmt [msgs]
(apply str (interpose " " (map pr-str msgs))))

(defn info [& s]
(let [msg (fmt s)]
#?(:clj (log/info msg)
:cljs (glog/info logger msg))))

(defn debug [& s]
(let [msg (fmt s)]
#?(:clj (log/debug msg)
:cljs (glog/fine logger msg))))

(defn warn [& s]
(let [msg (fmt s)]
#?(:clj (log/warn msg)
:cljs (glog/warning logger msg))))

(defn error [& s]
(let [msg (fmt s)]
#?(:clj (log/error msg)
:cljs (glog/error logger msg))))
6 changes: 6 additions & 0 deletions test.cljs.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
^{
;; use an alternative landing page for the tests so that we don't
;; launch the application
:open-url "http://[[server-hostname]]:[[server-port]]/test.html"
:launch-js ["/opt/google/chrome/google-chrome" "--headless" "--disable-gpu" "--remote-debugging-port=9222" "--no-sandbox" :open-url]}
{:main re-graph.test-runner}
1 change: 0 additions & 1 deletion test/re_graph/core_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
:refer-macros [run-test-sync run-test-async wait-for]]
[clojure.test :refer [deftest is testing run-tests]
:refer-macros [deftest is testing run-tests]]
#?(:cljs [devcards.core :refer-macros [deftest]])
#?(:clj [cheshire.core :as json])))

(def on-ws-message @#'internals/on-ws-message)
Expand Down
6 changes: 0 additions & 6 deletions test/re_graph/runner.cljs

This file was deleted.

8 changes: 8 additions & 0 deletions test/re_graph/test_runner.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
;; This test runner is intended to be run from the command line
(ns re-graph.test-runner
(:require
[re-graph.all-tests]
[figwheel.main.testing :refer [run-tests-async]]))

(defn -main [& args]
(run-tests-async 5000))

0 comments on commit 8ebc6cd

Please sign in to comment.