|
70 | 70 | (defonce ^:private !results
|
71 | 71 | (atom {}))
|
72 | 72 |
|
| 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 | + |
73 | 80 | (defn- fx-handler-run?
|
74 | 81 | [{:keys [stack]}]
|
75 | 82 | (->> stack
|
76 | 83 | (filter #(= :fx-handler (:id %)))
|
77 | 84 | (seq)))
|
78 | 85 |
|
| 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 | + |
79 | 92 | (defn- run-acofx!
|
80 |
| - [coeffects {:keys [id data-id handler args-fn]}] |
| 93 | + [coeffects {:keys [id data-id handler args]}] |
81 | 94 | (go
|
82 | 95 | (let [result
|
83 |
| - (->> coeffects |
84 |
| - (args-fn) |
| 96 | + (->> args |
| 97 | + (mapv (partial compute-argument coeffects)) |
85 | 98 | (apply handler coeffects)
|
86 | 99 | (<!)
|
87 | 100 | (#(get % id)))]
|
|
92 | 105 | [{:keys [acoeffects coeffects] :as context}
|
93 | 106 | {:keys [error-dispatch acofxs] inject-id :id}]
|
94 | 107 |
|
95 |
| - (let [dispatch-id |
| 108 | + (let [error-dispatch |
| 109 | + (or error-dispatch @!global-error-dispatch) |
| 110 | + |
| 111 | + dispatch-id |
96 | 112 | (:dispatch-id acoeffects)
|
97 | 113 |
|
98 | 114 | event
|
|
142 | 158 | :handler (get-handler kind cofx true)
|
143 | 159 | :args-fn (constantly nil)}
|
144 | 160 |
|
145 |
| - (and (vector? cofx) (fn? (second cofx))) |
| 161 | + (and (vector? cofx)) |
146 | 162 | {:id (first cofx)
|
147 | 163 | :handler (get-handler kind (first cofx) true)
|
148 |
| - :args-fn #(apply (second cofx) % (nnext cofx))} |
| 164 | + :args (next cofx)} |
149 | 165 |
|
150 |
| - :else |
151 |
| - {:id (first cofx) |
152 |
| - :handler (get-handler kind (first cofx) true) |
153 |
| - :args-fn (constantly (next cofx))})) |
| 166 | + :else cofx)) |
154 | 167 |
|
155 | 168 | (defn- normalize-acofxs
|
156 | 169 | [acofxs]
|
|
167 | 180 | (defn inject-acofx
|
168 | 181 | "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`.
|
169 | 182 |
|
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. |
171 | 184 |
|
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. |
173 | 186 |
|
174 | 187 | The previous association of a `async coeffect handler` with an `id` will have happened via a call to `reg-acofx` - generally on program startup.
|
175 | 188 |
|
|
0 commit comments