-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
async_cljs.clj
230 lines (191 loc) · 6.87 KB
/
async_cljs.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
(ns com.wsscode.async.async-cljs
(:require [cljs.core.async :as async]
[clojure.spec.alpha :as s]))
(defmacro go
"Same as cljs.core.async/go. Just a convenience place for it."
[& body]
`(async/go ~@body))
(defmacro go-loop
"Same as `clojure.core.async/go-loop`. Just a convenience place for it."
[bindings & body]
`(async/go-loop ~bindings ~@body))
(defmacro go-catch
"Creates a go block that has a try/catch wrapping body, in case of errors the error
flows up as data instead triggering the exception."
[& body]
`(async/go
(try
~@body
(catch :default e# e#))))
(defmacro go-promise
"Creates a go block using a promise channel, so the output of the go block can be
read any number of times once ready."
[& body]
`(let [ch# (async/promise-chan)]
(async/go
(if-some [res# (try
~@body
(catch :default e# e#))]
(async/put! ch# res#)
(async/close! ch#)))
ch#))
(defmacro <!
"Same as clojure.core.async/<!!. Just a convenience place for it."
[& body]
`(async/<! ~@body))
(defmacro <!p
"Similar to core.async <!, but instead of taking a channel, <!p takes a Javascript
Promise, converts to a channel and reads, this allows the use of regular JS promises
inside go blocks using await like syntax.
Example:
(go ; start with go block
(-> (js/fetch \"some-url\") <!p ; call fetch and await for response
.text <!p ; await for text body reading
js/console.log))"
[promise]
`(consumer-pair (async/<! (promise->chan ~promise))))
(defmacro <?
"Reads a channel value and check if it is an error, in case it is, throw the error."
[ch]
`(throw-err (async/<! ~ch)))
(defmacro <?maybe
"Tries to await for a value, first if checks if x is a channel, if so will read
on it; then it checks if it's a JS promise, if so will convert it to a channel
and read from it. Otherwise returns x as is."
[x]
`(let [res# ~x]
(cond
(chan? res#)
(<? res#)
(promise? res#)
(<!p res#)
:else
res#)))
(defmacro <!maybe
"Reads a channel if it is a channel, if it's not a channel, return x."
[x]
`(let [res# ~x]
(if (chan? res#) (async/<! res#) res#)))
(defmacro let-chan
"Handles a possible channel on value."
[[name value] & body]
`(let [res# ~value]
(if (chan? res#)
(go-catch
(let [~name (<? res#)]
~@body))
(let [~name res#]
~@body))))
(defmacro let-chan*
"Like let-chan, but async errors will be returned instead of propagated"
[[name value] & body]
`(let [res# ~value]
(if (chan? res#)
(go-catch
(let [~name (async/<! res#)]
~@body))
(let [~name res#]
~@body))))
(defmacro pulling-retry
"Async pulling mechanism that will run body until ::done? is satisfied.
There two ways to call this helper:
Shorthand version: pass the ::done? function and the body:
(wa/pulling-retry int? (do-something))
Or the full version to specify all details
(wa/pulling-retry {::wa/done? int?
::wa/retry-ms 50
::wa/timeout 3000}
(do-something))"
[options & body]
`(pulling-retry* ~options (fn [] ~@body)))
(s/def ::timeout nat-int?)
(s/def ::done? fn?)
(s/def ::retry-ms nat-int?)
(s/fdef pulling-retry
:args (s/cat :options (s/or :detailed (s/keys :opt [::timeout ::done? ::retry-ms])
:quick any?)
:body (s/* any?)))
(defmacro async-test
"Creates an async block on the test, this helper uses the cljs.test async feature, the user body
will be wrapped around a `go` block automatically and the async done will be called
after the go block finishes it's execution. Example:
(deftest async-test
(wa/async-test
(is (= 42 (<! (some-async-op)))))
This will also add a timeout (default 2000ms), to change the timeout you can send
a map with configuration after the test symbol, example:
(deftest async-test
(wa/async-test
{::wa/timeout 5000} ; 5000ms timeout
(is (= 42 (<! (some-async-op)))))
"
[& body]
(let [[options body]
(if (map? (first body))
[(first body) (rest body)]
[{} body])
{::keys [timeout]} (merge {::timeout 2000} options)]
`(cljs.test/async done#
(let [timeout-ms# ~timeout]
(async/go
(let [timer# (async/timeout timeout-ms#)
[res# ch#] (async/alts! [(go-promise ~@body) timer#] :priority true)]
(if (= ch# timer#)
(cljs.test/is (= (str "Test timeout after " timeout-ms# "ms") false)))
(if (error? res#)
(cljs.test/is (= res# false)))
(done#)
res#))))))
(s/def ::go-try-stream-args
(s/cat :params (s/and vector? #(= 2 (count %)))
:body (s/+ any?)
:catch (s/spec (s/cat :catch #{'catch}
:error-type any?
:error-var symbol?
:catch-body (s/+ any?)))))
(defmacro go-try-stream
"If you want to read from a stream and catch errors in the messages that come
from it, this helper is for you.
The complication around adding try-catch on a go-loop is that we can't recur inside
try/catch. This helper will create a structure around it that will catch errors and recur
properly.
Usage:
(go-try-stream [value some-chan]
(do-operation-here)
(catch :default e
(report-error e))"
[& args]
(let [{:keys [params body catch]} (s/conform ::go-try-stream-args args)
{:keys [error-type error-var catch-body]} catch
[binding-key binding-value] params]
`(go-promise
(let [continue*# (volatile! true)]
(loop []
(try
(if-let [~binding-key (<? ~binding-value)]
(do
~@body)
(vreset! continue*# false))
(catch ~error-type ~error-var
~@catch-body))
(if @continue*#
(recur)))))))
(s/fdef go-try-stream
:args ::go-try-stream-args
:ret any?)
(defmacro deftest-async
"Define an async test, this helper uses the cljs.test async feature, the user body
will be wrapped around a `go` block automatically and the async done will be called
after the go block finishes it's execution. Example:
(wa/deftest-async async-test
(is (= 42 (<! (some-async-op))))
This will also add a timeout (default 2000ms), to change the timeout you can send
a map with configuration after the test symbol, example:
(wa/deftest-async async-test
{::wa/timeout 5000} ; 5000ms timeout
(is (= 42 (<! (some-async-op))))
If you want to use this with a different `deftest` constructor, use the `async-test`
macro."
[sym & body]
`(cljs.test/deftest ~sym
(async-test ~@body)))