Skip to content

Instantly share code, notes, and snippets.

@mhuebert
Last active March 5, 2020 20:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mhuebert/eb930b25184944aa28721f323ba727bc to your computer and use it in GitHub Desktop.
Save mhuebert/eb930b25184944aa28721f323ba727bc to your computer and use it in GitHub Desktop.
useMutableSource with a Clojure atom - debugging example
;;
;; 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