-
-
Notifications
You must be signed in to change notification settings - Fork 18
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
134 additions
and
2 deletions.
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
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,59 @@ | ||
/// An asynchronous sequence that only emits the provided value once. | ||
/// | ||
/// ```swift | ||
/// Empty<Int>().sink( | ||
/// receiveValue: { print($0) }, | ||
/// receiveCompletion: { completion in | ||
/// switch completion { | ||
/// case .finished: | ||
/// print("Finished") | ||
/// case .failure: | ||
/// print("Failed") | ||
/// } | ||
/// } | ||
/// ) | ||
/// | ||
/// // Prints: | ||
/// // Finished | ||
/// ``` | ||
public struct Empty<Element>: AsyncSequence { | ||
|
||
// Private | ||
private let completeImmediately: Bool | ||
|
||
// MARK: Initialization | ||
|
||
/// Creates an empty async sequence. | ||
/// | ||
/// - Parameter completeImmediately: A Boolean value that indicates whether | ||
/// the async sequence should immediately finish. | ||
public init(completeImmediately: Bool = true) { | ||
self.completeImmediately = completeImmediately | ||
} | ||
|
||
// MARK: AsyncSequence | ||
|
||
/// Creates an async iterator that emits elements of this async sequence. | ||
/// - Returns: An instance that conforms to `AsyncIteratorProtocol`. | ||
public func makeAsyncIterator() -> Self { | ||
.init(completeImmediately: self.completeImmediately) | ||
} | ||
} | ||
|
||
// MARK: AsyncIteratorProtocol | ||
|
||
extension Empty: AsyncIteratorProtocol { | ||
|
||
/// Produces the next element in the sequence. | ||
/// | ||
/// Because this is an empty sequence, this will always be nil. | ||
/// | ||
/// - Returns: `nil` as this is an empty sequence. | ||
public mutating func next() async -> Element? { | ||
if !self.completeImmediately { | ||
try? await Task.sleep(seconds: 999_999_999) | ||
} | ||
|
||
return nil | ||
} | ||
} |
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,45 @@ | ||
import XCTest | ||
@testable import Asynchrone | ||
|
||
|
||
final class EmptyTests: XCTestCase { | ||
func testNothingEmitted() async { | ||
let values = await Empty<Int>().collect() | ||
XCTAssertEqual(values, []) | ||
} | ||
|
||
func testSequenceDoesntComplete() async { | ||
do { | ||
try await withThrowingTaskGroup(of: Void.self) { group in | ||
let timeout: TimeInterval = 1 | ||
let deadline = Date(timeIntervalSinceNow: timeout) | ||
|
||
group.addTask { | ||
_ = await Empty<Int>(completeImmediately: false).collect() | ||
throw TaskCompletedError() | ||
} | ||
|
||
group.addTask { | ||
if deadline.timeIntervalSinceNow > 0 { | ||
try await Task.sleep(seconds: timeout) | ||
} | ||
} | ||
|
||
try await group.next() | ||
group.cancelAll() | ||
} | ||
|
||
// All good! | ||
} catch _ as TaskCompletedError { | ||
XCTFail("Task incorrectly finished") | ||
} catch { | ||
XCTFail("Unknown error thrown \(error)") | ||
} | ||
} | ||
} | ||
|
||
|
||
|
||
// MARK: TaskCompletedError | ||
|
||
fileprivate struct TaskCompletedError: Error { } |
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