From 662714a5e03bc6916d9c55e6772ed05c99f13ad1 Mon Sep 17 00:00:00 2001 From: Dan Holmsand Date: Tue, 17 Dec 2013 19:20:20 +0100 Subject: [PATCH] Don't be so clever with reactive props --- Makefile | 2 +- src/cloact/impl/component.cljs | 49 ++++++++++++++-------------------- 2 files changed, 21 insertions(+), 30 deletions(-) diff --git a/Makefile b/Makefile index 6b4f8bf..0cf561a 100644 --- a/Makefile +++ b/Makefile @@ -28,7 +28,7 @@ install: setup lein install preclean: - rm -rf repl .repl target + rm -rf repl .repl target out clean: preclean lein -o clean diff --git a/src/cloact/impl/component.cljs b/src/cloact/impl/component.cljs index 0482605..d5bbd73 100644 --- a/src/cloact/impl/component.cljs +++ b/src/cloact/impl/component.cljs @@ -8,15 +8,12 @@ (def React tmpl/React) -;;; Atom protocol as mixin +;;; IDeref protocol as mixin (def CloactMixin (js-obj)) (def -ToExtend (js-obj)) (set! (.-prototype -ToExtend) CloactMixin) -;; (declare get-props) -;; (declare get-children) - (extend-type -ToExtend IEquiv (-equiv [C other] (identical? C other)) @@ -55,15 +52,19 @@ (defn- args-of [C] (-> C .-props .-cljsArgs)) -(defn- cljs-props [C] - (let [args (args-of C) - p (nth args 1 nil)] - (when (map? p) - p))) +(defn- props-in-args [args] + (let [p (nth args 1 nil)] + (when (map? p) p))) + +(defn props-in-props [props] + (-> props .-cljsArgs props-in-args)) (defn- first-child [args] - (let [p? (nth args 1 nil)] - (if (or (nil? p?) (map? p?)) 2 1))) + (let [p (nth args 1 nil)] + (if (or (nil? p) (map? p)) 2 1))) + +(defn- cljs-props [C] + (-> (args-of C) props-in-args)) (defn- get-children [C] (let [args (args-of C) @@ -83,15 +84,9 @@ (replace-props C (merge (cljs-props C) newprops))) (defn get-props [C] - (let [ctx ratom/*ratom-context*] - (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)))))))) + (cljs-props C)) (defn- do-render [C f] - (set! (.-isRenderContext ratom/*ratom-context*) true) (let [res (f (cljs-props C) C (.-state C)) conv (if (vector? res) (tmpl/as-component res) @@ -118,26 +113,23 @@ ;; reset! doesn't call -notifyWatches unless -watches is set (set! (.-watches C) {}) (when f - (set! (.-cljsOldState C) - (merge (.-state C) (f C))))) + (set! (.-cljsOldState C) (merge (.-state C) (f C))))) :componentWillReceiveProps (fn [C props] - (when-not (nil? (.-cljsPropsAtom C)) - (reset! (.-cljsPropsAtom C) (cljs-props C))) - (when f (f C props))) + (when f (f C (props-in-props props)))) :shouldComponentUpdate (fn [C nextprops nextstate] - (assert (nil? f) "shouldComponentUpdate is not yet supported") (let [a1 (args-of C) a2 (-> nextprops .-cljsArgs) - ostate (.-cljsOldState C) - eq (and (identical? ostate nextstate) - (tmpl/equal-args a1 a2))] + ostate (.-cljsOldState C)] (assert (vector? a1)) (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 (fn [C] @@ -167,7 +159,6 @@ (default-wrapper (or wrap f)))) (def obligatory {:getInitialState nil - :componentWillReceiveProps nil :shouldComponentUpdate nil :componentWillUnmount nil})