0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-10 08:50:57 -05:00

🎉 Add blocking helper to vertx.core.

This commit is contained in:
Andrey Antukh 2020-02-03 22:30:32 +01:00
parent 2cebbbc2f8
commit 2a5fa4df0e

View file

@ -49,37 +49,44 @@
[^Vertx o] [^Vertx o]
(.close o)) (.close o))
(def ^:dynamic *context* nil)
(defn get-or-create-context (defn get-or-create-context
[vsm] [vsm]
(.getOrCreateContext ^Vertx (vu/resolve-system vsm))) (or *context* (.getOrCreateContext ^Vertx (vu/resolve-system vsm))))
(defn current-context (defn current-context
[] []
(Vertx/currentContext)) (or *context* (Vertx/currentContext)))
(defmacro blocking (defmacro blocking
[& body] [& body]
`(let [vsm# (-> (current-context) (let [sym-vsm (with-meta (gensym "blocking")
(vu/resolve-system)) {:tag 'io.vertx.core.Vertx})
d# (p/deferred)] sym-e (with-meta (gensym "blocking")
(.executeBlocking {:tag 'java.lang.Throwable})
vsm# sym-prm (gensym "blocking")
(reify Handler sym-ar (gensym "blocking")]
(handle [_ prm#] `(let [~sym-vsm (-> (current-context)
(try (vu/resolve-system))
(.complete prm# (do ~@body)) d# (p/deferred)]
(catch Throwable e# (.executeBlocking
(.fail prm# e#))))) ~sym-vsm
true (reify Handler
(reify Handler (handle [_ ~sym-prm]
(handle [_ ar#] (let [prm# ~(with-meta sym-prm {:tag 'io.vertx.core.Promise})]
(if (.failed ^AsyncResult ar#) (try
(p/reject! d# (.cause ^AsyncResult ar#)) (.complete prm# (do ~@body))
(p/resolve! d# (.result ^AsyncResult ar#)))))) (catch Throwable ~sym-e
d#)) (.fail prm# ~sym-e))))))
true
(reify Handler
(handle [_ ~sym-ar]
(let [ar# ~(with-meta sym-ar {:tag 'io.vertx.core.AsyncResult})]
(if (.failed ar#)
(p/reject! d# (.cause ar#))
(p/resolve! d# (.result ar#)))))))
d#)))
(defn wrap-blocking (defn wrap-blocking
([f] (wrap-blocking (current-context) f)) ([f] (wrap-blocking (current-context) f))