-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manifold.clj
99 lines (84 loc) · 1.86 KB
/
manifold.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
(ns qbits.auspex.manifold
(:require [qbits.auspex.protocols :as p]
[manifold.deferred :as d])
(:import (java.util.concurrent CompletableFuture)))
(defn- error
[e]
(if (instance? Throwable e)
e
(ex-info (format "manifold.deferred error: %s"
(ex-message e))
{:qbits.auspex.manifold/error e})))
(extend-type manifold.deferred.IDeferred
p/Future
p/Success!
(-success! [d x]
(d/success! d x))
p/Error!
(-error! [d e]
(d/error! d (error e)))
p/Catch
(-catch
([d f]
(d/catch d
(fn [e]
(f (error e)))))
([d error-class f]
(d/catch d error-class f)))
p/Finally
(-finally
([d f]
(p/-finally d f nil))
([d f executor]
(d/finally d f)))
p/Handle
(-handle
([d f] (p/-handle d f nil))
([d f executor]
(d/on-realized d
#(f % nil)
#(f nil (error %)))))
p/Then
(-then
([d f]
(p/-then d f nil))
([d f executor]
(d/chain' d f)))
p/FMap
(-fmap
([d f]
(p/-fmap d f nil))
([d f executor]
(d/chain d f)))
p/WhenComplete
(-when-complete
([d f]
(p/-when-complete d f nil))
([d f executor]
(p/-handle d f nil)))
p/Realized?
(-realized? [d]
(d/realized? d))
p/Timeout!
(-timeout!
([d timeout-ms]
(d/timeout! d timeout-ms))
([d timeout-ms timeout-val]
(d/timeout! d timeout-ms timeout-val)))
p/Wrap
(-wrap [x]
(let [cf (CompletableFuture.)]
(d/on-realized x
#(.complete cf %)
#(.completeExceptionally cf (error %)))
cf))
p/Empty
(-empty [_] (d/deferred)))
(defn wrap
"Converts `CompletableFuture` to `manifod.deferred`"
[fut]
(let [d (d/deferred)]
(-> fut
(p/-then #(d/success! d %))
(p/-catch #(d/error! d %)))
d))