-
-
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 ) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Update:
Here is a better and more useful pattern using Protocols and Records:
https://gist.github.com/idibidiart/0090a95b6b4f9c12c070