-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This middleware keeps the client notified of what the current repl type is.
- Loading branch information
Showing
5 changed files
with
64 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
(ns cider.nrepl.middleware.track-state | ||
"State tracker for client sessions." | ||
{:author "Artur Malabarba"} | ||
(:require [cider.nrepl.middleware.util.cljs :as cljs] | ||
[clojure.tools.nrepl.middleware :refer [set-descriptor!]] | ||
[clojure.tools.nrepl.middleware.interruptible-eval :refer [*msg*]] | ||
[clojure.tools.nrepl.misc :refer [response-for]] | ||
[clojure.tools.nrepl.transport :as transport]) | ||
(:import clojure.tools.nrepl.transport.Transport)) | ||
|
||
(defn assoc-state | ||
"Return response with a :state entry assoc'ed." | ||
[response msg] | ||
(assoc response :state {:repl-type (if (cljs/grab-cljs-env msg) :cljs :clj)})) | ||
|
||
(defn make-transport | ||
"Return a Transport that defers to `transport` and possibly notifies | ||
about the state." | ||
[{:keys [^Transport transport] :as msg}] | ||
(reify Transport | ||
(recv [this] (.recv transport)) | ||
(recv [this timeout] (.recv transport timeout)) | ||
(send [this {:keys [status] :as response}] | ||
(.send transport (cond-> response | ||
(contains? status :done) (assoc-state msg)))))) | ||
|
||
(defn wrap-tracker | ||
"Middleware that tracks relevant server info and notifies the client." | ||
[handler] | ||
(fn [{:keys [op] :as msg}] | ||
(cond | ||
(#{"eval" "load-file"} op) (handler (assoc msg :transport (make-transport msg))) | ||
:else (handler msg)))) | ||
|
||
(set-descriptor! | ||
#'wrap-tracker | ||
(cljs/requires-piggieback | ||
{:expects #{"eval"} | ||
:handles | ||
{"track-state-middleware" | ||
{:doc "Enhances the `eval` op by notifying the client of the current REPL state. Currently, only the REPL type (Clojure or ClojureScript) is informed."}}})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
(ns cider.nrepl.middleware.track-state-test | ||
(:require [cider.nrepl.middleware.track-state :as s] | ||
[cider.nrepl.middleware.util.cljs :as cljs] | ||
[clojure.test :refer :all] | ||
[clojure.tools.nrepl.transport :as t]) | ||
(:import clojure.tools.nrepl.transport.Transport)) | ||
|
||
(deftest make-transport | ||
(is (instance? Transport (s/make-transport nil))) | ||
(is (try (send (s/make-transport nil) 10) | ||
nil | ||
(catch Exception e true)))) | ||
|
||
(deftest assoc-state | ||
(is (= (s/assoc-state {} {}) | ||
{:state {:repl-type :clj}})) | ||
(with-redefs [cljs/grab-cljs-env identity] | ||
(is (= (s/assoc-state {} {}) | ||
{:state {:repl-type :cljs}})))) |