diff --git a/CHANGELOG.md b/CHANGELOG.md index 9349cc45..17605196 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Changelog ## master (unreleased) +- Added new inspector function `tap-current-value` ### New features diff --git a/src/orchard/inspect.clj b/src/orchard/inspect.clj index 9bdd4178..07bb3a07 100644 --- a/src/orchard/inspect.clj +++ b/src/orchard/inspect.clj @@ -16,7 +16,7 @@ (java.lang.reflect Constructor Field Method Modifier) (java.util List Map))) -;; Datafy and Nav are only available since Clojure 1.10 +;; Datafy Nav and tap> are only available since Clojure 1.10 (require 'clojure.core.protocols) (def ^:private datafy @@ -25,6 +25,8 @@ (def ^:private nav (misc/call-when-resolved 'clojure.core.protocols/nav)) +(def ^:private maybe-tap> + (misc/call-when-resolved 'clojure.core/tap>)) ;; ;; Navigating Inspector State ;; @@ -171,6 +173,12 @@ (intern namespace (symbol var-name) (:value inspector)) (inspect-render inspector)) +(defn tap-current-value + "Tap the currently inspected value." + [inspector] + (maybe-tap> (:value inspector)) + (inspect-render inspector)) + (declare inspector-value-string) ;; diff --git a/src/orchard/misc.clj b/src/orchard/misc.clj index c011e2e6..de089049 100644 --- a/src/orchard/misc.clj +++ b/src/orchard/misc.clj @@ -165,6 +165,11 @@ otherwise false." (some? (resolve 'clojure.core.protocols/datafy))) +(def tap? + "True if tap> (added in Clojure 1.10) is supported, + otherwise false." + (some? (resolve 'clojure.core/tap>))) + (defn call-when-resolved "Return a fn that calls the fn resolved through `var-sym` with the arguments passed to it. `var-sym` will be required and resolved diff --git a/test/orchard/inspect_test.clj b/test/orchard/inspect_test.clj index 688967be..fa23aa1b 100644 --- a/test/orchard/inspect_test.clj +++ b/test/orchard/inspect_test.clj @@ -9,7 +9,8 @@ [clojure.string :as str] [clojure.test :as t :refer [deftest is are testing]] [orchard.inspect :as inspect] - [orchard.misc :refer [datafy? java-api-version]]) + [orchard.misc :refer [datafy? tap? java-api-version]] + [orchard.misc :as misc]) (:import java.io.File)) (defn- demunge-str [s] @@ -894,3 +895,22 @@ " " (:value ":data" 36) " = " (:value "{}" 37) (:newline))) (datafy-section rendered)))))))) + +(deftest tap-current-value + (testing "tap> current value") + (when tap? + (let [proof (atom []) + test-tap-handler (fn [x] + (swap! proof conj x))] + ((misc/call-when-resolved 'clojure.core/add-tap) test-tap-handler) + (-> (inspect/fresh) + (inspect/start {:a {:b 1}}) + (inspect/tap-current-value) + (inspect/down 2) + (inspect/tap-current-value) + (inspect/down 1) + (inspect/tap-current-value)) + ((misc/call-when-resolved 'clojure.core/remove-tap) test-tap-handler) + (is (= [{:a {:b 1}} + {:b 1} + :b] @proof)))))