Skip to content

Commit

Permalink
Merge pull request #18 from avasenin/component-update-fns
Browse files Browse the repository at this point in the history
Add :will-update and :did-update support
  • Loading branch information
tonsky committed Feb 13, 2015
2 parents 35602ed + b13b865 commit c5f3b3d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -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 }
```

Expand Down
19 changes: 13 additions & 6 deletions src/rum.cljs
Expand Up @@ -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))
]
Expand Down Expand Up @@ -79,18 +80,24 @@
(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
(let [state (state this)
[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 []
Expand Down

0 comments on commit c5f3b3d

Please sign in to comment.