From b13b865cb50da3adbd1d288225e8df74a5919cce Mon Sep 17 00:00:00 2001 From: Andrey Vasenin Date: Thu, 12 Feb 2015 17:40:50 +0600 Subject: [PATCH] Add :will-update and :did-update support --- README.md | 2 ++ src/rum.cljs | 19 +++++++++++++------ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index c86cbdd..96ff777 100644 --- a/README.md +++ b/README.md @@ -204,8 +204,10 @@ Mixins are basic building blocks for designing new components behaviors in Rum. :did-mount ;; state ⇒ state :transfer-state ;; old-state, state ⇒ state :should-update ;; old-state, state ⇒ boolean + :will-update ;; state ⇒ state :render ;; state ⇒ [pseudo-dom state] :wrap-render ;; render-fn ⇒ render-fn + :did-update ;; state ⇒ state :will-unmount ;; state ⇒ state } ``` diff --git a/src/rum.cljs b/src/rum.cljs index d16a0e5..8f453d7 100644 --- a/src/rum.cljs +++ b/src/rum.cljs @@ -34,10 +34,11 @@ did-mount (fns :did-mount classes) ;; state -> state transfer-state (fns :transfer-state classes) ;; old-state state -> state should-update (fns :should-update classes) ;; old-state state -> boolean + will-update (fns :will-update classes) ;; state -> state render (first (fns :render classes)) ;; state -> [dom state] wrapped-render (reduce #(%2 %1) render (fns :wrap-render classes)) ;; render-fn -> render-fn + did-update (fns :did-update classes) ;; state -> state will-unmount (fns :will-unmount classes) ;; state -> state - props->state (fn [props] (call-all (aget props ":rum/state") init props)) ] @@ -79,10 +80,12 @@ (let [old-state @(state this) new-state @(aget next-props ":rum/state")] (or (some #(% old-state new-state) should-update) false))))) -;; :componentWillUpdate -;; (fn [next-props next-state] -;; (this-as this -;; (println (::id @(state this)) "will-update"))) + :componentWillUpdate + (when-not (empty? will-update) + (fn [next-props _] + (this-as this + (let [new-state (aget next-props ":rum/state")] + (vswap! new-state call-all will-update))))) :render (fn [] (this-as this @@ -90,7 +93,11 @@ [dom next-state] (wrapped-render @state)] (vreset! state next-state) dom))) -;; :componentDidUpdate (fn [prev-props prev-state]) + :componentDidUpdate + (when-not (empty? did-update) + (fn [_ _] + (this-as this + (vswap! (state this) call-all did-update)))) :componentWillUnmount (when-not (empty? will-unmount) (fn []