This repository has been archived by the owner on Oct 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add AsyncStream convenience initializer
- Loading branch information
Showing
3 changed files
with
94 additions
and
0 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,39 @@ | ||
// | ||
// AsyncStream+Extensions.swift | ||
// Aware | ||
// | ||
// Created by Joshua Peek on 3/24/24. | ||
// | ||
|
||
extension AsyncStream { | ||
/// Annoying wrapper to get | ||
/// typealias Yield = @discardableResult (Element) -> Continuation.YieldResult | ||
struct Yield: Sendable { | ||
private let continuation: Continuation | ||
|
||
fileprivate init(continuation: Continuation) { | ||
self.continuation = continuation | ||
} | ||
|
||
@discardableResult | ||
func callAsFunction(_ value: Element) -> Continuation.YieldResult { | ||
continuation.yield(value) | ||
} | ||
} | ||
|
||
init( | ||
_ elementType: Element.Type = Element.self, | ||
bufferingPolicy limit: Continuation.BufferingPolicy = .unbounded, | ||
_ build: @Sendable @escaping (Yield) async -> Void | ||
) { | ||
self.init(elementType, bufferingPolicy: limit) { continuation in | ||
let task = Task { | ||
await build(Yield(continuation: continuation)) | ||
continuation.finish() | ||
} | ||
continuation.onTermination = { _ in | ||
task.cancel() | ||
} | ||
} | ||
} | ||
} |
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,47 @@ | ||
import XCTest | ||
|
||
@testable import Aware | ||
|
||
final class AsyncStreamTests: XCTestCase { | ||
func testSimpleAsyncStream() async throws { | ||
@Sendable func answer() async -> Int { | ||
42 | ||
} | ||
|
||
let stream = AsyncStream { [answer] yield in | ||
let n = await answer() | ||
yield(n) | ||
yield(n + 1) | ||
yield(n + 2) | ||
} | ||
|
||
var numbers: [Int] = [] | ||
for await n in stream { | ||
numbers.append(n) | ||
} | ||
|
||
XCTAssertEqual(numbers, [42, 43, 44]) | ||
} | ||
|
||
func testMapAsyncStream() async throws { | ||
let stream1 = AsyncStream { continuation in | ||
continuation.yield(1) | ||
continuation.yield(2) | ||
continuation.yield(3) | ||
continuation.finish() | ||
} | ||
|
||
let stream2 = AsyncStream { yield in | ||
for await n in stream1 { | ||
yield(n * 2) | ||
} | ||
} | ||
|
||
var numbers: [Int] = [] | ||
for await n in stream2 { | ||
numbers.append(n) | ||
} | ||
|
||
XCTAssertEqual(numbers, [2, 4, 6]) | ||
} | ||
} |