Skip to content

Commit eac3dc2

Browse files
committed
Implement CompletionStage .handle
1 parent 7a670ca commit eac3dc2

File tree

2 files changed

+70
-3
lines changed

2 files changed

+70
-3
lines changed

src/manifold/deferred.clj

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@
6666
accept-either accept-either-async
6767
run-after-either run-after-either-async
6868

69-
then-compose then-compose-async)
69+
then-compose then-compose-async
70+
71+
then-handle then-handle-async)
7072

7173
;; The potemkin abstract type for
7274
;; implementations such as CompletionStage
@@ -121,7 +123,6 @@
121123
(applyToEitherAsync [this other operator executor]
122124
(apply-to-either-async this other operator executor))
123125

124-
125126
(acceptEither [this other operator]
126127
(accept-either this other operator))
127128
(acceptEitherAsync [this other operator]
@@ -141,7 +142,14 @@
141142
(thenComposeAsync [this operator]
142143
(then-compose-async this operator))
143144
(thenComposeAsync [this operator executor]
144-
(then-compose-async this operator executor)))
145+
(then-compose-async this operator executor))
146+
147+
(handle [this operator]
148+
(then-handle this operator))
149+
(handleAsync [this operator]
150+
(then-handle-async this operator))
151+
(handleAsync [this operator executor]
152+
(then-handle-async this operator executor)))
145153

146154
(definline realized?
147155
"Returns true if the manifold deferred is realized."
@@ -1529,18 +1537,21 @@
15291537

15301538
(def ^:private then-apply-async (async-for then-apply))
15311539

1540+
15321541
(defn- then-accept [this ^Consumer operator]
15331542
(assert-some operator)
15341543
(chain this #(.accept operator %)))
15351544

15361545
(def ^:private then-accept-async (async-for then-accept))
15371546

1547+
15381548
(defn- then-run [this ^Runnable operator]
15391549
(assert-some operator)
15401550
(chain this (fn [_] (.run operator))))
15411551

15421552
(def ^:private then-run-async (async-for then-run))
15431553

1554+
15441555
(defn- then-combine [this other ^BiFunction operator]
15451556
(assert-some other operator)
15461557
(let-flow [mine this
@@ -1549,6 +1560,7 @@
15491560

15501561
(def ^:private then-combine-async (async-for-dual then-combine))
15511562

1563+
15521564
(defn- then-accept-both [this other ^BiConsumer operator]
15531565
(assert-some other operator)
15541566
(let-flow [mine this
@@ -1557,6 +1569,7 @@
15571569

15581570
(def ^:private then-accept-both-async (async-for-dual then-accept-both))
15591571

1572+
15601573
(defn- run-after-both [this other ^Runnable operator]
15611574
(assert-some other operator)
15621575
(let-flow [_ this
@@ -1566,31 +1579,44 @@
15661579

15671580
(def ^:private run-after-both-async (async-for-dual run-after-both))
15681581

1582+
15691583
(defn- apply-to-either [this other ^java.util.function.Function operator]
15701584
(assert-some other operator)
15711585
(then-apply (alt this other) operator))
15721586

15731587
(def ^:private apply-to-either-async (async-for-dual apply-to-either))
15741588

1589+
15751590
(defn- accept-either [this other ^java.util.function.Function operator]
15761591
(assert-some other operator)
15771592
(then-accept (alt this other) operator))
15781593

15791594
(def ^:private accept-either-async (async-for-dual accept-either))
15801595

1596+
15811597
(defn- run-after-either [this other ^java.util.function.Function operator]
15821598
(assert-some other operator)
15831599
(then-run (alt this other) operator))
15841600

15851601
(def ^:private run-after-either-async (async-for-dual run-after-either))
15861602

1603+
15871604
(defn- then-compose [this ^java.util.function.Function operator]
15881605
(assert-some operator)
15891606
(chain this (fn [value] (.apply operator value))))
15901607

15911608
(def ^:private then-compose-async (async-for then-compose))
15921609

15931610

1611+
(defn- then-handle [this ^java.util.function.BiFunction operator]
1612+
(assert-some operator)
1613+
(-> this
1614+
(chain #(.apply operator % nil))
1615+
(catch #(.apply operator nil %))))
1616+
1617+
(def ^:private then-handle-async (async-for then-handle))
1618+
1619+
15941620

15951621

15961622
;;;

test/manifold/deferred_stage_test.clj

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,3 +387,44 @@
387387
(fn [x] (CompletableFuture/completedFuture (inc x)))
388388
))]
389389
(is (= @d2 11)))))
390+
391+
(deftest test-handle
392+
(testing ".handle success"
393+
(let [d1 ^CompletionStage (d/success-deferred 1)
394+
d2 (.handle d1 (fn->BiFunction (fn [x _] (+ 1 x))))]
395+
396+
(is (= 1 @d1))
397+
(is (= 2 @d2))))
398+
399+
(testing ".handle error"
400+
(let [ex (RuntimeException.)
401+
d1 ^CompletionStage (d/error-deferred ex)
402+
d2 (.handle d1 (fn->BiFunction
403+
(fn [x error]
404+
(is (nil? x))
405+
(is (#{ex (.getCause ex)} error))
406+
2
407+
)))]
408+
409+
(is (thrown? RuntimeException @d1))
410+
(is (= 2 @d2))))
411+
412+
(testing ".handleAsync success"
413+
(let [d1 ^CompletionStage (d/success-deferred 1)
414+
d2 (.handleAsync d1 (fn->BiFunction (fn [x _] (+ 1 x))))]
415+
416+
(is (= 1 @d1))
417+
(is (= 2 @d2))))
418+
419+
(testing ".handleAsync error"
420+
(let [ex (RuntimeException.)
421+
d1 ^CompletionStage (d/error-deferred ex)
422+
d2 (.handleAsync d1 (fn->BiFunction
423+
(fn [x error]
424+
(is (nil? x))
425+
(is (#{ex (.getCause ex)} error))
426+
2
427+
)))]
428+
429+
(is (thrown? RuntimeException @d1))
430+
(is (= 2 @d2)))))

0 commit comments

Comments
 (0)