Last active
November 30, 2022 15:30
-
-
Save gfredericks/7143494 to your computer and use it in GitHub Desktop.
thread-local version of with-redefs
This file contains hidden or 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
(defn with-local-redefs-fn | |
[a-var its-new-value func] | |
(cast clojure.lang.IFn @a-var) | |
(alter-meta! a-var | |
(fn [m] | |
(if (::scope-count m) | |
(update-in m [::scope-count] inc) | |
(assoc m | |
::scope-count 1 | |
::thread-local-var (doto (clojure.lang.Var/create @a-var) | |
(.setDynamic true)))))) | |
(let [thread-local-var (-> a-var meta ::thread-local-var)] | |
(with-redefs-fn {a-var (fn [& args] | |
(apply (var-get thread-local-var) args))} | |
(fn [] | |
(push-thread-bindings {thread-local-var its-new-value}) | |
(try (func) | |
(finally | |
(pop-thread-bindings) | |
(alter-meta! a-var | |
(fn [m] | |
(if (= 1 (::scope-count m)) | |
(dissoc m ::scope-count ::thread-local-var) | |
(update-in m [::scope-count] dec)))))))))) | |
(defmacro with-local-redefs | |
"Like with-redefs, but changes a var's value thread-locally. Unlike binding, | |
it does not require the var to be dynamic, but it does require it to be a | |
function. | |
Does not compose with with-redefs, as it uses with-redefs internally." | |
[bindings & body] | |
(if-let [[v val & more] (seq bindings)] | |
`(with-local-redefs-fn (var ~v) ~val | |
(fn [] (with-local-redefs ~more ~@body))) | |
(cons 'do body))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey @gfredericks the above doesn't seem to work for this test case where the same function is bound to two different functions in two different futures.
I wrote a work around on this here https://gist.github.com/mourjo/c7fc03e59eb96f8a342dfcabd350a927 -- would appreciate it if you could check it out and suggest improvements/corrections!