From e4c9d29b6931b301464603fabc741ef855184b65 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Sat, 25 Jan 2020 17:21:22 +0100 Subject: [PATCH] :tada: Add helper for blocking executuion on vertx. --- backend/vendor/vertx/src/vertx/core.clj | 34 ++++++++++++++++++++----- 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/backend/vendor/vertx/src/vertx/core.clj b/backend/vendor/vertx/src/vertx/core.clj index 925167fecf..92e6ecaf08 100644 --- a/backend/vendor/vertx/src/vertx/core.clj +++ b/backend/vendor/vertx/src/vertx/core.clj @@ -5,16 +5,17 @@ ;; Copyright (c) 2019 Andrey Antukh (ns vertx.core - (:require [clojure.spec.alpha :as s] - [promesa.core :as p] - [vertx.eventbus :as vxe] - [vertx.util :as vu]) + (:require + [clojure.spec.alpha :as s] + [promesa.core :as p] + [vertx.eventbus :as vxe] + [vertx.util :as vu]) (:import + io.vertx.core.AsyncResult io.vertx.core.Context io.vertx.core.DeploymentOptions - io.vertx.core.Future - io.vertx.core.Promise io.vertx.core.Handler + io.vertx.core.Promise io.vertx.core.Verticle io.vertx.core.Vertx io.vertx.core.VertxOptions @@ -52,6 +53,27 @@ [] (Vertx/currentContext)) +(defn wrap-blocking + ([f] (wrap-blocking (current-context) f)) + ([ctx f] + (let [^Vertx vsm (vu/resolve-system ctx)] + (fn [& args] + (let [d (p/deferred)] + (.executeBlocking + vsm + (reify Handler + (handle [_ prm] + (try + (.resolve ^Promise prm (apply f args)) + (catch Throwable e + (.fail ^Promise prm e))))) + false + (reify Handler + (handle [_ ar] + (if (.failed ^AsyncResult ar) + (p/reject! d (.cause ^AsyncResult ar)) + (p/resolve! d (.result ^AsyncResult ar))))))))))) + (defn handle-on-context "Attaches the context (current if not explicitly provided) to the promise execution chain."