Last active
March 5, 2020 20:03
-
-
Save mhuebert/eb930b25184944aa28721f323ba727bc to your computer and use it in GitHub Desktop.
useMutableSource with a Clojure atom - debugging example
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
;; | |
;; Simple ClojureScript example of useMutableSource | |
;; | |
;; (just basic usage - does not demonstrate multiple views on the same store) | |
(def my-store | |
(atom 0)) | |
(defn get-snapshot [store] | |
(prn :get-snapshot store) | |
@store) | |
(defn get-version [store] | |
(prn :get-version store) | |
;; NOTE | |
;; store is not passed in here - need to have an independent | |
;; reference to the store | |
@my-store) | |
(def my-source | |
(react/createMutableSource my-store get-version)) | |
(defn subscribe-to-my-store [store cb] | |
(prn :subscribe store) | |
(let [cb-id (goog/getUid cb)] | |
(add-watch store cb-id cb) | |
#(remove-watch store cb-id))) | |
(defn use-my-source [] | |
(react/useMutableSource my-source get-snapshot subscribe-to-my-store)) | |
(v/defview example-view [] | |
[:div | |
{:on-click #(swap! my-store inc)} | |
(str "Current value: " (use-my-source))]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment