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

Implement collection diff updates #12

Merged
merged 6 commits into from Feb 5, 2017

Remove obsolete function to delete components

  • Loading branch information...
nblumoe committed Jan 25, 2017
commit b6ec26fcd915ae6c5997ac7b7157da5a9607b33f
View
@@ -6,7 +6,6 @@
(defprotocol IDom
(create-component! [this type])
(delete-component! [this k child])
(set-child! [this parent id child])
(set-indexed-child! [this parent k idx child])
(delete-indexed-child! [this parent k idx child])
@@ -53,7 +52,7 @@
(dotimes [idx (max (count a-list) (count b-list))]
(let [a (nth a-list idx nil)
b (nth b-list idx nil)]
(let [{:keys [node] :as result} (diff dom k a b)]
(let [{:keys [node] :as result} (diff dom a b)]
(condp instance? result
;; TODO: Unmount?
Created (set-indexed-child! dom parent-node k idx node)
@@ -67,7 +66,7 @@
(let [vb (get spec-b k)]
(if (sequential? vb)
(diff-child-list dom dom-node k va vb)
(let [result (diff dom k va vb)]
(let [result (diff dom va vb)]
(if (or (instance? Created result)
(instance? Updated result))
(set-property! dom dom-node k (:node result)))))))
@@ -79,52 +78,50 @@
(when-not (get spec-a k)
(if (sequential? vb)
(diff-child-list dom dom-node k nil vb)
(let [result (diff dom k nil vb)]
(let [result (diff dom nil vb)]
(if (or (instance? Created result)
(instance? Updated result))
(set-property! dom dom-node k (:node result)))))))
nil
spec-b))
(defn diff
([dom a b]
(diff dom nil a b))
([dom k a b]
(let [refresh-node (fn [node compo-a compo-b]
(set-once! compo-b :dom-node node)
(diff-component dom node (:props compo-a) (:props compo-b))
node)
new-node (fn [compo]
(let [node (create-component! dom (:type compo))]
(assert node "No Node returned by create-component!")
(refresh-node node nil compo)))]
(condp = [(val-type a) (val-type b)]
[:nil :comp] (->Created (new-node b))
[:val :val] (if (= a b)
(->Noop b)
(->Updated b))
[:nil :val] (->Created b)
[:nil :ucomp] (diff dom nil (render-user-component b))
[:ucomp :nil] (diff dom (render-user-component a) nil)
[:ucomp :ucomp] (if (needs-update? a b)
(diff dom k (render-user-component a) (render-user-component b))
(->Noop (:dom-node (:render-result b))))
[:comp :comp] (-> (if (= (:type a) (:type b))
(doto (:dom-node a)
(assert (str "No DOM Node" (pr-str a)))
(refresh-node a b))
(new-node b))
->Updated)
[:comp :nil] (->Deleted (:dom-node a))
[:val :nil] (->Deleted a)))))
[dom a b]
(let [refresh-node (fn [node compo-a compo-b]
(set-once! compo-b :dom-node node)
(diff-component dom node (:props compo-a) (:props compo-b))
node)
new-node (fn [compo]
(let [node (create-component! dom (:type compo))]
(assert node "No Node returned by create-component!")
(refresh-node node nil compo)))]
(condp = [(val-type a) (val-type b)]
[:nil :comp] (->Created (new-node b))
[:val :val] (if (= a b)
(->Noop b)
(->Updated b))
[:nil :val] (->Created b)
[:nil :ucomp] (diff dom nil (render-user-component b))
[:ucomp :nil] (diff dom (render-user-component a) nil)
[:ucomp :ucomp] (if (needs-update? a b)
(diff dom (render-user-component a) (render-user-component b))
(->Noop (:dom-node (:render-result b))))
[:comp :comp] (-> (if (= (:type a) (:type b))
(doto (:dom-node a)
(assert (str "No DOM Node" (pr-str a)))
(refresh-node a b))
(new-node b))
->Updated)
[:comp :nil] (->Deleted (:dom-node a))
[:val :nil] (->Deleted a))))
(defn component
View
@@ -42,14 +42,7 @@
(let [^List lst (render-core/get-property (unwrap-promise parent) k)]
(doto lst
(.remove ^int idx)
(.add ^int idx (unwrap-promise new-child))))))
(delete-component! [this k child]
(run-later
(let [node (unwrap-promise child)
parent (unwrap-promise (.getParent node))
^List lst (render-core/get-property parent k)]
(.remove lst node)))))
(.add ^int idx (unwrap-promise new-child)))))))
(defrecord App [prev-state dom root handler-fn])
@@ -68,5 +61,3 @@
(defn update-app [{:keys [prev-state dom root handler-fn]} new-state]
(let [new-node (:node (time (diff dom prev-state new-state)))]
(->App new-state dom new-node handler-fn)))
ProTip! Use n and p to navigate between commits in a pull request.