-
Notifications
You must be signed in to change notification settings - Fork 0
/
async.cljc
241 lines (201 loc) · 7.97 KB
/
async.cljc
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
230
231
232
233
234
235
236
237
238
239
240
241
(ns deepstate.action.async
#?(:clj
(:require
[deepstate.action :as-alias action])
:cljs
(:require
[promesa.core :as p]
[deepstate.action :as action]))
#?(:cljs
(:require-macros
[deepstate.action.async])))
#?(:cljs
(defn ^:no-doc navigate-on-condition
"reaction helper - navigate when (pred <action-status>) is true"
([async-action-state url pred]
(navigate-on-condition {} async-action-state url pred))
([effs
{status ::action/status
:as _async-action-state}
url
pred]
(if (pred status)
(assoc effs ::action/navigate url)
effs))))
#?(:cljs
(defn navigate-on-success
"reaction helper - navigate only on success"
([async-action-state url]
(navigate-on-success {} async-action-state url))
([effs async-action-state url]
(navigate-on-condition effs async-action-state url #(= ::action/success %)))))
#?(:cljs
(defn navigate-on-error
"reaction helper - navigate only on error"
([async-action-state url]
(navigate-on-error {} async-action-state url))
([effs async-action-state url]
(navigate-on-condition effs async-action-state url #(= ::action/error %)))))
#?(:cljs
(defn navigate-on-inflight
"reaction helper - navigate only when inflight"
([async-action-state url]
(navigate-on-inflight {} async-action-state url))
([effs async-action-state url]
(navigate-on-condition effs async-action-state url #(= ::action/inflight %)))))
#?(:cljs
(defn navigate-always
"reaction helper - always navigate"
([async-action-state url]
(navigate-always {} async-action-state url))
([effs async-action-state url]
(navigate-on-condition effs async-action-state url (constantly true)))))
#?(:cljs
(defn ^:private get-action-path
"determine the path of the async-action-state in the global state"
[key
{action-path ::action/path
:as action}]
(or action-path
(cond
(sequential? key) (vec key)
(keyword? key) [key]
:else
(throw
(ex-info "key is not a seq or keyword"
{:key key
:action action}))))))
#?(:cljs
(defn ^:no-doc get-async-action-state
"fetch async-action-state from global state"
[key state action]
(get-in state (get-action-path key action))))
#?(:cljs
(defn async-action-handler
"a promise-based async action handler providing a consistent
format for handling and recording emerging `async-action-state`, and
for interacting with navigation
- `key` : the `::action/key` to match a `dispatch`. identifies the action,
and is the default path of the `async-action-state` in `state`
- `state` : the global state
- `action` : the action value being handled
- `::action/path` - instead of updating the `state` at `key`,
update the `state` at `path`
- `async-action-data-promise` : a promise of the action data
- `reactino-fn` : a (fn <state>) to be called when the promise completes,
returning an effects map
`async-action-state` will be updated at `::action-path` with a map
with these keys:
`::action/status` - ::inflight, ::success or ::error
`::action/action` - the action value
`::action/data` - the happy-path result of the action
`::action/error` - any error value"
[key
state
action
async-action-data-promise
reaction-fn]
(let [ap (get-action-path key action)
init-state (update-in state ap
merge {::action/status ::action/inflight
::action/action action})]
;; (js/console.info "async-action" (pr-str action-map))
{::action/state init-state
::action/later
(p/handle
async-action-data-promise
(fn [r e]
(fn [state]
(let [new-state (update-in
state
ap
merge
(if (some? e)
{::action/status ::action/error
::action/error e}
{::action/status ::action/success
::action/data r
::action/error nil}))
effs (reaction-fn new-state)]
;; allow the reaction definition full control of
;; state changes
(merge
{::action/state new-state}
effs)))))})))
#?(:clj
(defmacro async-action-bindings
"set up bindings for an async action definition"
[key
[state-bindings async-action-state-bindings action-bindings]
state
action
& body]
`(let [~state-bindings ~state
~async-action-state-bindings (get-async-action-state ~key ~state ~action)
~action-bindings (action/remove-action-keys ~action)]
~@body)))
#?(:clj
(defmacro def-async-action-handler
"a macro to establish bindings used by other async-action macros.
both [[def-async-action]] and [[deepstate.action.axios/def-axios-action]]
defer to this macro to establish bindings"
[key
[_state-bindings _async-action-state-bindings _action-bindings :as bindings]
async-action-data-promise
reaction-map
handler-fn]
`(defmethod action/handle ~key
[action#]
(fn [state#]
(let [async-action-data-promise# (async-action-bindings
~key
~bindings
state#
action#
~async-action-data-promise)
;; the reaction-fn can use the same bindings as the
;; action-data-promise, but will be invoked later in
;; reaction to the promise completing
reaction-fn# (fn [reaction-state#]
(async-action-bindings
~key
~bindings
reaction-state#
action#
~reaction-map))]
(~handler-fn
~key
state#
action#
async-action-data-promise#
reaction-fn#))))))
#?(:clj
(defmacro def-async-action
"define an action handler to service a promise-based async action
an `async-action-state` map will be initialised at a path in the global
`state` map, and updated after the action has completed.
`async-action-state` will have shape:
{`::action/status` `::inflight|::success|::error`
`::action/action` `<action-map>`
`::action/data` `<action-data>`
`::action/error` `<action-error>`}
- `key` : the action key and the default path in the `state` for
the `async-action-state`
- `state-bindings` : fn bindings to destructure the global `state` map
- `async-action-state-bindings` : fn bindings to destructure the `async-action-state` map
- `action-bindings` : fn bindings to destructure the `action` map
- `async-action-data-promise` : form returning a promise of the
`<action-data>` - may use any established bindings
- `reaction-map` : form which will be evaluated when promise completes
returning effects including `::action/state`, `::action/dispatch` and
`::action/navigate`"
[key
[_state-bindings _async-action-state-bindings _action-bindings :as bindings]
async-action-data-promise
reaction-map]
`(def-async-action-handler
~key
~bindings
~async-action-data-promise
~reaction-map
async-action-handler)))