diff --git a/CHANGELOG.md b/CHANGELOG.md index a6e287cd..ba618726 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,10 @@ SCI is used in [babashka](https://github.com/babashka/babashka), [joyride](https://github.com/BetterThanTomorrow/joyride/) and many [other](https://github.com/babashka/sci#projects-using-sci) projects. +## Unreleased + +- Fix [#926](https://github.com/babashka/sci/issues/926): Support `add-watch` on vars in CLJS + ## 0.8.43 (2024-08-06) - Fix shadow-cljs warnings diff --git a/src/sci/impl/vars.cljc b/src/sci/impl/vars.cljc index 0b618211..da1767dd 100644 --- a/src/sci/impl/vars.cljc +++ b/src/sci/impl/vars.cljc @@ -225,7 +225,7 @@ (defn var-set [v val] (t/setVal v val)) -(defn unqualify-symbol +(defn unqualify-symbol "If sym is namespace-qualified, remove the namespace, else return sym" [sym] (if (qualified-symbol? sym) diff --git a/src/sci/lang.cljc b/src/sci/lang.cljc index 456f9c77..f9c344d1 100644 --- a/src/sci/lang.cljc +++ b/src/sci/lang.cljc @@ -188,10 +188,19 @@ (vars/with-writeable-var this meta (set! watches (assoc watches key fn))) this) - (removeWatch [this _] + (removeWatch [this key] (vars/with-writeable-var this meta (set! watches (dissoc watches key))) - this)]) + this)] + :cljs [IWatchable + (-add-watch [this key fn] + (vars/with-writeable-var this meta + (set! watches (assoc watches key fn))) + this) + (-remove-watch [this key] + (vars/with-writeable-var this meta + (set! watches (dissoc watches key))) + this)]) ;; #?(:cljs Fn) ;; In the real CLJS this is there... why? #?(:clj clojure.lang.IFn :cljs IFn) (#?(:clj invoke :cljs -invoke) [this] diff --git a/test/sci/vars_test.cljc b/test/sci/vars_test.cljc index e63b5fc6..7448c2b3 100644 --- a/test/sci/vars_test.cljc +++ b/test/sci/vars_test.cljc @@ -256,8 +256,7 @@ (is (true? (eval* "(def ^:dynamic *x*) (def ^:dynamic *y*) (binding [*x* *x* *y* *y*] (thread-bound? #'*x* #'*x*))")))) -#?(:clj - (deftest add-watch-test - (is (str/starts-with? - (sci/with-out-str (sci/eval-string "(def x 1) (add-watch #'x :foo (fn [k r o n] (prn :o o :n n))) (alter-var-root #'x (constantly 5))")) - ":o 1 :n 5")))) +(deftest add-watch-test + (is (str/starts-with? + (sci/with-out-str (sci/eval-string "(def x 1) (add-watch #'x :foo (fn [k r o n] (prn :o o :n n))) (alter-var-root #'x (constantly 5))")) + ":o 1 :n 5")))