-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
impl.clj
127 lines (111 loc) · 3.01 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
(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
p/Success!
(-success! [cf x]
(.complete cf x))
p/Error!
(-error! [cf e]
(.completeExceptionally cf e))
p/Complete!
(-complete! [cf f executor]
(.completeAsync cf
(f/supplier f)
^Executor executor))
p/Error?
(-error? [cf]
(.isCompletedExceptionally cf))
p/Catch
(-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))))))))
p/Finally
(-finally
([cf f]
(p/-when-complete cf
(fn [_ _] (f))))
([cf f executor]
(p/-when-complete cf
(fn [_ _] (f))
^Executor executor)))
p/Handle
(-handle
([cs f]
(.handle cs (f/bifunction f)))
([cs f executor]
(.handleAsync cs
(f/bifunction f)
^Executor executor)))
p/Then
(-then
([cs f]
(.thenApply cs (f/function f)))
([cs f executor]
(.thenApplyAsync cs
(f/function f)
^Executor executor)))
p/FMap
(-fmap
([cs f]
(.thenCompose cs (f/function f)))
([cs f executor]
(.thenComposeAsync cs
(f/function f)
^Executor executor)))
p/WhenComplete
(-when-complete
([cf f]
(.whenComplete cf (f/biconsumer f)))
([cf f executor]
(.whenCompleteAsync cf
(f/biconsumer f)
^Executor executor)))
p/Realized?
(-realized? [cf]
(.isDone cf))
p/Cancel!
(-cancel! [cf]
(.cancel cf true)
cf)
p/Canceled?
(-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)))
p/Wrap
(-wrap [cf] cf)
p/Empty
(-empty [_] (CompletableFuture.)))
(extend-protocol p/Wrap
Object
(-wrap [x] (CompletableFuture/completedFuture x)))