-
Notifications
You must be signed in to change notification settings - Fork 433
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add asyncStream
and asyncThrowingStream
for Signal
and SignalProducer
#847
Open
Marcocanc
wants to merge
14
commits into
master
Choose a base branch
from
swift-concurrency
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
aae72e2
Add asyncStream and asyncThrowingStream
Marcocanc afffc20
Add to project file
Marcocanc 9163df1
Update Changelog
Marcocanc 03832b3
Use actual value in tests
Marcocanc 73a5073
Use tabs instead of spaces to align with rest of codebase
Marcocanc 9d2393e
Add missing availability for mac catalyst
Marcocanc 5896c3d
add availability to test extension
Marcocanc 1c94d30
Use Xcode 13.1 for CI
Marcocanc 469b8c4
Replace Task with Dispatch async
Marcocanc be62397
Test New Xcode
Marcocanc d354eda
Merge branch 'master' into swift-concurrency
Marcocanc d2d6079
Fix availability and changelog
Marcocanc e08e237
Use Xcode 13.2.1
Marcocanc 3d40187
Merge branch 'master' into swift-concurrency
Marcocanc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,52 @@ | ||
// | ||
// Signal+SwiftConcurrency.swift | ||
// ReactiveSwift | ||
// | ||
// Created by Marco Cancellieri on 2021-11-11. | ||
// Copyright (c) 2021 GitHub. All rights reserved. | ||
// | ||
#if compiler(>=5.5.2) && canImport(_Concurrency) | ||
import Foundation | ||
|
||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, *) | ||
extension Signal { | ||
public var asyncThrowingStream: AsyncThrowingStream<Value, Swift.Error> { | ||
AsyncThrowingStream<Value, Swift.Error> { continuation in | ||
let disposable = observe { event in | ||
switch event { | ||
case .value(let value): | ||
continuation.yield(value) | ||
case .completed, .interrupted: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point |
||
continuation.finish() | ||
case .failed(let error): | ||
continuation.finish(throwing: error) | ||
} | ||
} | ||
continuation.onTermination = { @Sendable termination in | ||
disposable?.dispose() | ||
} | ||
} | ||
} | ||
} | ||
|
||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, *) | ||
extension Signal where Error == Never { | ||
public var asyncStream: AsyncStream<Value> { | ||
AsyncStream<Value> { continuation in | ||
let disposable = observe { event in | ||
switch event { | ||
case .value(let value): | ||
continuation.yield(value) | ||
case .completed, .interrupted: | ||
continuation.finish() | ||
case .failed: | ||
fatalError("Never is impossible to construct") | ||
} | ||
} | ||
continuation.onTermination = { @Sendable termination in | ||
disposable?.dispose() | ||
} | ||
} | ||
} | ||
} | ||
#endif |
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,52 @@ | ||
// | ||
// SignalProducer+SwiftConcurrency.swift | ||
// ReactiveSwift | ||
// | ||
// Created by Marco Cancellieri on 2021-11-11. | ||
// Copyright (c) 2021 GitHub. All rights reserved. | ||
// | ||
#if compiler(>=5.5.2) && canImport(_Concurrency) | ||
import Foundation | ||
|
||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, *) | ||
extension SignalProducer { | ||
public var asyncThrowingStream: AsyncThrowingStream<Value, Swift.Error> { | ||
AsyncThrowingStream<Value, Swift.Error> { continuation in | ||
let disposable = start { event in | ||
switch event { | ||
case .value(let value): | ||
continuation.yield(value) | ||
case .completed, .interrupted: | ||
continuation.finish() | ||
case .failed(let error): | ||
continuation.finish(throwing: error) | ||
} | ||
} | ||
continuation.onTermination = { @Sendable _ in | ||
disposable.dispose() | ||
} | ||
} | ||
} | ||
} | ||
|
||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, *) | ||
extension SignalProducer where Error == Never { | ||
public var asyncStream: AsyncStream<Value> { | ||
AsyncStream<Value> { continuation in | ||
let disposable = start { event in | ||
switch event { | ||
case .value(let value): | ||
continuation.yield(value) | ||
case .completed, .interrupted: | ||
continuation.finish() | ||
case .failed: | ||
fatalError("Never is impossible to construct") | ||
} | ||
} | ||
continuation.onTermination = { @Sendable _ in | ||
disposable.dispose() | ||
} | ||
} | ||
} | ||
} | ||
#endif |
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,129 @@ | ||
// | ||
// SwiftConcurrencyTests.swift | ||
// ReactiveSwift | ||
// | ||
// Created by Marco Cancellieri on 2021-11-11. | ||
// Copyright (c) 2021 GitHub. All rights reserved. | ||
// | ||
|
||
#if compiler(>=5.5.2) && canImport(_Concurrency) | ||
import Foundation | ||
import ReactiveSwift | ||
import XCTest | ||
|
||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, *) | ||
class SwiftConcurrencyTests: XCTestCase { | ||
func testValuesAsyncSignalProducer() async { | ||
let values = [1,2,3] | ||
var sum = 0 | ||
let asyncStream = SignalProducer(values).asyncStream | ||
for await number in asyncStream { | ||
sum += number | ||
} | ||
XCTAssertEqual(sum, 6) | ||
} | ||
|
||
func testValuesAsyncThrowingSignalProducer() async throws { | ||
let values = [1,2,3] | ||
var sum = 0 | ||
let asyncStream = SignalProducer(values).asyncThrowingStream | ||
for try await number in asyncStream { | ||
sum += number | ||
} | ||
XCTAssertEqual(sum, 6) | ||
} | ||
|
||
func testCompleteAsyncSignalProducer() async { | ||
let asyncStream = SignalProducer<String, Never>.empty.asyncStream | ||
let first = await asyncStream.first(where: { _ in true }) | ||
XCTAssertEqual(first, nil) | ||
} | ||
|
||
func testCompleteAsyncThrowingSignalProducer() async throws { | ||
let asyncStream = SignalProducer<String, Error>.empty.asyncThrowingStream | ||
let first = try await asyncStream.first(where: { _ in true }) | ||
XCTAssertEqual(first, nil) | ||
} | ||
|
||
func testErrorSignalProducer() async { | ||
let error = NSError(domain: "domain", code: 0, userInfo: nil) | ||
let asyncStream = SignalProducer<String, Error>(error: error).asyncThrowingStream | ||
await XCTAssertThrowsError(try await asyncStream.first(where: { _ in true })) | ||
} | ||
|
||
func testValuesAsyncSignal() async { | ||
let signal = Signal<Int, Never> { observer, _ in | ||
DispatchQueue.main.async { | ||
for number in [1, 2, 3] { | ||
observer.send(value: number) | ||
} | ||
observer.sendCompleted() | ||
} | ||
} | ||
var sum = 0 | ||
let asyncStream = signal.asyncStream | ||
for await number in asyncStream { | ||
sum += number | ||
} | ||
XCTAssertEqual(sum, 6) | ||
} | ||
|
||
func testValuesAsyncThrowingSignal() async throws { | ||
let signal = Signal<Int, Never> { observer, _ in | ||
DispatchQueue.main.async { | ||
for number in [1, 2, 3] { | ||
observer.send(value: number) | ||
} | ||
observer.sendCompleted() | ||
} | ||
} | ||
var sum = 0 | ||
let asyncStream = signal.asyncThrowingStream | ||
for try await number in asyncStream { | ||
sum += number | ||
} | ||
XCTAssertEqual(sum, 6) | ||
} | ||
|
||
func testCompleteAsyncSignal() async { | ||
let asyncStream = Signal<String, Never>.empty.asyncStream | ||
let first = await asyncStream.first(where: { _ in true }) | ||
XCTAssertEqual(first, nil) | ||
} | ||
|
||
func testCompleteAsyncThrowingSignal() async throws { | ||
let asyncStream = Signal<String, Error>.empty.asyncThrowingStream | ||
let first = try await asyncStream.first(where: { _ in true }) | ||
XCTAssertEqual(first, nil) | ||
} | ||
|
||
func testErrorSignal() async { | ||
let error = NSError(domain: "domain", code: 0, userInfo: nil) | ||
let signal = Signal<String, Error> { observer, _ in | ||
DispatchQueue.main.async { | ||
observer.send(error: error) | ||
} | ||
} | ||
let asyncStream = signal.asyncThrowingStream | ||
await XCTAssertThrowsError(try await asyncStream.first(where: { _ in true })) | ||
} | ||
} | ||
// Extension to allow Throw assertion for async expressions | ||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, *) | ||
fileprivate extension XCTest { | ||
func XCTAssertThrowsError<T: Sendable>( | ||
_ expression: @autoclosure () async throws -> T, | ||
_ message: @autoclosure () -> String = "", | ||
file: StaticString = #filePath, | ||
line: UInt = #line, | ||
_ errorHandler: (_ error: Error) -> Void = { _ in } | ||
) async { | ||
do { | ||
_ = try await expression() | ||
XCTFail(message(), file: file, line: line) | ||
} catch { | ||
errorHandler(error) | ||
} | ||
} | ||
} | ||
#endif |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should use
AsyncStream<Result<Value, Error>>
to maintain the error type.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bump