Ini s a (def initial-state {:items [] :loading? false :error nil}) (defmulti products (fn [event _ _] event)) (defmethod products :init [_ _ state] (assoc state :products initial-state)) (defmethod products :fetch-by-category [_ [category] state] ;; fetch & dispatch on done ) Dis c ev t e Ad i t a t Han s c e t
Reg r co r r (def controllers {:products products}) (button {:on-click #(dispatch! :products :fetch-by-category "electronics")} "Show Electronics") Dis c ev
:id] :segment] [["contacts/" :segment-id "/profile/" :profile-id] :profile]]]) (bidi.bidi/match-route routes "/contacts/lkasd6812/profile/32hf53412") {:handler :profile :route-params {:segment-id "lkasd6812" :profile-id "32hf53412"}} Ro t ef i n Mat UR ag t o s o d s an Ret s a w m he ro
n () (def db (atom {:profiles {:items [] :loading? false} :settings {}})) (def profiles (rum/cursor-in db [:profiles])) (let [{:keys [items loading?]} (rum/react profiles)] (if loading? (render-spinner) (render-profiles items))) Ap ic o s su c t o Re c g o c a g shouldComponentUpdate is p, b at im b e
Re c (defn profile-names [n] (reduce-cursor-in db [:profiles :items] #(->> % (map :name) (take n)))) (let [items (rum/react (profile-names 100))] (render-profiles items)) Ag eg o f t o Ret s es r 100 p o l
a l Up a s e m i t (defmethod profile :update [_ [id old-name new-name] state] (->> (rpc "audience.updateProfileName" {:profile-id id :name new-name}) (on-done #(dispatch! :profile :update-ready id old-name %))) (update-profile-by-id id {:name new-name} state)) Dis c -do n er
Rol c fa (defmethod profile :update-ready [_ [id old-name resp] state] (if (error? resp) (update-profile-by-id id {:name old-name} state) state)) Do n n ot w e
[response] state] (if (either/right? response) (let [profiles (either/right-value response)] (assoc-in state [:profiles :items] profiles)) (assoc-in state [:profiles :error] (either/left-value response)))) Is i ? Un ap va & up s a Set ro h is
(either/map #(map :name %)) (either/map #(map trim %)) (either/map #(map capitalize %))) (->> resp (either/map #(map :events-attended %)) (either/map #(apply + %))) Ren n e Ren ve s t
& args] (let [curr-state @state next-state (swap! state #(apply-event ctrl event args %)) [before after] (clojure.data/diff curr-state next-state)] (console.log event args before after))) Com ne t Com di lo