mirror of https://github.com/status-im/reagent.git
Don't be so clever with reactive props
This commit is contained in:
parent
239120954d
commit
662714a5e0
2
Makefile
2
Makefile
|
@ -28,7 +28,7 @@ install: setup
|
||||||
lein install
|
lein install
|
||||||
|
|
||||||
preclean:
|
preclean:
|
||||||
rm -rf repl .repl target
|
rm -rf repl .repl target out
|
||||||
|
|
||||||
clean: preclean
|
clean: preclean
|
||||||
lein -o clean
|
lein -o clean
|
||||||
|
|
|
@ -8,15 +8,12 @@
|
||||||
|
|
||||||
(def React tmpl/React)
|
(def React tmpl/React)
|
||||||
|
|
||||||
;;; Atom protocol as mixin
|
;;; IDeref protocol as mixin
|
||||||
|
|
||||||
(def CloactMixin (js-obj))
|
(def CloactMixin (js-obj))
|
||||||
(def -ToExtend (js-obj))
|
(def -ToExtend (js-obj))
|
||||||
(set! (.-prototype -ToExtend) CloactMixin)
|
(set! (.-prototype -ToExtend) CloactMixin)
|
||||||
|
|
||||||
;; (declare get-props)
|
|
||||||
;; (declare get-children)
|
|
||||||
|
|
||||||
(extend-type -ToExtend
|
(extend-type -ToExtend
|
||||||
IEquiv
|
IEquiv
|
||||||
(-equiv [C other] (identical? C other))
|
(-equiv [C other] (identical? C other))
|
||||||
|
@ -55,15 +52,19 @@
|
||||||
(defn- args-of [C]
|
(defn- args-of [C]
|
||||||
(-> C .-props .-cljsArgs))
|
(-> C .-props .-cljsArgs))
|
||||||
|
|
||||||
(defn- cljs-props [C]
|
(defn- props-in-args [args]
|
||||||
(let [args (args-of C)
|
(let [p (nth args 1 nil)]
|
||||||
p (nth args 1 nil)]
|
(when (map? p) p)))
|
||||||
(when (map? p)
|
|
||||||
p)))
|
(defn props-in-props [props]
|
||||||
|
(-> props .-cljsArgs props-in-args))
|
||||||
|
|
||||||
(defn- first-child [args]
|
(defn- first-child [args]
|
||||||
(let [p? (nth args 1 nil)]
|
(let [p (nth args 1 nil)]
|
||||||
(if (or (nil? p?) (map? p?)) 2 1)))
|
(if (or (nil? p) (map? p)) 2 1)))
|
||||||
|
|
||||||
|
(defn- cljs-props [C]
|
||||||
|
(-> (args-of C) props-in-args))
|
||||||
|
|
||||||
(defn- get-children [C]
|
(defn- get-children [C]
|
||||||
(let [args (args-of C)
|
(let [args (args-of C)
|
||||||
|
@ -83,15 +84,9 @@
|
||||||
(replace-props C (merge (cljs-props C) newprops)))
|
(replace-props C (merge (cljs-props C) newprops)))
|
||||||
|
|
||||||
(defn get-props [C]
|
(defn get-props [C]
|
||||||
(let [ctx ratom/*ratom-context*]
|
(cljs-props C))
|
||||||
(if (or (nil? ctx) (.-isRenderContext ctx))
|
|
||||||
(cljs-props C)
|
|
||||||
;; Use atom if getting props in an ratom
|
|
||||||
(deref (or (.-cljsPropsAtom C)
|
|
||||||
(set! (.-cljsPropsAtom C) (ratom/ratom (cljs-props C))))))))
|
|
||||||
|
|
||||||
(defn- do-render [C f]
|
(defn- do-render [C f]
|
||||||
(set! (.-isRenderContext ratom/*ratom-context*) true)
|
|
||||||
(let [res (f (cljs-props C) C (.-state C))
|
(let [res (f (cljs-props C) C (.-state C))
|
||||||
conv (if (vector? res)
|
conv (if (vector? res)
|
||||||
(tmpl/as-component res)
|
(tmpl/as-component res)
|
||||||
|
@ -118,26 +113,23 @@
|
||||||
;; reset! doesn't call -notifyWatches unless -watches is set
|
;; reset! doesn't call -notifyWatches unless -watches is set
|
||||||
(set! (.-watches C) {})
|
(set! (.-watches C) {})
|
||||||
(when f
|
(when f
|
||||||
(set! (.-cljsOldState C)
|
(set! (.-cljsOldState C) (merge (.-state C) (f C)))))
|
||||||
(merge (.-state C) (f C)))))
|
|
||||||
|
|
||||||
:componentWillReceiveProps
|
:componentWillReceiveProps
|
||||||
(fn [C props]
|
(fn [C props]
|
||||||
(when-not (nil? (.-cljsPropsAtom C))
|
(when f (f C (props-in-props props))))
|
||||||
(reset! (.-cljsPropsAtom C) (cljs-props C)))
|
|
||||||
(when f (f C props)))
|
|
||||||
|
|
||||||
:shouldComponentUpdate
|
:shouldComponentUpdate
|
||||||
(fn [C nextprops nextstate]
|
(fn [C nextprops nextstate]
|
||||||
(assert (nil? f) "shouldComponentUpdate is not yet supported")
|
|
||||||
(let [a1 (args-of C)
|
(let [a1 (args-of C)
|
||||||
a2 (-> nextprops .-cljsArgs)
|
a2 (-> nextprops .-cljsArgs)
|
||||||
ostate (.-cljsOldState C)
|
ostate (.-cljsOldState C)]
|
||||||
eq (and (identical? ostate nextstate)
|
|
||||||
(tmpl/equal-args a1 a2))]
|
|
||||||
(assert (vector? a1))
|
(assert (vector? a1))
|
||||||
(set! (.-cljsOldState C) nextstate)
|
(set! (.-cljsOldState C) nextstate)
|
||||||
(not eq)))
|
(if (nil? f)
|
||||||
|
(not (and (identical? ostate nextstate)
|
||||||
|
(tmpl/equal-args a1 a2)))
|
||||||
|
(f a1 a2 ostate nextstate))))
|
||||||
|
|
||||||
:componentWillUnmount
|
:componentWillUnmount
|
||||||
(fn [C]
|
(fn [C]
|
||||||
|
@ -167,7 +159,6 @@
|
||||||
(default-wrapper (or wrap f))))
|
(default-wrapper (or wrap f))))
|
||||||
|
|
||||||
(def obligatory {:getInitialState nil
|
(def obligatory {:getInitialState nil
|
||||||
:componentWillReceiveProps nil
|
|
||||||
:shouldComponentUpdate nil
|
:shouldComponentUpdate nil
|
||||||
:componentWillUnmount nil})
|
:componentWillUnmount nil})
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue