Skip to content

Commit

Permalink
Setup middleware to warn user and fail-safely if cognitect.rebl is no…
Browse files Browse the repository at this point in the history
…t found.
  • Loading branch information
jaidetree committed Dec 13, 2018
1 parent 6f8f410 commit 2b20c78
Showing 1 changed file with 33 additions and 17 deletions.
50 changes: 33 additions & 17 deletions src/nrebl/middleware.clj
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
(ns nrebl.middleware
(:require [nrepl
;; TODO: Please look at my lein-2.8.1 branch for cleanup of imports and
;; supporting legacy lein with dynamic imports.
(:require [clojure.tools.nrepl
[middleware :refer [set-descriptor!]]
[transport :as transport]]
[nrepl.middleware.interruptible-eval :as ev]
[cognitect.rebl.ui :as ui]
[cognitect.rebl :as rebl]
[clojure.datafy :refer [datafy]])
(:import
nrepl.transport.Transport))
clojure.tools.nrepl.transport.Transport))

(def rebl-exists?
(try
(require '[cognitect.rebl :as rebl])
true
(catch java.io.FileNotFoundException _
false)))

;; Ensure cognitect.rebl namespace exists
(when-not rebl-exists?
(create-ns 'rebl)
(intern 'rebl 'submit identity)
(intern 'rebl 'ui identity))

(defn send-to-rebl! [{:keys [code] :as req} {:keys [value] :as resp}]
(when-let [value (datafy value)]
Expand All @@ -17,7 +29,7 @@
(defn- wrap-rebl-sender
"Wraps a `Transport` with code which prints the value of messages sent to
it using the provided function."
[{:keys [id op ^Transport transport] :as request} ]
[{:keys [id op ^Transport transport] :as request}]
(reify Transport
(recv [this]
(.recv transport))
Expand All @@ -28,11 +40,16 @@
(send-to-rebl! request resp))
this)))

(defn wrap-nrebl [handler]
(fn [{:keys [id op transport] :as request}]
(if (= op "start-rebl-ui")
(rebl/ui)
(handler (assoc request :transport (wrap-rebl-sender request))))))
(defn wrap-nrebl
[handler]
(if rebl-exists?
(fn [{:keys [id op transport] :as request}]
(cond
(= op "start-rebl-ui") (rebl/ui)
:else (handler (assoc request :transport (wrap-rebl-sender request)))))
(do
(println "WARNING: cognitect.rebl namespace was not found on classpath. nrebl.middlware could not be started.")
handler)))

(set-descriptor! #'wrap-nrebl
{:requires #{}
Expand All @@ -49,17 +66,16 @@
(-> (nrepl/client conn 1000) ; message receive timeout required
;(nrepl/message {:op "inspect-nrebl" :code "[1 2 3 4 5 6 7 8 9 10 {:a :b :c :d :e #{5 6 7 8 9 10}}]"})
(nrepl/message {:op "eval" :code "(do {:a :b :c [1 2 3 4] :d #{5 6 7 8} :e (range 20)})"})
nrepl/response-values
))
nrepl/response-values))


(with-open [conn (nrepl/connect :port 52756)]
(-> (nrepl/client conn 1000) ; message receive timeout required
(nrepl/message {:op "start-rebl-ui"})
nrepl/response-values
))
nrepl/response-values))


(require '[nrepl.server :as ser])

(def nrep (ser/start-server :port 55804
:handler (ser/default-handler #'wrap-nrebl)))
)
:handler (ser/default-handler #'wrap-nrebl))))

0 comments on commit 2b20c78

Please sign in to comment.