Skip to content

Commit eb5123b

Browse files
committed
Betters readme and example code
1 parent fd3aef9 commit eb5123b

File tree

2 files changed

+63
-74
lines changed

2 files changed

+63
-74
lines changed

README.md

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ re-frame interceptors to register and inject async actions as coeffects for even
1010
* register async coeffects
1111
* inject one or more async coeffects to events
1212
* multiple async coeffects will be synced for event calls (concurrent processing)
13+
* supports error handling via error dispatch vector (can also be set globally)
1314
* convert effects like [http-fx](https://github.com/day8/re-frame-http-fx) to async coeffect
1415

1516
## Motivation
@@ -99,10 +100,10 @@ See in repo [your-project.cljs](https://github.com/jtkDvlp/re-frame-async-coeffe
99100
(ns jtk-dvlp.your-project
100101
(:require
101102
...
102-
[jtk-dvlp.re-frame.async-coeffects :refer [reg-acofx reg-acofx-by-fx inject-acofx]]))
103+
[jtk-dvlp.re-frame.async-coeffects :as rf-acofxs]))
103104

104105

105-
(reg-acofx ::async-now
106+
(rf-acofxs/reg-acofx ::async-now
106107
(fn [coeffects delay-in-ms]
107108
(go
108109
(let [delay-in-ms
@@ -119,52 +120,48 @@ See in repo [your-project.cljs](https://github.com/jtkDvlp/re-frame-async-coeffe
119120
(println "acofx async-now finished" delay-in-ms)
120121
(assoc coeffects ::async-now (- (.getTime (js/Date.))(.getTime start)),)))))
121122

122-
(reg-acofx-by-fx ::github-repo-meta
123+
(rf-acofxs/reg-acofx-by-fx ::github-repo-meta
123124
:http-xhrio
124125
:on-success
125126
:on-failure
126127
{:method :get
127128
:uri "https://api.github.com/repos/jtkDvlp/re-frame-async-coeffects"
128129
:response-format (ajax/json-response-format {:keywords? true})})
129130

130-
(reg-acofx-by-fx ::http-request
131+
(rf-acofxs/reg-acofx-by-fx ::http-request
131132
:http-xhrio
132133
:on-success
133134
:on-failure
134135
{:method :get
135136
:response-format (ajax/json-response-format {:keywords? true})})
136137

137-
(reg-event-fx ::do-async-stuff
138-
[(inject-acofx
139-
{:acofxs
140-
{::async-now
141-
::async-now
138+
(rf-acofxs/set-global-error-dispatch! [::change-message "ahhhhhh!"])
142139

143-
::async-now-5-secs-delayed
144-
[::async-now
145-
5000]
140+
(rf/reg-event-fx ::do-work-with-async-stuff
141+
[(rf-acofxs/inject-acofx ::async-now) ; Inject one single acofx without error-dispatch (global set error-dispatch will be used)
142+
(rf-acofxs/inject-acofxs ; Inject multiple acofxs and renames keys within coeffects map.
143+
{::async-now*
144+
::async-now
146145

147-
::async-now-x-secs-delayed
148-
[::async-now
149-
(fn [{:keys [db]}] [(get db ::delay 0)])]
146+
::async-now-5-secs-delayed
147+
[::async-now 5000] ; Inject with one value arg
150148

151-
::async-now-xDIV2-secs-delayed
152-
[::async-now
153-
(fn [{:keys [db]} multiply] [(* multiply (get db ::delay 0))])
154-
0.5]
149+
::async-now-x-secs-delayed
150+
[::async-now #(get-in % [:db ::delay] 0)] ; Inject with one fn arg
155151

156-
::github-repo-meta
157-
::github-repo-meta
152+
::github-repo-meta
153+
::github-repo-meta
158154

159-
::re-frame-tasks-meta
160-
[::http-request {:uri "https://api.github.com/repos/jtkDvlp/re-frame-tasks"}]
155+
::re-frame-tasks-meta
156+
[::http-request {:uri "https://api.github.com/repos/jtkDvlp/re-frame-tasks"}]
161157

162-
::core.async-helpers-meta
163-
[::http-request {:uri "https://api.github.com/repos/jtkDvlp/core.async-helpers"}]}
158+
::core.async-helpers-meta
159+
[::http-request {:uri "https://api.github.com/repos/jtkDvlp/core.async-helpers"}]}
164160

165-
:error-dispatch [::change-message "ahhhhhh!"]}
161+
{:error-dispatch [::change-message "ahhhhhh!"]} ; Overrides global set error-dispatch for these acofxs
166162
,,,)
167-
(inject-cofx ::now)]
163+
(rf/inject-cofx ::now) ; Inject normal cofx
164+
]
168165
(fn [{:keys [db] :as cofxs} _]
169166
(let [async-computed-results
170167
(-> cofxs
@@ -175,7 +172,7 @@ See in repo [your-project.cljs](https://github.com/jtkDvlp/re-frame-async-coeffe
175172

176173
{:db
177174
(-> db
178-
(update ::async-computed-results (fnil conj []) async-computed-results)
175+
(assoc ::async-computed-results async-computed-results)
179176
(assoc ::message nil))})))
180177
```
181178

dev/jtk_dvlp/your_project.cljs

Lines changed: 38 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@
88

99
[goog.dom :as gdom]
1010
[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]
1212

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]))
1414

1515

16-
(reg-cofx ::now
16+
(rf/reg-cofx ::now
1717
(fn [coeffects]
1818
(println "cofx now")
1919
(assoc coeffects ::now (js/Date.))))
2020

21-
(reg-acofx ::async-now
21+
(rf-acofxs/reg-acofx ::async-now
2222
(fn [coeffects delay-in-ms]
2323
(go
2424
(let [delay-in-ms
@@ -35,52 +35,48 @@
3535
(println "acofx async-now finished" delay-in-ms)
3636
(assoc coeffects ::async-now (- (.getTime (js/Date.))(.getTime start)),)))))
3737

38-
(reg-acofx-by-fx ::github-repo-meta
38+
(rf-acofxs/reg-acofx-by-fx ::github-repo-meta
3939
:http-xhrio
4040
:on-success
4141
:on-failure
4242
{:method :get
4343
:uri "https://api.github.com/repos/jtkDvlp/re-frame-async-coeffects"
4444
:response-format (ajax/json-response-format {:keywords? true})})
4545

46-
(reg-acofx-by-fx ::http-request
46+
(rf-acofxs/reg-acofx-by-fx ::http-request
4747
:http-xhrio
4848
:on-success
4949
:on-failure
5050
{:method :get
5151
:response-format (ajax/json-response-format {:keywords? true})})
5252

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!"])
5854

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
6260

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
6663

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
7166

72-
::github-repo-meta
73-
::github-repo-meta
67+
::github-repo-meta
68+
::github-repo-meta
7469

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"}]
7772

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"}]}
8075

81-
:error-dispatch [::change-message "ahhhhhh!"]}
76+
{:error-dispatch [::change-message "ahhhhhh!"]} ; Overrides global set error-dispatch for these acofxs
8277
,,,)
83-
(inject-cofx ::now)]
78+
(rf/inject-cofx ::now) ; Inject normal cofx
79+
]
8480
(fn [{:keys [db] :as cofxs} _]
8581
(let [async-computed-results
8682
(-> cofxs
@@ -91,46 +87,42 @@
9187

9288
{:db
9389
(-> db
94-
(update ::async-computed-results (fnil conj []) async-computed-results)
90+
(assoc ::async-computed-results async-computed-results)
9591
(assoc ::message nil))})))
9692

97-
(reg-sub ::async-computed-results
93+
(rf/reg-sub ::async-computed-results
9894
(fn [db]
99-
(reverse (::async-computed-results db))))
95+
(::async-computed-results db)))
10096

101-
(reg-event-db ::change-delay
97+
(rf/reg-event-db ::change-delay
10298
(fn [db [_ delay]]
10399
(assoc db ::delay delay)))
104100

105-
(reg-sub ::delay
101+
(rf/reg-sub ::delay
106102
(fn [db]
107103
(::delay db 0)))
108104

109-
(reg-event-db ::change-message
105+
(rf/reg-event-db ::change-message
110106
(fn [db [_ message & more]]
111107
(assoc db ::message [message more])))
112108

113-
(reg-sub ::message
109+
(rf/reg-sub ::message
114110
(fn [db]
115111
(::message db)))
116112

117113
(defn app-view
118114
[]
119115
[:<>
120-
[:p (str @(subscribe [::message]))]
116+
[:p (str @(rf/subscribe [::message]))]
121117
[:button
122-
{:on-click #(dispatch [::do-async-stuff])}
123-
"take timestamp"]
118+
{:on-click #(rf/dispatch [::do-work-with-async-stuff])}
119+
"do work"]
124120
[:input
125121
{:type :number
126-
:value @(subscribe [::delay])
122+
:value @(rf/subscribe [::delay])
127123
: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])))]])
134126

135127

136128
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

0 commit comments

Comments
 (0)