|
| 1 | +;; Copyright © 2022 Manetu, Inc. All rights reserved |
| 2 | + |
| 3 | +(ns ^:no-doc temporal.internal.promise |
| 4 | + (:require [promesa.protocols :as pt] |
| 5 | + [temporal.internal.utils :refer [->Func] :as u]) |
| 6 | + (:import [clojure.lang IDeref IBlockingDeref] |
| 7 | + [io.temporal.workflow Promise] |
| 8 | + [java.util.concurrent CompletableFuture])) |
| 9 | + |
| 10 | +(deftype PromiseAdapter [^Promise p] |
| 11 | + IDeref |
| 12 | + (deref [_] (.get p)) |
| 13 | + IBlockingDeref |
| 14 | + (deref [_ ms val] (.get p ms val))) |
| 15 | + |
| 16 | +(defmulti ->temporal type) |
| 17 | + |
| 18 | +(defmethod ->temporal Promise |
| 19 | + [x] x) |
| 20 | + |
| 21 | +(defmethod ->temporal CompletableFuture |
| 22 | + [^CompletableFuture x] |
| 23 | + (reify Promise |
| 24 | + (get [_] (.get x)) |
| 25 | + (isCompleted [_] (.isDone x)) |
| 26 | + (handle [_ f] |
| 27 | + (try |
| 28 | + (let [r (.get x)] |
| 29 | + (.apply f r nil)) |
| 30 | + (catch Exception e |
| 31 | + (.apply f nil e)))))) |
| 32 | + |
| 33 | +(defmethod ->temporal :default |
| 34 | + [x] |
| 35 | + (reify Promise |
| 36 | + (get [_] x) |
| 37 | + (handle [_ f] |
| 38 | + (.apply f x nil)) |
| 39 | + (isCompleted [_] true))) |
| 40 | + |
| 41 | +(def fw-identity (->Func identity)) |
| 42 | + |
| 43 | +(extend-protocol pt/IPromise |
| 44 | + PromiseAdapter |
| 45 | + (-map |
| 46 | + ([it f] |
| 47 | + (.thenApply ^Promise (.p it) (->Func f))) |
| 48 | + |
| 49 | + ([it f executor])) |
| 50 | + |
| 51 | + (-bind |
| 52 | + ([it f] |
| 53 | + (.thenCompose ^Promise (.p it) (->Func f))) |
| 54 | + |
| 55 | + ([it f executor])) |
| 56 | + |
| 57 | + (-then |
| 58 | + ([it f] |
| 59 | + (pt/-promise (.thenCompose ^Promise (.p it) (->Func (comp ->temporal f))))) |
| 60 | + |
| 61 | + ([it f executor])) |
| 62 | + |
| 63 | + (-mapErr |
| 64 | + ([it f] |
| 65 | + (letfn [(handler [e] |
| 66 | + (if (instance? Promise e) |
| 67 | + (f (.getCause ^Exception e)) |
| 68 | + (f e)))] |
| 69 | + (.exceptionally ^Promise (.p it) (->Func handler)))) |
| 70 | + |
| 71 | + ([it f executor])) |
| 72 | + |
| 73 | + (-thenErr |
| 74 | + ([it f] |
| 75 | + (letfn [(handler [v e] |
| 76 | + (if e |
| 77 | + (if (instance? Promise e) |
| 78 | + (pt/-promise (f (.getFailure e))) |
| 79 | + (pt/-promise (f e))) |
| 80 | + it))] |
| 81 | + (as-> ^Promise (.p it) $$ |
| 82 | + (.handle $$ (->Func handler)) |
| 83 | + (.thenCompose $$ fw-identity)))) |
| 84 | + |
| 85 | + ([it f executor])) |
| 86 | + |
| 87 | + (-handle |
| 88 | + ([it f] |
| 89 | + (as-> ^Promise (.p it) $$ |
| 90 | + (.handle $$ (->Func (comp ->temporal f))) |
| 91 | + (.thenCompose $$ fw-identity))) |
| 92 | + |
| 93 | + ([it f executor])) |
| 94 | + |
| 95 | + (-finally |
| 96 | + ([it f]) |
| 97 | + |
| 98 | + ([it f executor]))) |
| 99 | + |
| 100 | +(extend-protocol pt/IPromiseFactory |
| 101 | + Promise |
| 102 | + (-promise [p] (->PromiseAdapter p)) |
| 103 | + |
| 104 | + PromiseAdapter |
| 105 | + (-promise [p] p)) |
0 commit comments