From f8a6342b980cda2003381d75bf46911f94bc6b03 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Sun, 3 Jan 2016 18:44:57 +0200 Subject: [PATCH] Improved cursored mixin. --- frontend/uxbox/ui/mixins.cljs | 39 +++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/frontend/uxbox/ui/mixins.cljs b/frontend/uxbox/ui/mixins.cljs index 1fef3530e..1a1c03c7a 100644 --- a/frontend/uxbox/ui/mixins.cljs +++ b/frontend/uxbox/ui/mixins.cljs @@ -22,23 +22,27 @@ (defn- cursored-did-mount [state] - (doseq [v (:rum/props state) - :when (satisfies? IWatchable v)] - (add-watch v (cursored-key state) - (fn [_ _ _ _] - (rum/request-render (:rum/react-component state))))) - state) + (let [key (gensym "foobar")] + (doseq [v (:rum/props state) + :when (satisfies? IWatchable v)] + (add-watch v key + (fn [_ _ _ _] + (rum/request-render (:rum/react-component state))))) + (assoc state ::key key))) -(defn- cursored-will-umount +(defn- cursored-will-unmount [state] - (doseq [v (:rum/props state) - :when (satisfies? IWatchable v)] - (remove-watch v (cursored-key state))) - state) + (let [key (::key state)] + (doseq [v (:rum/props state) + :when (satisfies? IWatchable v)] + (remove-watch v key)) + (dissoc state ::key))) (defn- cursored-transfer-state [old new] - (assoc new :rum/old-props (:rum/old-props old))) + (assoc new + :rum/old-props (:rum/old-props old) + ::key (::key old))) (defn- cursored-should-update [old-state new-state] @@ -51,15 +55,14 @@ [dom (assoc next-state :rum/old-props (deref-props (:rum/props state)))]))) (def cursored - "A cursor like mixin that works with - the `component` sugar syntax and lenses - from the cats library." - {:did-mount cursored-did-mount - :will-unmount cursored-will-umount - :transfer-state cursored-transfer-state + {:transfer-state cursored-transfer-state :should-update cursored-should-update :wrap-render cursored-wrap-render}) +(def cursored-watch + {:did-mount cursored-did-mount + :will-unmount cursored-will-unmount}) + (defn local "Adds an atom to component’s state that can be used as local state. Atom is stored under key `:rum/local`.