0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-08 07:50:43 -05:00

Improved cursored mixin.

This commit is contained in:
Andrey Antukh 2016-01-03 18:44:57 +02:00
parent 80ec20d9cd
commit f8a6342b98

View file

@ -22,23 +22,27 @@
(defn- cursored-did-mount (defn- cursored-did-mount
[state] [state]
(doseq [v (:rum/props state) (let [key (gensym "foobar")]
:when (satisfies? IWatchable v)] (doseq [v (:rum/props state)
(add-watch v (cursored-key state) :when (satisfies? IWatchable v)]
(fn [_ _ _ _] (add-watch v key
(rum/request-render (:rum/react-component state))))) (fn [_ _ _ _]
state) (rum/request-render (:rum/react-component state)))))
(assoc state ::key key)))
(defn- cursored-will-umount (defn- cursored-will-unmount
[state] [state]
(doseq [v (:rum/props state) (let [key (::key state)]
:when (satisfies? IWatchable v)] (doseq [v (:rum/props state)
(remove-watch v (cursored-key state))) :when (satisfies? IWatchable v)]
state) (remove-watch v key))
(dissoc state ::key)))
(defn- cursored-transfer-state (defn- cursored-transfer-state
[old new] [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 (defn- cursored-should-update
[old-state new-state] [old-state new-state]
@ -51,15 +55,14 @@
[dom (assoc next-state :rum/old-props (deref-props (:rum/props state)))]))) [dom (assoc next-state :rum/old-props (deref-props (:rum/props state)))])))
(def cursored (def cursored
"A cursor like mixin that works with {:transfer-state cursored-transfer-state
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
:should-update cursored-should-update :should-update cursored-should-update
:wrap-render cursored-wrap-render}) :wrap-render cursored-wrap-render})
(def cursored-watch
{:did-mount cursored-did-mount
:will-unmount cursored-will-unmount})
(defn local (defn local
"Adds an atom to components state that can be used as local state. "Adds an atom to components state that can be used as local state.
Atom is stored under key `:rum/local`. Atom is stored under key `:rum/local`.