Tag local state changes

to avoid passing remote changes back to remote
This commit is contained in:
Nicholas Kariniemi 2014-10-10 18:45:51 +03:00
parent 423b962d2c
commit 0f06d21767
5 changed files with 13 additions and 13 deletions

View file

@ -25,7 +25,7 @@
(dom/on-document-mousedown #(put! >events {:type :body-mousedown :event %}))
(dom/on-window-scroll #(put! >events {:type :body-scroll :event %}))))))
(defn render-app [initial-state remote-states local-states]
(defn render-app [initial-state <remote >remote]
(let [state (atom initial-state)
>events (chan)
<events (a/pub >events :type)
@ -36,8 +36,8 @@
:shared {:>events >events
:<events <events
:add-grubs-ch add-grubs-ch}
:tx-listen (fn [{:keys [new-state]} _]
(put! local-states new-state))})
(go (loop [] (when-let [new-state (<! remote-states)]
:tx-listen (fn [{:keys [new-state tag]} _]
(when (= tag :local) (put! >remote new-state)))})
(go (loop [] (when-let [new-state (<! <remote)]
(reset! state new-state)
(recur))))))

View file

@ -34,7 +34,7 @@
(om/set-state! owner :timeout-id timeout-id))
[:pressed :waiting] (js/clearTimeout (om/get-state owner :timeout-id))
[:editing :waiting] (let [grub (om/get-props owner)]
(om/transact! grub #(assoc % :text (om/get-state owner :grub-text))))
(om/transact! grub nil #(assoc % :text (om/get-state owner :grub-text)) :local))
nil)
(when-not (= current next) (om/set-state! owner :edit-state next))))
@ -58,7 +58,7 @@
(when (= edit-state :pressed) "grub-active")
(when (= edit-state :editing) "edit")]
:on-click (fn [e] (when (#{:waiting :pressed} edit-state)
(om/transact! grub #(assoc % :completed (not completed)))
(om/transact! grub nil #(assoc % :completed (not completed)) :local)
(.blur (om/get-node owner :grub-input))))
:on-mouse-down #(transition-state owner :mouse-down)
:on-mouse-up #(transition-state owner :mouse-up)

View file

@ -22,7 +22,7 @@
(when (not (empty? new-grub-text))
(let [new-grub (grub-view/new-grub new-grub-text)]
(om/set-state! owner :new-grub-text "")
(om/transact! grubs #(assoc % (:id new-grub) new-grub)))))
(om/transact! grubs nil #(assoc % (:id new-grub) new-grub) :local))))
(defn view [grubs owner]
(reify
@ -57,7 +57,7 @@
{:id "clear-all-btn"
:class (when (empty? grubs) "hidden")
:type "button"
:on-click #(om/update! grubs {})}
:on-click #(om/update! grubs nil {} :local)}
"Clear all"]])))
om/IWillMount
(will-mount [_]
@ -66,10 +66,10 @@
(go-loop []
(let [grubs-map (<! add-grubs-ch)]
(when-not (nil? grubs-map)
(om/transact! grubs #(merge % grubs-map))
(om/transact! grubs nil #(merge % grubs-map) :local)
(recur))))
(go-loop []
(let [id (<! remove-grub-ch)]
(when-not (nil? id)
(om/transact! grubs #(dissoc % id))
(om/transact! grubs nil #(dissoc % id) :local)
(recur))))))))

View file

@ -38,7 +38,7 @@
name (om/get-state owner :name)
grubs (om/get-state owner :grubs)]
(om/transact! recipe #(assoc % :name name :grubs grubs)))
(om/transact! recipe nil #(assoc % :name name :grubs grubs) :local))
nil)
(when-not (= current next) (om/set-state! owner :edit-state next))))

View file

@ -16,7 +16,7 @@
new-recipe (recipe/new-recipe name grubs)]
(om/set-state! owner :new-recipe-name "")
(om/set-state! owner :new-recipe-grubs "")
(om/transact! recipes #(assoc % (:id new-recipe) new-recipe)))))
(om/transact! recipes nil #(assoc % (:id new-recipe) new-recipe) :local))))
(def transitions
{:waiting {:click :editing}
@ -113,4 +113,4 @@
(go-loop []
(let [removed-id (<! remove-recipe-ch)]
(when-not (nil? removed-id)
(om/transact! recipes #(dissoc % removed-id)))))))))
(om/transact! recipes nil #(dissoc % removed-id) :local))))))))