|
8 | 8 |
|
9 | 9 | [goog.dom :as gdom]
|
10 | 10 | [reagent.dom :as rdom]
|
11 |
| - [re-frame.core :refer [reg-event-fx reg-event-db dispatch reg-sub subscribe reg-cofx inject-cofx] :as rf] |
| 11 | + [re-frame.core :as rf] |
12 | 12 |
|
13 |
| - [jtk-dvlp.re-frame.async-coeffects :refer [reg-acofx reg-acofx-by-fx inject-acofx]])) |
| 13 | + [jtk-dvlp.re-frame.async-coeffects :as rf-acofxs])) |
14 | 14 |
|
15 | 15 |
|
16 |
| -(reg-cofx ::now |
| 16 | +(rf/reg-cofx ::now |
17 | 17 | (fn [coeffects]
|
18 | 18 | (println "cofx now")
|
19 | 19 | (assoc coeffects ::now (js/Date.))))
|
20 | 20 |
|
21 |
| -(reg-acofx ::async-now |
| 21 | +(rf-acofxs/reg-acofx ::async-now |
22 | 22 | (fn [coeffects delay-in-ms]
|
23 | 23 | (go
|
24 | 24 | (let [delay-in-ms
|
|
35 | 35 | (println "acofx async-now finished" delay-in-ms)
|
36 | 36 | (assoc coeffects ::async-now (- (.getTime (js/Date.))(.getTime start)),)))))
|
37 | 37 |
|
38 |
| -(reg-acofx-by-fx ::github-repo-meta |
| 38 | +(rf-acofxs/reg-acofx-by-fx ::github-repo-meta |
39 | 39 | :http-xhrio
|
40 | 40 | :on-success
|
41 | 41 | :on-failure
|
42 | 42 | {:method :get
|
43 | 43 | :uri "https://api.github.com/repos/jtkDvlp/re-frame-async-coeffects"
|
44 | 44 | :response-format (ajax/json-response-format {:keywords? true})})
|
45 | 45 |
|
46 |
| -(reg-acofx-by-fx ::http-request |
| 46 | +(rf-acofxs/reg-acofx-by-fx ::http-request |
47 | 47 | :http-xhrio
|
48 | 48 | :on-success
|
49 | 49 | :on-failure
|
50 | 50 | {:method :get
|
51 | 51 | :response-format (ajax/json-response-format {:keywords? true})})
|
52 | 52 |
|
53 |
| -(reg-event-fx ::do-async-stuff |
54 |
| - [(inject-acofx |
55 |
| - {:acofxs |
56 |
| - {::async-now |
57 |
| - ::async-now |
| 53 | +(rf-acofxs/set-global-error-dispatch! [::change-message "ahhhhhh!"]) |
58 | 54 |
|
59 |
| - ::async-now-5-secs-delayed |
60 |
| - [::async-now |
61 |
| - 5000] |
| 55 | +(rf/reg-event-fx ::do-work-with-async-stuff |
| 56 | + [(rf-acofxs/inject-acofx ::async-now) ; Inject one single acofx without error-dispatch (global set error-dispatch will be used) |
| 57 | + (rf-acofxs/inject-acofxs ; Inject multiple acofxs and renames keys within coeffects map. |
| 58 | + {::async-now* |
| 59 | + ::async-now |
62 | 60 |
|
63 |
| - ::async-now-x-secs-delayed |
64 |
| - [::async-now |
65 |
| - (fn [{:keys [db]}] [(get db ::delay 0)])] |
| 61 | + ::async-now-5-secs-delayed |
| 62 | + [::async-now 5000] ; Inject with one value arg |
66 | 63 |
|
67 |
| - ::async-now-xDIV2-secs-delayed |
68 |
| - [::async-now |
69 |
| - (fn [{:keys [db]} multiply] [(* multiply (get db ::delay 0))]) |
70 |
| - 0.5] |
| 64 | + ::async-now-x-secs-delayed |
| 65 | + [::async-now #(get-in % [:db ::delay] 0)] ; Inject with one fn arg |
71 | 66 |
|
72 |
| - ::github-repo-meta |
73 |
| - ::github-repo-meta |
| 67 | + ::github-repo-meta |
| 68 | + ::github-repo-meta |
74 | 69 |
|
75 |
| - ::re-frame-tasks-meta |
76 |
| - [::http-request {:uri "https://api.github.com/repos/jtkDvlp/re-frame-tasks"}] |
| 70 | + ::re-frame-tasks-meta |
| 71 | + [::http-request {:uri "https://api.github.com/repos/jtkDvlp/re-frame-tasks"}] |
77 | 72 |
|
78 |
| - ::core.async-helpers-meta |
79 |
| - [::http-request {:uri "https://api.github.com/repos/jtkDvlp/core.async-helpers"}]} |
| 73 | + ::core.async-helpers-meta |
| 74 | + [::http-request {:uri "https://api.github.com/repos/jtkDvlp/core.async-helpers"}]} |
80 | 75 |
|
81 |
| - :error-dispatch [::change-message "ahhhhhh!"]} |
| 76 | + {:error-dispatch [::change-message "ahhhhhh!"]} ; Overrides global set error-dispatch for these acofxs |
82 | 77 | ,,,)
|
83 |
| - (inject-cofx ::now)] |
| 78 | + (rf/inject-cofx ::now) ; Inject normal cofx |
| 79 | + ] |
84 | 80 | (fn [{:keys [db] :as cofxs} _]
|
85 | 81 | (let [async-computed-results
|
86 | 82 | (-> cofxs
|
|
91 | 87 |
|
92 | 88 | {:db
|
93 | 89 | (-> db
|
94 |
| - (update ::async-computed-results (fnil conj []) async-computed-results) |
| 90 | + (assoc ::async-computed-results async-computed-results) |
95 | 91 | (assoc ::message nil))})))
|
96 | 92 |
|
97 |
| -(reg-sub ::async-computed-results |
| 93 | +(rf/reg-sub ::async-computed-results |
98 | 94 | (fn [db]
|
99 |
| - (reverse (::async-computed-results db)))) |
| 95 | + (::async-computed-results db))) |
100 | 96 |
|
101 |
| -(reg-event-db ::change-delay |
| 97 | +(rf/reg-event-db ::change-delay |
102 | 98 | (fn [db [_ delay]]
|
103 | 99 | (assoc db ::delay delay)))
|
104 | 100 |
|
105 |
| -(reg-sub ::delay |
| 101 | +(rf/reg-sub ::delay |
106 | 102 | (fn [db]
|
107 | 103 | (::delay db 0)))
|
108 | 104 |
|
109 |
| -(reg-event-db ::change-message |
| 105 | +(rf/reg-event-db ::change-message |
110 | 106 | (fn [db [_ message & more]]
|
111 | 107 | (assoc db ::message [message more])))
|
112 | 108 |
|
113 |
| -(reg-sub ::message |
| 109 | +(rf/reg-sub ::message |
114 | 110 | (fn [db]
|
115 | 111 | (::message db)))
|
116 | 112 |
|
117 | 113 | (defn app-view
|
118 | 114 | []
|
119 | 115 | [:<>
|
120 |
| - [:p (str @(subscribe [::message]))] |
| 116 | + [:p (str @(rf/subscribe [::message]))] |
121 | 117 | [:button
|
122 |
| - {:on-click #(dispatch [::do-async-stuff])} |
123 |
| - "take timestamp"] |
| 118 | + {:on-click #(rf/dispatch [::do-work-with-async-stuff])} |
| 119 | + "do work"] |
124 | 120 | [:input
|
125 | 121 | {:type :number
|
126 |
| - :value @(subscribe [::delay]) |
| 122 | + :value @(rf/subscribe [::delay]) |
127 | 123 | :on-change #(rf/dispatch-sync [::change-delay (-> % .-target .-value)])}]
|
128 |
| - [:ul |
129 |
| - (for [timestamp @(subscribe [::async-computed-results])] |
130 |
| - ^{:key timestamp} |
131 |
| - [:li |
132 |
| - [:pre |
133 |
| - (with-out-str (cljs.pprint/pprint timestamp))]])]]) |
| 124 | + [:pre |
| 125 | + (with-out-str (cljs.pprint/pprint @(rf/subscribe [::async-computed-results])))]]) |
134 | 126 |
|
135 | 127 |
|
136 | 128 | ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
0 commit comments