Skip to content

Commit c23331c

Browse files
committed
Adds global error dispatch and changes arg structure
1 parent edcda83 commit c23331c

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

src/jtk_dvlp/re_frame/async_coeffects.cljs

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,31 @@
7070
(defonce ^:private !results
7171
(atom {}))
7272

73+
(def !global-error-dispatch
74+
(atom nil))
75+
76+
(def set-global-error-dispatch!
77+
"Sets global error dispatch to prevent using advanced structure for `inject-acofx`."
78+
(partial reset! !global-error-dispatch))
79+
7380
(defn- fx-handler-run?
7481
[{:keys [stack]}]
7582
(->> stack
7683
(filter #(= :fx-handler (:id %)))
7784
(seq)))
7885

86+
(defn- compute-argument
87+
[{:keys [event] :as coeffects} arg-or-fn]
88+
(if (fn? arg-or-fn)
89+
(arg-or-fn coeffects event)
90+
arg-or-fn))
91+
7992
(defn- run-acofx!
80-
[coeffects {:keys [id data-id handler args-fn]}]
93+
[coeffects {:keys [id data-id handler args]}]
8194
(go
8295
(let [result
83-
(->> coeffects
84-
(args-fn)
96+
(->> args
97+
(mapv (partial compute-argument coeffects))
8598
(apply handler coeffects)
8699
(<!)
87100
(#(get % id)))]
@@ -92,7 +105,10 @@
92105
[{:keys [acoeffects coeffects] :as context}
93106
{:keys [error-dispatch acofxs] inject-id :id}]
94107

95-
(let [dispatch-id
108+
(let [error-dispatch
109+
(or error-dispatch @!global-error-dispatch)
110+
111+
dispatch-id
96112
(:dispatch-id acoeffects)
97113

98114
event
@@ -142,15 +158,12 @@
142158
:handler (get-handler kind cofx true)
143159
:args-fn (constantly nil)}
144160

145-
(and (vector? cofx) (fn? (second cofx)))
161+
(and (vector? cofx))
146162
{:id (first cofx)
147163
:handler (get-handler kind (first cofx) true)
148-
:args-fn #(apply (second cofx) % (nnext cofx))}
164+
:args (next cofx)}
149165

150-
:else
151-
{:id (first cofx)
152-
:handler (get-handler kind (first cofx) true)
153-
:args-fn (constantly (next cofx))}))
166+
:else cofx))
154167

155168
(defn- normalize-acofxs
156169
[acofxs]
@@ -167,9 +180,9 @@
167180
(defn inject-acofx
168181
"Given async-coeffects (acofxs) returns an interceptor whose `:before` adds to the `:coeffects` (map) by calling a pre-registered 'async coeffect handler' identified by `id`.
169182
170-
Give as much acofxs as you want to compute async (pseudo parallel) values via `id` of the acofx or an vector of `id` and `args` or `id`, `args-fn` and `args`. `args` will be applied to acofx handler unless give `args-fn`, then `args` will be applied to `args-fn`. Result of `args-fn` will be applied to acofx-handler. Both acofx-handler and `args-fn` first argument will be coeffects map.
183+
Give as much acofxs as you want to compute async (pseudo parallel) values via `id` of the acofx or an vector of `id` and `args`. `args` can be mixed of any val and fns. fns will be applied with coeffects and event. Computed args prepended with coeffects will be applied to acofx handler.
171184
172-
Give a map instead of multiple acofxs like `{:acofxs ...}` to carry a `:error-dispatch` vector. `error-dispatch` will be called on error of any acofxs, event will be aborted. `:acofxs` can be given as vector or map. Use map keys to rename keys within event coeffects map.
185+
Give a map instead of multiple acofxs like `{:acofxs ...}` to carry a `:error-dispatch` vector, see also `set-global-error-dispatch`. `error-dispatch` will be called on error of any acofxs, event will be aborted. `:acofxs` can be given as vector or map. Use map keys to rename keys within event coeffects map.
173186
174187
The previous association of a `async coeffect handler` with an `id` will have happened via a call to `reg-acofx` - generally on program startup.
175188

0 commit comments

Comments
 (0)