Skip to content

Commit f69cef2

Browse files
committed
Kotlin promises composition:
commit 605b36e Author: Tiago de Freitas Lima <[email protected]> Date: Mon May 23 10:28:00 2022 -0300 (wip)
1 parent 8578187 commit f69cef2

File tree

2 files changed

+76
-12
lines changed

2 files changed

+76
-12
lines changed

kotlin/src/main/kotlin/com/github/ljtfreitas/julian/k/Extensions.kt

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,22 @@ fun <T> Attempt<T>.result() : Result<T> = fold(Result.Companion::success, Result
2929

3030
fun <T> Promise<T>.result() : Result<T> = join().result()
3131

32-
operator fun Promise<out Any>.plus(other: Promise<out Any>): Promise<Array<Any?>> = bind { a ->
33-
other.bind { b ->
34-
Promise.done(when {
35-
a is Array<*> && b is Array<*> -> arrayOf(*a, *b)
36-
a is Array<*> -> arrayOf(*a, b)
37-
b is Array<*> -> arrayOf(a, *b)
38-
else -> arrayOf(a, b)
39-
})
40-
}
41-
}
32+
@JvmName("promisePlus")
33+
operator fun <A, B> Promise<A>.plus(other: Promise<B>): Promise<Pair<A, B>> = bind { a ->
34+
other.then { b -> a to b }
35+
}
36+
37+
@JvmName("promisePairPlus")
38+
operator fun <A, B, C> Promise<Pair<A, B>>.plus(other: Promise<C>): Promise<Triple<A, B, C>> = bind { (a, b) ->
39+
other.then { c -> Triple(a, b, c) }
40+
}
41+
42+
@JvmName("promiseTriplePlus")
43+
operator fun <A, B, C> Promise<Triple<A, B, C>>.plus(other: Promise<*>): Promise<Array<*>> = bind { (a, b, c) ->
44+
other.then { d -> arrayOf(a, b, c, d) }
45+
}
46+
47+
@JvmName("promiseArrayPlus")
48+
operator fun Promise<Array<*>>.plus(other: Promise<*>): Promise<Array<*>> = bind { a ->
49+
other.then { b -> arrayOf(a, b) }
50+
}

kotlin/src/test/kotlin/com/github/ljtfreitas/julian/k/ExtensionsTest.kt

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,80 @@ class ExtensionsTest : DescribeSpec({
3030

3131
describe("composed Promises") {
3232

33-
it("composed, dependent successful Promises") {
33+
it("a composition from two Promises should return a Pair") {
34+
val one = Promise.done("one")
35+
val two = Promise.done("two")
36+
37+
val composed = one + two
38+
39+
val pair: Pair<String, String> = composed.join().unsafe()
40+
41+
val (a, b) = pair
42+
43+
a shouldBe "one"
44+
b shouldBe "two"
45+
}
46+
47+
it("a composition from three Promises should return a Triple") {
3448
val one = Promise.done("one")
3549
val two = Promise.done("two")
3650
val three = Promise.done("three")
3751

3852
val composed = one + two + three
3953

40-
val (a, b, c) = composed.join().unsafe()
54+
val triple: Triple<String, String, String> = composed.join().unsafe()
55+
56+
val (a, b, c) = triple
57+
58+
a shouldBe "one"
59+
b shouldBe "two"
60+
c shouldBe "three"
61+
}
62+
63+
it("a composition from four Promises should return an Array") {
64+
val one = Promise.done("one")
65+
val two = Promise.done("two")
66+
val three = Promise.done("three")
67+
val four = Promise.done("four")
68+
69+
val composed = one + two + three + four
70+
71+
val array: Array<*> = composed.join().unsafe()
72+
73+
val (a, b, c, d) = array
74+
75+
a shouldBe "one"
76+
b shouldBe "two"
77+
c shouldBe "three"
78+
d shouldBe "four"
79+
}
80+
81+
it("a composition from five (or more) Promises should return an Array as well") {
82+
val one = Promise.done("one")
83+
val two = Promise.done("two")
84+
val three = Promise.done("three")
85+
val four = Promise.done("four")
86+
val five = Promise.done("five")
87+
88+
val composed = one + two + three + four + five
89+
90+
val array: Array<*> = composed.join().unsafe()
91+
92+
val (a, b, c, d, e) = array
4193

4294
a shouldBe "one"
4395
b shouldBe "two"
4496
c shouldBe "three"
97+
d shouldBe "four"
98+
e shouldBe "five"
4599
}
46100

47101
it("a failed Promise should break composition") {
48102
val failure = RuntimeException("i'm failed...")
49103

50104
val one = Promise.done("one")
51105
val two = Promise.done("two")
106+
52107
val three = Promise.failed<String, RuntimeException>(failure)
53108

54109
val composed = one + two + three

0 commit comments

Comments
 (0)