Simplify handling of state

Just compare new state with identical?, and forceUpdate if necessary
This commit is contained in:
Dan Holmsand 2013-12-20 19:01:58 +01:00
parent 1ad5ef44e8
commit 8bd03a8b36
3 changed files with 11 additions and 12 deletions

View File

@ -9,7 +9,7 @@ PROF = dev,test
CLJSBUILD = client
CLJSDIRS = src test
VERSION = 0.0.3 # -SNAPSHOT
VERSION = 0.0.4-SNAPSHOT
all: buildrun

View File

@ -1,6 +1,6 @@
(defproject cloact "0.0.3"
(defproject cloact "0.0.4-SNAPSHOT"
:url "http://github.com/holmsand/cloact"
:license {:name "MIT"}
:description "A simple ClojureScript interface to React"

View File

@ -32,7 +32,8 @@
IWatchable
(-notify-watches [C old new]
(.replaceState C new))
(when-not (identical? old new)
(.forceUpdate C)))
(-add-watch [C key f] (assert false "Component isn't really watchable"))
(-remove-watch [C key] (assert false "Component isn't really watchable"))
@ -120,7 +121,7 @@
;; reset! doesn't call -notifyWatches unless -watches is set
(set! (.-watches C) {})
(when f
(set! (.-cljsOldState C) (merge (.-state C) (f C)))))
(set! (.-state C) (merge (.-state C) (f C)))))
:componentWillReceiveProps
(fn [C props]
@ -128,17 +129,15 @@
:shouldComponentUpdate
(fn [C nextprops nextstate]
;; Don't care about nextstate here, we use forceUpdate
;; when only when state has changed anyway.
(let [a1 (args-of C)
a2 (-> nextprops .-cljsArgs)
ostate (.-cljsOldState C)]
a2 (-> nextprops .-cljsArgs)]
(assert (vector? a1))
(set! (.-cljsOldState C) nextstate)
(if (nil? f)
(not (and (identical? ostate nextstate)
(tmpl/equal-args a1 a2)))
;; Call f with oldprops, newprops, oldstate, newstate
(f (props-in-args a1) (props-in-args a2)
ostate nextstate))))
(not (tmpl/equal-args a1 a2))
;; Call f with oldprops, newprops
(f (props-in-args a1) (props-in-args a2)))))
:componentWillUnmount
(fn [C]