-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.cljc
323 lines (267 loc) · 9.29 KB
/
action.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
(ns deepstate.action
#?(:clj
(:require
[helix.core :as-alias hx]
[helix.hooks :as-alias hooks]))
#?(:cljs
(:require
["react" :as react]
[promesa.core :as p]
[helix.core :as hx]
[helix.hooks :as hooks]
[deepstate.navigate :as nav]))
#?(:cljs
(:require-macros
[deepstate.action])))
#?(:cljs
(defmulti handle
"handle a possibly asynchronous action
macros such as
[[def-action]], [[def-state-action]] and
[[deepstate.action.async/def-async-action]] provide
sugar to create implementations of this multimethod
- `action` : the `action` map. must have key `::action`
which identifies the handler multimethod
returns:
`action-effects` = (f state) ->
{::state <state'>
::navigate <url>
::dispatch <ActionMap>|[<ActionMap>]
::later Promise<ActionEffects>}
i.e. handle returns a fn of `state` which, when invoked,
returns a map of (all optional) effects, including
`::state` - updated state
`::navigate` - a url to navigate to
`::dispatch` - further ActionMaps to dispatch
`::later` - a Promise of another ActionEffects fn"
(fn [{action-key ::action
:as _action}]
action-key)))
#?(:cljs
(defmethod handle :default
[action]
(js/console.warn "no handler matching action key: " (pr-str action))))
#?(:cljs
(declare internal-dispatch))
#?(:cljs
(defn ^:private apply-dispatch-effect
[action-context-val
dispatch-effect]
(cond
(map? dispatch-effect)
(internal-dispatch action-context-val dispatch-effect)
(sequential? dispatch-effect)
(doseq [deff dispatch-effect]
(internal-dispatch action-context-val deff))
:else
(throw
(ex-info
"unrecognised dispatch effect"
{:dispatch-effect dispatch-effect})))))
#?(:cljs
(defn ^:private action-effects-reducer-action
"return a reducer action which can be dispatched
to the underlying react reducer
`action-context-val` - action context value
`action-effects-fn` - a (fn <state>) from (handle <ActionMap>)"
[{react-dispatch ::react-dispatch
set-navurl ::set-navurl
:as action-context-val}
action-effects-fn]
(fn [state]
(let [{new-state-eff ::state
navigate-eff ::navigate
dispatch-eff ::dispatch
later-eff ::later
:as _effects} (action-effects-fn state)]
(js/console.debug
"action-effects-reducer-action"
(pr-str _effects))
(when (some? navigate-eff)
(set-navurl navigate-eff))
(when (some? dispatch-eff)
(apply-dispatch-effect action-context-val dispatch-eff))
(when (some? later-eff)
(p/handle
later-eff
(fn [succ err]
(react-dispatch
(action-effects-reducer-action
action-context-val
(if (some? err)
(fn [state] {::state (assoc state ::error err)})
succ))))))
;; return the updated state to the react reducer
(or new-state-eff state)))))
#?(:cljs
(defn ^:no-doc internal-dispatch
"dispatch an action to update the state
should not be called directly from components - a component
should use-action and call the dispatch fn returned, which
conveniently closes over the action-context-val
- `action-context-val` : the value from an action-context
provided by `action-context-provider`
- `action` : an ActionMap. must have key `::action` which
identifies the handler multimethod. if the action
is just a keyword it will be treated as the
`::action` key"
[{react-dispatch ::react-dispatch
:as action-context-val}
action]
(js/console.debug
"deepstate.action/dispatch"
;; (pr-str action-context-val)
(pr-str action))
(let [;; allow bare keywords as actions
action (if (keyword? action)
{::action action}
action)
action-effects-fn (handle action)]
;; (js/console.log "deepstate.action/dispatch action-effects"
;; (pr-str action-effects))
(react-dispatch
(action-effects-reducer-action
action-context-val
action-effects-fn))
true)))
#?(:cljs
#_{:clj-kondo/ignore [:unused-private-var]}
(defn ^:private action-fn-reducer
"new state is simply the fn `f` applied to the old `state`"
[state f]
(let [new-state (f state)]
;; (js/console.debug "deepstate.action/action-fn-reducer" (pr-str new-state))
new-state)))
#?(:cljs
#_{:clj-kondo/ignore [:unused-private-var]}
(defn ^:private make-action-context-val
"make the value passed around in an action-context ...
it encapsulates both the `state` and the
`dispatch` fn from a react `useState` hook"
[state react-dispatch set-navurl]
;; (js/console.warn "make-action-context-val")
{::state state
::react-dispatch react-dispatch
::set-navurl set-navurl}))
#?(:cljs
(defn create-action-context
"create the React `Context` object"
[]
(react/createContext)))
#?(:cljs
(defn use-action-context
"get the value from an `action-context`"
[ctx]
(react/useContext ctx)))
#?(:cljs
(defn use-action-dispatch
"get a dispatch fn
- `ctx` - the action context"
[ctx]
(let [ctx-val (react/useContext ctx)]
(partial internal-dispatch ctx-val))))
#?(:cljs
(defn use-action
"get a state value and dispatch fn
- `ctx` - the action context
- `path` - optional path into state for returned state value
returns:
[state dispatch]"
([ctx] (use-action ctx nil))
([ctx path]
(let [{state ::state
:as ctx-val} (react/useContext ctx)]
[(get-in state path)
(partial internal-dispatch ctx-val)]))))
#?(:cljs
(defn use-action-state
"extract the value at `path` (default `nil`) in the current `state`
from an `action-context`"
([ctx]
(use-action-state ctx nil))
([ctx path]
(let [{state ::state
:as _ctx-val} (react/useContext ctx)]
(get-in state path)))))
#?(:cljs
#_{:clj-kondo/ignore [:unused-private-var]}
(defn ^:private remove-action-keys
"remove keys from the ::action namespace, leaving
only the "
[action]
(->> action
(remove
(fn [[k _v]]
(= "deepstate.action"
(namespace k))))
(into {}))))
#?(:clj
(defmacro def-action
"define a generic action handler
- `key` : the action key
- `state-bindings` : fn bindings to destructure the state
- `action-bindings` : fn bindings to destructure the action
- `body` : any forms which return an action `handle` ActionEffects fn
(i.e. a (fn <state>)). can use the `action-bindings`"
[key
[state-bindings action-bindings]
& body]
`(defmethod handle ~key
[action#]
(let [~action-bindings (remove-action-keys action#)]
(fn [state#]
(let [~state-bindings state#]
~@body))))))
#?(:clj
(defmacro def-state-action
"define an action handler with only state effects
- `key` : the action key
- `state-bindings` : fn bindings to destructure the state
- `action-bindings` : fn bindings to destructure the action
- `body` : forms for a (fn <state>)->state .
can use the `action-bindings`"
[key
bindings
& body]
`(def-action
~key
~bindings
{::state ~@body})))
#?(:cljs
(hx/defnc ActionContextProvider
[{context :context
initial-arg :initial-arg
[child] :children}]
;; since this is where state is stored
{:helix/features {:fast-refresh true}}
(let [[state react-dispatch] (hooks/use-reducer action-fn-reducer initial-arg)
;; navurl will receive an optional url to navigate to after
;; an action is handled
[navurl set-navurl] (hooks/use-state nil)
val (make-action-context-val state react-dispatch set-navurl)
navigate (nav/navigator)]
;; if navigate is called during a render then we get an
;; error from react - so collect the forward url in navurl
;; and navigate after render
(hooks/use-effect
[navurl]
(when (some? navurl)
(js/console.info
"deepstate.action/action-context-provider navigating"
navurl)
(set-navurl nil)
(navigate navurl)))
(hx/provider
{:context context
:value val}
child))))
#?(:clj
(defmacro action-context-provider
"a ContextProvider element to provide an action-context
props:
- `context` - the `action-context` React context
- `initial-arg` - the initial `state` value
- `children` - only the first child will be included
as a child of the `ContextProvider` element"
[& args]
`(hx/$ ActionContextProvider ~@args)))