Skip to content

Commit 6b3d4d3

Browse files
Fix use of private wrap-fx in macro (#160)
* Remove unused helper * Fix use of private function from macro This ends up expanding in the caller's namespace, which references the private `wrap-fx` and leads to a compiler warning under Figwheel. I believe Shadow is suppressing these so it wasn't caught.
1 parent 0d3774d commit 6b3d4d3

File tree

2 files changed

+20
-24
lines changed

2 files changed

+20
-24
lines changed

src/helix/hooks.cljc

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
:once Equivalent to using [] as the deps.
1212
:auto-deps Infer the dependencies automatically from the code by finding
1313
local vars. Not available for the function form of a hook."
14-
#?(:clj (:require [helix.impl.analyzer :as hana])
14+
#?(:clj (:require [helix.impl.analyzer :as hana]
15+
[helix.impl.hooks :as-alias impl.hooks])
1516
:cljs (:require
17+
[helix.impl.hooks :as impl.hooks]
1618
["react" :as react]
1719
[goog.object :as gobj]))
1820
#?(:cljs (:require-macros [helix.hooks])))
@@ -117,21 +119,6 @@
117119
"Just react/useContext"
118120
react/useContext))
119121

120-
121-
;; React `useEffect` expects either a function or undefined to be returned
122-
#?(:cljs
123-
(defn- wrap-fx [f]
124-
(fn wrap-fx-return []
125-
(let [x (f)]
126-
(if (fn? x)
127-
x
128-
js/undefined)))))
129-
130-
131-
(defn simple-body? [body]
132-
(and (= (count body) 1) (symbol? (first body))))
133-
134-
135122
#?(:clj
136123
(defn deps-macro-body [env deps body simple-body-ok? deps->hook-body]
137124
(cond
@@ -187,9 +174,9 @@
187174
(deps-macro-body
188175
&env deps body false
189176
(fn
190-
([fn-body] `^clj-nil (raw-use-effect (wrap-fx (fn [] ~@fn-body))))
177+
([fn-body] `^clj-nil (raw-use-effect (impl.hooks/wrap-fx (fn [] ~@fn-body))))
191178
([deps fn-body]
192-
`^clj-nil (raw-use-effect (wrap-fx (fn [] ~@fn-body)) ~deps))))))
179+
`^clj-nil (raw-use-effect (impl.hooks/wrap-fx (fn [] ~@fn-body)) ~deps))))))
193180

194181

195182
#?(:cljs
@@ -198,12 +185,12 @@
198185
;; be harder to read
199186
(defn use-effect*
200187
"Like react/useEffect. See `use-effect` for details on what `f`'s return values. See namespace doc for `deps`."
201-
([f] (react/useEffect (wrap-fx f)))
188+
([f] (react/useEffect (impl.hooks/wrap-fx f)))
202189
([f deps]
203190
(when goog/DEBUG
204191
(when (= deps :auto-deps)
205192
(throw (js/Error. "Can't use `:auto-deps` with `use-effect*`; use `use-effect` macro for that"))))
206-
(react/useEffect (wrap-fx f) (to-array deps)))))
193+
(react/useEffect (impl.hooks/wrap-fx f) (to-array deps)))))
207194

208195

209196
(defmacro use-layout-effect
@@ -214,20 +201,20 @@
214201
(deps-macro-body
215202
&env deps body false
216203
(fn
217-
([fn-body] `^clj-nil (raw-use-layout-effect (wrap-fx (fn [] ~@fn-body))))
204+
([fn-body] `^clj-nil (raw-use-layout-effect (impl.hooks/wrap-fx (fn [] ~@fn-body))))
218205
([deps fn-body]
219-
`^clj-nil (raw-use-layout-effect (wrap-fx (fn [] ~@fn-body)) ~deps))))))
206+
`^clj-nil (raw-use-layout-effect (impl.hooks/wrap-fx (fn [] ~@fn-body)) ~deps))))))
220207

221208

222209
#?(:cljs
223210
(defn use-layout-effect*
224211
"Like `use-effect*` but instead calls react/useLayoutEffect."
225-
([f] (react/useLayoutEffect (wrap-fx f)))
212+
([f] (react/useLayoutEffect (impl.hooks/wrap-fx f)))
226213
([f deps]
227214
(when goog/DEBUG
228215
(when (= deps :auto-deps)
229216
(throw (js/Error. "Can't use `:auto-deps` with `use-layout-effect*`; use `use-layout-effect` macro for that"))))
230-
(react/useLayoutEffect (wrap-fx f) (to-array deps)))))
217+
(react/useLayoutEffect (impl.hooks/wrap-fx f) (to-array deps)))))
231218

232219

233220
(defmacro use-memo

src/helix/impl/hooks.cljs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
(ns helix.impl.hooks)
2+
3+
;; React `useEffect` expects either a function or undefined to be returned
4+
(defn wrap-fx [f]
5+
(fn wrap-fx-return []
6+
(let [x (f)]
7+
(if (fn? x)
8+
x
9+
js/undefined))))

0 commit comments

Comments
 (0)