Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 11 additions & 24 deletions src/helix/hooks.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
:once Equivalent to using [] as the deps.
:auto-deps Infer the dependencies automatically from the code by finding
local vars. Not available for the function form of a hook."
#?(:clj (:require [helix.impl.analyzer :as hana])
#?(:clj (:require [helix.impl.analyzer :as hana]
[helix.impl.hooks :as-alias impl.hooks])
:cljs (:require
[helix.impl.hooks :as impl.hooks]
["react" :as react]
[goog.object :as gobj]))
#?(:cljs (:require-macros [helix.hooks])))
Expand Down Expand Up @@ -117,21 +119,6 @@
"Just react/useContext"
react/useContext))


;; React `useEffect` expects either a function or undefined to be returned
#?(:cljs
(defn- wrap-fx [f]
(fn wrap-fx-return []
(let [x (f)]
(if (fn? x)
x
js/undefined)))))


(defn simple-body? [body]
(and (= (count body) 1) (symbol? (first body))))


#?(:clj
(defn deps-macro-body [env deps body simple-body-ok? deps->hook-body]
(cond
Expand Down Expand Up @@ -187,9 +174,9 @@
(deps-macro-body
&env deps body false
(fn
([fn-body] `^clj-nil (raw-use-effect (wrap-fx (fn [] ~@fn-body))))
([fn-body] `^clj-nil (raw-use-effect (impl.hooks/wrap-fx (fn [] ~@fn-body))))
([deps fn-body]
`^clj-nil (raw-use-effect (wrap-fx (fn [] ~@fn-body)) ~deps))))))
`^clj-nil (raw-use-effect (impl.hooks/wrap-fx (fn [] ~@fn-body)) ~deps))))))


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


(defmacro use-layout-effect
Expand All @@ -214,20 +201,20 @@
(deps-macro-body
&env deps body false
(fn
([fn-body] `^clj-nil (raw-use-layout-effect (wrap-fx (fn [] ~@fn-body))))
([fn-body] `^clj-nil (raw-use-layout-effect (impl.hooks/wrap-fx (fn [] ~@fn-body))))
([deps fn-body]
`^clj-nil (raw-use-layout-effect (wrap-fx (fn [] ~@fn-body)) ~deps))))))
`^clj-nil (raw-use-layout-effect (impl.hooks/wrap-fx (fn [] ~@fn-body)) ~deps))))))


#?(:cljs
(defn use-layout-effect*
"Like `use-effect*` but instead calls react/useLayoutEffect."
([f] (react/useLayoutEffect (wrap-fx f)))
([f] (react/useLayoutEffect (impl.hooks/wrap-fx f)))
([f deps]
(when goog/DEBUG
(when (= deps :auto-deps)
(throw (js/Error. "Can't use `:auto-deps` with `use-layout-effect*`; use `use-layout-effect` macro for that"))))
(react/useLayoutEffect (wrap-fx f) (to-array deps)))))
(react/useLayoutEffect (impl.hooks/wrap-fx f) (to-array deps)))))


(defmacro use-memo
Expand Down
9 changes: 9 additions & 0 deletions src/helix/impl/hooks.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
(ns helix.impl.hooks)

;; React `useEffect` expects either a function or undefined to be returned
(defn wrap-fx [f]
(fn wrap-fx-return []
(let [x (f)]
(if (fn? x)
x
js/undefined))))