Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
toBlocking equivalent for Combine's Publishers
extension Publisher {
func waitForCompletion(timeout: TimeInterval = 1.0, file: StaticString = #file, line: UInt = #line) throws -> [Output] {
let expectation = XCTestExpectation(description: "wait for completion")
var completion: Subscribers.Completion<Failure>?
var output = [Output]()
let subscription = self.collect()
.sink(receiveCompletion: { receiveCompletion in
completion = receiveCompletion
expectation.fulfill()
}, receiveValue: { value in
output = value
})
XCTWaiter().wait(for: [expectation], timeout: timeout)
subscription.cancel()
switch try XCTUnwrap(completion, "Publisher never completed", file: file, line: line) {
case let .failure(error):
throw error
case .finished:
return output
}
}
func waitForFirstOutput(timeout: TimeInterval = 1.0, file: StaticString = #file, line: UInt = #line) throws -> Output {
return try XCTUnwrap(prefix(1).waitForCompletion(file: file, line: line).first, "", file: file, line: line)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.