-
Notifications
You must be signed in to change notification settings - Fork 3
/
macros.clj
26 lines (24 loc) · 1.09 KB
/
macros.clj
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
(ns shadow.macros
(:require [clojure.string :as str]))
(defmacro ^{:js-require 'shadow.xhr} wait
[wait-let & body]
;; if more than 1 result pair, combine and wait for all, otherwise just wait for the one
(if (= 2 (count wait-let))
(let [[value result] wait-let]
`(shadow.xhr/result-wait-on-success ~result (fn [~value] ~@body)))
(let [let-bindings (->> wait-let
(partition 2)
(map (fn [[name value]]
(let [alias (gensym)]
{:name name
:value value
:alias alias
:get-value `(.getValue ~alias)
}))))]
`(let [~@(mapcat (juxt :alias :value) let-bindings)
combo# (shadow.xhr/result-combine ~@(map :alias let-bindings))]
(shadow.xhr/result-wait-on-success
combo#
(fn [dummy#]
(let [~@(mapcat (juxt :name :get-value) let-bindings)]
~@body)))))))