Created
March 6, 2018 11:30
-
-
Save achikin/22e48dfca6a1a14c752b88b06783ec6c to your computer and use it in GitHub Desktop.
Test of subscription watchers
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 whatever.sub-test | |
(:require [reagent.core :as r] | |
[reagent.ratom :as ratom] | |
[re-frame.core :as rf])) | |
(rf/reg-sub :test (fn [db] (:test db))) | |
(rf/reg-event-db :set-test (fn [db [_ newval]] (assoc db :test newval))) | |
(rf/reg-event-db :set-test-changed (fn [db [_ newval]] (assoc db :test-changed newval))) | |
(rf/reg-sub :test-changed (fn [db _] (:test-changed db))) | |
(rf/reg-event-db :change-d (fn [db [_ newd]] | |
(assoc-in db [:test :b 1 :d] newd))) | |
(defn dangerous-games [] | |
(let [changed (rf/subscribe [:test-changed]) | |
test (rf/subscribe [:test]) | |
_ (add-watch test :change | |
(fn [_ _ _ _] (rf/dispatch [:set-test-changed true])))] | |
[:div | |
[:button {:on-click #(rf/dispatch [:set-test {:a 1 :b [{:c 3} {:d 4}]}])} | |
"Set initial state"] | |
[:button {:on-click #(rf/dispatch [:change-d (rand-int 10)])} | |
"Change field in :test randomly"] | |
[:button {:on-click #(rf/dispatch [:set-test-changed false])} | |
"Reset :test-changed"] | |
[:div ":test " (str @test)] | |
[:div ":test-changed " (str @changed)]])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment