-
-
Save idibidiart/2b3aa1594ce707227b96 to your computer and use it in GitHub Desktop.
In and Out Transforms with Reagent Cursor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns main.hello | |
(:require | |
[clojure.walk :as walk] | |
[reagent.core :as reagent :refer [atom]])) | |
(enable-console-print!) | |
; example state ratom | |
(def state (atom {:parentX { | |
:childX 42 | |
} | |
:parentY { | |
:childY 23 | |
} | |
:parentZ { | |
:childZ 7 | |
} | |
})) | |
(defmulti set-multi | |
(fn [k v] k)) | |
(defmulti get-multi | |
(fn [k] k)) | |
(defn set-get-multi | |
([k] (get-multi k)) | |
([k v] (set-multi k v))) | |
;; cursors | |
(def cursorX (reagent/cursor set-get-multi [:parentX :childX])) | |
(def cursorY (reagent/cursor set-get-multi [:parentY :childY])) | |
(def cursorZ (reagent/cursor set-get-multi [:parentZ :childZ])) | |
(def cursorY-No-Out-Transform (reagent/cursor state [:parentY :childY])) | |
;; update function | |
(defn update! [s v] | |
(reset! s v)) | |
;; transformers | |
(defn double-it [v] | |
(* v 2)) | |
(defn make-keymap [string-map] | |
(walk/keywordize-keys string-map)) | |
;; in-transforms, for matched paths and default | |
(defmethod set-multi :default [k v] | |
(swap! state assoc-in k v)) | |
(defmethod set-multi [:parentX :childX] [k v] | |
(swap! state assoc-in k (double-it v))) | |
(defmethod set-multi [:parentZ :childZ] [k v] | |
(swap! state assoc-in k (make-keymap v))) | |
;; out-transforms, for matched paths and default | |
(defmethod get-multi :default [k] | |
(get-in @state k)) | |
(defmethod get-multi [:parentY :childY] [k] | |
(double-it (get-in @state k))) | |
;; use the cursor to set a value | |
(update! cursorX 6) | |
;; log state to test in-transform | |
(println "in-transform changes actual value of cursor: node pointed to by cursorX is now 12 (2X value) after being set to 6 \n" @state) | |
;; node pointed to by cursorX is now 12 (2X value) after being set to 6 | |
;; log cursor to test out-transform | |
(println "out-transform changes return value: cursorY returns 2X its actual value of 23, --> " @cursorY) | |
;; cursorY returns 2X its actual value | |
;; log state to test in-transform | |
(println "out-transform doesn't change actual value of cursor: node pointed to by cursorY still shows 23 as value \n" @state) | |
;; node pointed to by cursorY is still 23 | |
(println "You can still get at cursorY's actual value via @cursorY-No-Transform " @cursorY-No-Out-Transform) | |
;; set curzorZ to string map | |
(update! cursorZ {"x" 5 "y" 6}) | |
;; out-transform will keywordize the map | |
(println @cursorZ ) | |
Update:
Here is a better and more useful pattern using Protocols and Records:
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
During our last SF Reagent meetup (http://www.meetup.com/Reactive-ClojureScript/events/221710610/ -- I still owe you a professionally produced video), there was this interesting perception that cursors in Reagent were just pointers and therefore couldn't implement any transformation on the data. Maybe I misunderstood the context but that's what I recall being said.
So I put together a quick example of how to implement inward and outward transforms on app state atom using Reagent cursors, which are quite powerful.
Disclaimer: I literally just learned about multi-methods a couple days ago so I'm pretty sure I'm misusing them in some way, but that shouldn't take away from the basic fact that we can do inward and outward transformations on state using just cursors.
I've asked a few more experienced folks to educate me on the various mistakes I've made and explain any failings of this abstraction or help improve it. I'll post any updates and contributions to this thread, or you may post your own right here.