-
Notifications
You must be signed in to change notification settings - Fork 25
/
utils.cljc
54 lines (48 loc) · 1.79 KB
/
utils.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
(ns konserve.utils
(:require [clojure.walk]))
(defn invert-map [m]
(->> (map (fn [[k v]] [v k]) m)
(into {})))
(defn now []
#?(:clj (java.util.Date.)
:cljs (js/Date.)))
(defn meta-update
"Metadata has following 'edn' format
{:key 'The stored key'
:type 'The type of the stored value binary or edn'
:last-write Date timestamp in milliseconds.}
Returns the meta value of the stored key-value tupel. Returns metadata if the key
value not exist, if it does it will update the last-write to date now. "
[key type old]
(if (empty? old)
{:key key :type type :last-write (now)}
(clojure.core/assoc old :last-write (now))))
#?(:clj
(defmacro async+sync
[sync? async->sync async-code]
(let [async->sync (if (symbol? async->sync)
(or (resolve async->sync)
(when-let [_ns (or (get-in &env [:ns :use-macros async->sync])
(get-in &env [:ns :uses async->sync]))]
(resolve (symbol (str _ns) (str async->sync)))))
async->sync)]
(assert (some? async->sync))
`(if ~sync?
~(clojure.walk/postwalk (fn [n]
(if-not (meta n)
(async->sync n n) ;; primitives have no metadata
(with-meta (async->sync n n)
(update (meta n) :tag (fn [t] (async->sync t t))))))
async-code)
~async-code))))
(def ^:dynamic *default-sync-translation*
'{go-try try
<? do
go-try- try
<!- do
<?- do
go-locked locked})
(defmacro with-promise [sym & body]
`(let [~sym (cljs.core.async/promise-chan)]
~@body
~sym))