Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add :will-update and :did-update support #18

Merged
merged 1 commit into from
Feb 13, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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