|
158 | 158 |
|
159 | 159 | (def *loaded* (atom #{}))
|
160 | 160 |
|
| 161 | +(defn- run-async! |
| 162 | + "Like cljs.core/run!, but for an async procedure, and with the |
| 163 | + ability to break prior to processing the entire collection. |
| 164 | +
|
| 165 | + Chains successive calls to the supplied procedure for items in |
| 166 | + the collection. The procedure should accept an item from the |
| 167 | + collection and a callback of one argument. If the break? predicate, |
| 168 | + when applied to the procedure callback value, yields a truthy |
| 169 | + result, terminates early calling the supplied cb with the callback |
| 170 | + value. Otherwise, when complete, calls cb with nil." |
| 171 | + [proc coll break? cb] |
| 172 | + (if (seq coll) |
| 173 | + (proc (first coll) |
| 174 | + (fn [res] |
| 175 | + (if (break? res) |
| 176 | + (cb res) |
| 177 | + (run-async! proc (rest coll) break? cb)))) |
| 178 | + (cb nil))) |
| 179 | + |
| 180 | +(declare require) |
| 181 | + |
| 182 | +(defn- process-deps |
| 183 | + [bound-vars names opts cb] |
| 184 | + (run-async! (fn [name cb] |
| 185 | + (require bound-vars name nil opts cb)) |
| 186 | + names |
| 187 | + :error |
| 188 | + cb)) |
| 189 | + |
| 190 | +(defn- process-macros-deps |
| 191 | + [bound-vars cache opts cb] |
| 192 | + (process-deps bound-vars |
| 193 | + (distinct (vals (:require-macros cache))) |
| 194 | + (assoc opts :macros-ns true) |
| 195 | + cb)) |
| 196 | + |
| 197 | +(defn- process-libs-deps |
| 198 | + [bound-vars cache opts cb] |
| 199 | + (process-deps bound-vars |
| 200 | + (distinct (concat (vals (:requires cache)) (vals (:imports cache)))) |
| 201 | + (dissoc opts :macros-ns) |
| 202 | + cb)) |
| 203 | + |
161 | 204 | (defn require
|
162 | 205 | ([name cb]
|
163 | 206 | (require name nil cb))
|
|
201 | 244 | (do
|
202 | 245 | (swap! *loaded* conj name)
|
203 | 246 | (cb {:value true})))))
|
204 |
| - :js (let [res (try |
205 |
| - ((:*eval-fn* bound-vars) resource) |
206 |
| - (when cache |
207 |
| - (load-analysis-cache! |
208 |
| - (:*compiler* bound-vars) name cache)) |
209 |
| - (when source-map |
210 |
| - (load-source-map! |
211 |
| - (:*compiler* bound-vars) name source-map)) |
212 |
| - (catch :default cause |
213 |
| - (wrap-error |
214 |
| - (ana/error env |
215 |
| - (str "Could not require " name) cause))))] |
216 |
| - (if (:error res) |
217 |
| - (cb res) |
218 |
| - (do |
219 |
| - (swap! *loaded* conj name) |
220 |
| - (cb {:value true})))) |
| 247 | + :js (process-macros-deps bound-vars cache opts |
| 248 | + (fn [res] |
| 249 | + (if (:error res) |
| 250 | + (cb res) |
| 251 | + (process-libs-deps bound-vars cache opts |
| 252 | + (fn [res] |
| 253 | + (if (:error res) |
| 254 | + (cb res) |
| 255 | + (let [res (try |
| 256 | + ((:*eval-fn* bound-vars) resource) |
| 257 | + (when cache |
| 258 | + (load-analysis-cache! |
| 259 | + (:*compiler* bound-vars) name cache)) |
| 260 | + (when source-map |
| 261 | + (load-source-map! |
| 262 | + (:*compiler* bound-vars) name source-map)) |
| 263 | + (catch :default cause |
| 264 | + (wrap-error |
| 265 | + (ana/error env |
| 266 | + (str "Could not require " name) cause))))] |
| 267 | + (if (:error res) |
| 268 | + (cb res) |
| 269 | + (do |
| 270 | + (swap! *loaded* conj name) |
| 271 | + (cb {:value true})))))))))) |
221 | 272 | (cb (wrap-error
|
222 | 273 | (ana/error env
|
223 | 274 | (str "Invalid :lang specified " lang ", only :clj or :js allowed"))))))
|
|
0 commit comments