Skip to content

Commit

Permalink
Fix #926: support add-watch on CLJS vars (#927)
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude authored Aug 12, 2024
1 parent 05eec0a commit c556f44
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/sci/impl/vars.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 11 additions & 2 deletions src/sci/lang.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
9 changes: 4 additions & 5 deletions test/sci/vars_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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")))

0 comments on commit c556f44

Please sign in to comment.