-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
73 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import Combine | ||
import Foundation | ||
|
||
extension Publisher { | ||
/// Subscribes to the receiving publichser and expects a single value and a subsequent successfull completion. | ||
/// | ||
/// If no values is received, or more than one value is received, or a failure is received, the program will crash. | ||
/// - warning: The publisher must receive the value and completion event in a different queue from the queue where this property is called or the code will never execute. | ||
@inlinable public var await: Output { | ||
let group = DispatchGroup() | ||
group.enter() | ||
|
||
var value: Output? = nil | ||
let cancellable = self.sink(fixedDemand: 1, receiveCompletion: { | ||
switch $0 { | ||
case .failure(let error): fatalError("\(error)") | ||
case .finished: | ||
guard case .some = value else { fatalError() } | ||
group.leave() | ||
} | ||
}, receiveValue: { value = $0 }) | ||
|
||
group.wait() | ||
cancellable.cancel() | ||
return value! | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import XCTest | ||
import Conbini | ||
import Combine | ||
|
||
/// Tests the correct behavior of the `await` operator. | ||
final class AwaitOpTests: XCTestCase { | ||
override func setUp() { | ||
self.continueAfterFailure = false | ||
} | ||
} | ||
|
||
extension AwaitOpTests { | ||
/// Tests the `await` operator. | ||
func testAwait() { | ||
let publisher = Just("Hello") | ||
.delay(for: 1, scheduler: DispatchQueue.global()) | ||
|
||
let queue = DispatchQueue(label: "io.dehesa.conbini.tests.await") | ||
let cancellable = Just(()) | ||
.delay(for: 10, scheduler: queue) | ||
.sink { XCTFail("The await test failed") } | ||
|
||
let greeting = publisher.await | ||
XCTAssertEqual(greeting, "Hello") | ||
|
||
cancellable.cancel() | ||
} | ||
} |