-
Notifications
You must be signed in to change notification settings - Fork 7
/
promise.clj
47 lines (37 loc) · 1.69 KB
/
promise.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
;; Copyright © 2022 Manetu, Inc. All rights reserved
(ns temporal.promise
"Methods for managing promises from pending activities from within workflows"
(:require [promesa.core :as p]
[temporal.internal.promise :as pt])
(:import [temporal.internal.promise PromiseAdapter]
[io.temporal.workflow Promise]))
(defn- ->array
^"[Lio.temporal.workflow.Promise;" [coll]
{:pre [(every? (partial instance? PromiseAdapter) coll)]}
(into-array Promise (map #(.p %) coll)))
(defn all
"Returns Promise that becomes completed when all arguments are completed.
*N.B. A single promise failure causes resulting promise to deliver the failure immediately.*
Similar to [promesa/all](https://funcool.github.io/promesa/latest/promesa.core.html#var-all) but designed to work with
promises returned from [[temporal.activity/invoke]] from within workflow context.
```clojure
(-> (all [(a/invoke activity-a ..) (a/invoke activity-b ..)])
(promesa.core/then (fn [[a-result b-result]] ...)))
```
"
[coll]
(-> (pt/->PromiseAdapter (Promise/allOf (->array coll)))
(p/then (fn [_]
(mapv deref coll)))))
(defn race
"Returns Promise that becomes completed when any of the arguments are completed.
*N.B. A single promise failure causes resulting promise to deliver the failure immediately.*
Similar to [promesa/race](https://funcool.github.io/promesa/latest/promesa.core.html#var-race) but designed to work with
promises returned from [[temporal.activity/invoke]] from within workflow context.
```clojure
(-> (race [(a/invoke activity-a ..) (a/invoke activity-b ..)])
(promesa.core/then (fn [fastest-result] ...)))
```
"
[coll]
(pt/->PromiseAdapter (Promise/anyOf (->array coll))))