Skip to content

Commit

Permalink
Merge pull request #1302 from RomanPodymov/guarantees
Browse files Browse the repository at this point in the history
When 2 guarantees
  • Loading branch information
mxcl authored Jan 19, 2023
2 parents 4377261 + e68f15e commit 3ef2ca5
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 4 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ jobs:
matrix:
dst:
- platform=macOS
- platform=tvOS Simulator,OS=14.3,name=Apple TV
- platform=iOS Simulator,OS=14.4,name=iPhone 12
- platform=tvOS Simulator,OS=15.0,name=Apple TV
- platform=iOS Simulator,OS=15.0,name=iPhone 12
steps:
- uses: maxim-lobanov/setup-xcode@v1
with:
xcode-version: 12.4
xcode-version: 13.1
- uses: actions/checkout@v2
- uses: sersoft-gmbh/xcodebuild-action@v1
with:
Expand Down
41 changes: 40 additions & 1 deletion Sources/when.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,40 @@ private func _when<U: Thenable>(_ thenables: [U]) -> Promise<Void> {
return rp
}

private func __when<T>(_ guarantees: [Guarantee<T>]) -> Guarantee<Void> {
var countdown = guarantees.count
guard countdown > 0 else {
return .value(Void())
}

let rg = Guarantee<Void>(.pending)

#if PMKDisableProgress || os(Linux)
var progress: (completedUnitCount: Int, totalUnitCount: Int) = (0, 0)
#else
let progress = Progress(totalUnitCount: Int64(guarantees.count))
progress.isCancellable = false
progress.isPausable = false
#endif

let barrier = DispatchQueue(label: "org.promisekit.barrier.when", attributes: .concurrent)

for guarantee in guarantees {
guarantee.pipe { (_: T) in
barrier.sync(flags: .barrier) {
guard rg.isPending else { return }
progress.completedUnitCount += 1
countdown -= 1
if countdown == 0 {
rg.box.seal(())
}
}
}
}

return rg
}

/**
Wait for all promises in a set to fulfill.

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

// Waits on all provided Guarantees.
/// Waits on all provided Guarantees.
public func when(guarantees: [Guarantee<Void>]) -> Guarantee<Void> {
return when(fulfilled: guarantees).recover{ _ in }.asVoid()
}

/// Waits on all provided Guarantees.
public func when<U, V>(guarantees gu: Guarantee<U>, _ gv: Guarantee<V>) -> Guarantee<(U, V)> {
return __when([gu.asVoid(), gv.asVoid()]).map(on: nil) { (gu.value!, gv.value!) }
}
12 changes: 12 additions & 0 deletions Tests/CorePromise/WhenTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -264,4 +264,16 @@ class WhenTests: XCTestCase {

wait(for: [ex1, ex2], timeout: 10)
}

func testDoubleTupleGuarantees() {
let e1 = expectation(description: "")
let g1 = Guarantee.value(1)
let g2 = Guarantee.value("abc")
when(guarantees: g1, g2).done { x, y in
XCTAssertEqual(x, 1)
XCTAssertEqual(y, "abc")
e1.fulfill()
}
waitForExpectations(timeout: 1, handler: nil)
}
}
1 change: 1 addition & 0 deletions Tests/CorePromise/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ extension WhenTests {
static let __allTests__WhenTests = [
("testAllSealedRejectedFirstOneRejects", testAllSealedRejectedFirstOneRejects),
("testDoubleTuple", testDoubleTuple),
("testDoubleTupleGuarantees", testDoubleTupleGuarantees),
("testEmpty", testEmpty),
("testGuaranteeWhen", testGuaranteeWhen),
("testInt", testInt),
Expand Down

0 comments on commit 3ef2ca5

Please sign in to comment.