-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
1874715
commit d19009f
Showing
2 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
Sources/NiceThings/Extensions/Concurrency/AsyncSequence+Collect.swift
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,13 @@ | ||
// | ||
// AsyncSequence+Collect.swift | ||
// | ||
// | ||
// Created by Jesse Tipton on 12/10/22. | ||
// | ||
|
||
@available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *) | ||
extension AsyncSequence { | ||
public func collect() async rethrows -> [Element] { | ||
try await reduce(into: [Element]()) { $0.append($1) } | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
Tests/NiceThingsTests/Extensions/Concurrency/AsyncSequenceCollectTests.swift
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,37 @@ | ||
// | ||
// AsyncSequenceCollectTests.swift | ||
// | ||
// | ||
// Created by Jesse Tipton on 12/10/22. | ||
// | ||
|
||
import XCTest | ||
import NiceThings | ||
|
||
final class AsyncSequenceCollectTests: XCTestCase { | ||
func testCollect() async { | ||
let stream = AsyncStream { continuation in | ||
continuation.yield(1) | ||
continuation.yield(2) | ||
continuation.yield(3) | ||
continuation.finish() | ||
} | ||
|
||
let output = await stream.collect() | ||
|
||
XCTAssertEqual(output, [1, 2, 3]) | ||
} | ||
|
||
func testCollectOnThrowingSequence() async throws { | ||
let throwingStream = AsyncThrowingStream { continuation in | ||
continuation.yield(1) | ||
continuation.yield(2) | ||
continuation.yield(3) | ||
continuation.finish() | ||
} | ||
|
||
let output = try await throwingStream.collect() | ||
|
||
XCTAssertEqual(output, [1, 2, 3]) | ||
} | ||
} |