/
impl.clj
107 lines (92 loc) · 2.71 KB
/
impl.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
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
(ns qbits.auspex.impl
(:require [qbits.auspex.protocols :as p]
[qbits.auspex.function :as f])
(:import (java.util.concurrent CompletableFuture
Executor
CompletionException
ExecutionException)))
(set! *warn-on-reflection* true)
(defn ex-unwrap
"Unwraps exceptions if we have a valid ex-cause present"
[ex]
(if (or (instance? ExecutionException ex)
(instance? CompletionException ex))
(or (ex-cause ex) ex)
ex))
(extend-type CompletableFuture
p/Future
(-success! [cf x]
(.complete cf x))
(-error! [cf e]
(.completeExceptionally cf e))
(-complete! [cf f executor]
(.completeAsync cf
(f/supplier f)
^Executor executor))
(-error? [cf]
(.isCompletedExceptionally cf))
(-catch
([cf f]
(.exceptionally cf (f/function #(f (ex-unwrap %)))))
([cf error-class f]
(.exceptionally cf
(f/function (fn [^Throwable t]
(let [ex (ex-unwrap t)]
(if (instance? error-class ex)
(f ex)
(throw ex))))))))
(-finally
([cf f]
(p/-when-complete cf
(fn [_ _] (f))))
([cf f executor]
(p/-when-complete cf
(fn [_ _] (f))
^Executor executor)))
(-handle
([cs f]
(.handle cs (f/bifunction f)))
([cs f executor]
(.handleAsync cs
(f/bifunction f)
^Executor executor)))
(-then
([cs f]
(.thenApply cs (f/function f)))
([cs f executor]
(.thenApplyAsync cs
(f/function f)
^Executor executor)))
(-fmap
([cs f]
(.thenCompose cs (f/function f)))
([cs f executor]
(.thenComposeAsync cs
(f/function f)
^Executor executor)))
(-when-complete
([cf f]
(.whenComplete cf (f/biconsumer f)))
([cf f executor]
(.whenCompleteAsync cf
(f/biconsumer f)
^Executor executor)))
(-realized? [cf]
(.isDone cf))
p/Cancel
(-cancel! [cf]
(.cancel cf true)
cf)
(-canceled? [cf]
(.isCancelled cf))
p/Timeout
(-timeout!
([cf timeout-ms]
(.orTimeout cf
timeout-ms
java.util.concurrent.TimeUnit/MILLISECONDS))
([cf timeout-ms timeout-val]
(.completeOnTimeout cf
timeout-val
timeout-ms
java.util.concurrent.TimeUnit/MILLISECONDS))))