Skip to content

Commit 3ef2ca5

Browse files
authored
Merge pull request #1302 from RomanPodymov/guarantees
When 2 guarantees
2 parents 4377261 + e68f15e commit 3ef2ca5

4 files changed

Lines changed: 56 additions & 4 deletions

File tree

.github/workflows/ci.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,12 @@ jobs:
8080
matrix:
8181
dst:
8282
- platform=macOS
83-
- platform=tvOS Simulator,OS=14.3,name=Apple TV
84-
- platform=iOS Simulator,OS=14.4,name=iPhone 12
83+
- platform=tvOS Simulator,OS=15.0,name=Apple TV
84+
- platform=iOS Simulator,OS=15.0,name=iPhone 12
8585
steps:
8686
- uses: maxim-lobanov/setup-xcode@v1
8787
with:
88-
xcode-version: 12.4
88+
xcode-version: 13.1
8989
- uses: actions/checkout@v2
9090
- uses: sersoft-gmbh/xcodebuild-action@v1
9191
with:

Sources/when.swift

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,40 @@ private func _when<U: Thenable>(_ thenables: [U]) -> Promise<Void> {
4343
return rp
4444
}
4545

46+
private func __when<T>(_ guarantees: [Guarantee<T>]) -> Guarantee<Void> {
47+
var countdown = guarantees.count
48+
guard countdown > 0 else {
49+
return .value(Void())
50+
}
51+
52+
let rg = Guarantee<Void>(.pending)
53+
54+
#if PMKDisableProgress || os(Linux)
55+
var progress: (completedUnitCount: Int, totalUnitCount: Int) = (0, 0)
56+
#else
57+
let progress = Progress(totalUnitCount: Int64(guarantees.count))
58+
progress.isCancellable = false
59+
progress.isPausable = false
60+
#endif
61+
62+
let barrier = DispatchQueue(label: "org.promisekit.barrier.when", attributes: .concurrent)
63+
64+
for guarantee in guarantees {
65+
guarantee.pipe { (_: T) in
66+
barrier.sync(flags: .barrier) {
67+
guard rg.isPending else { return }
68+
progress.completedUnitCount += 1
69+
countdown -= 1
70+
if countdown == 0 {
71+
rg.box.seal(())
72+
}
73+
}
74+
}
75+
}
76+
77+
return rg
78+
}
79+
4680
/**
4781
Wait for all promises in a set to fulfill.
4882

@@ -357,7 +391,12 @@ public func when(_ guarantees: Guarantee<Void>...) -> Guarantee<Void> {
357391
return when(guarantees: guarantees)
358392
}
359393

360-
// Waits on all provided Guarantees.
394+
/// Waits on all provided Guarantees.
361395
public func when(guarantees: [Guarantee<Void>]) -> Guarantee<Void> {
362396
return when(fulfilled: guarantees).recover{ _ in }.asVoid()
363397
}
398+
399+
/// Waits on all provided Guarantees.
400+
public func when<U, V>(guarantees gu: Guarantee<U>, _ gv: Guarantee<V>) -> Guarantee<(U, V)> {
401+
return __when([gu.asVoid(), gv.asVoid()]).map(on: nil) { (gu.value!, gv.value!) }
402+
}

Tests/CorePromise/WhenTests.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,4 +264,16 @@ class WhenTests: XCTestCase {
264264

265265
wait(for: [ex1, ex2], timeout: 10)
266266
}
267+
268+
func testDoubleTupleGuarantees() {
269+
let e1 = expectation(description: "")
270+
let g1 = Guarantee.value(1)
271+
let g2 = Guarantee.value("abc")
272+
when(guarantees: g1, g2).done { x, y in
273+
XCTAssertEqual(x, 1)
274+
XCTAssertEqual(y, "abc")
275+
e1.fulfill()
276+
}
277+
waitForExpectations(timeout: 1, handler: nil)
278+
}
267279
}

Tests/CorePromise/XCTestManifests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ extension WhenTests {
254254
static let __allTests__WhenTests = [
255255
("testAllSealedRejectedFirstOneRejects", testAllSealedRejectedFirstOneRejects),
256256
("testDoubleTuple", testDoubleTuple),
257+
("testDoubleTupleGuarantees", testDoubleTupleGuarantees),
257258
("testEmpty", testEmpty),
258259
("testGuaranteeWhen", testGuaranteeWhen),
259260
("testInt", testInt),

0 commit comments

Comments
 (0)