Skip to content

Commit

Permalink
fix fn shorthand invalidates hook signature and breaks fast-refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
roman01la committed Sep 27, 2023
1 parent 27c751c commit 7c39a4e
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion core/src/uix/dev.clj
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,30 @@
(cons maybe-conds (cons signature (rest fdecl)))
(cons signature fdecl))))

(defn- rewrite-form
"Rewrites a form to replace generated names with stable names
to make sure that hook's signature for fast-refresh does not update
on every compilation, unless there was a change in the hook's body."
[form]
(clojure.walk/prewalk
(fn [x]
(cond
(and (symbol? x) (re-matches #"^p\d*__\d+#$" (str x)))
(symbol (str/replace (str x) #"__\d+#$" ""))

:else x))
form))

(defn- rewrite-forms [forms]
(map rewrite-form forms))

(defn fast-refresh-signature [var-sym body]
(binding [*out* *err*]
(doseq [form (rewrite-forms (uix.lib/find-form uix.linter/hook-call? body))]
(println form)))
`(when ~goog-debug
(when (cljs.core/exists? js/window.uix.dev)
(let [sig# (js/window.uix.dev.signature!)]
(sig# ~var-sym ~(str/join (uix.lib/find-form uix.linter/hook-call? body)) nil nil)
(sig# ~var-sym ~(str/join (rewrite-forms (uix.lib/find-form uix.linter/hook-call? body))) nil nil)
(js/window.uix.dev.register! ~var-sym (.-displayName ~var-sym))
(set! (.-fast-refresh-signature ~var-sym) sig#)))))

0 comments on commit 7c39a4e

Please sign in to comment.