Skip to content

Commit 52c52be

Browse files
author
Thibault Wittemberg
committed
operators: add fromAsync, fromThrowingAsync, fromAsyncSequence to bridge async/await with Combine
1 parent fc3e405 commit 52c52be

File tree

6 files changed

+695
-20
lines changed

6 files changed

+695
-20
lines changed

.github/workflows/tests.yml

+8-19
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,30 @@ on: [push, pull_request, workflow_dispatch]
55
jobs:
66
xcode-tests:
77
name: "Test"
8-
runs-on: macOS-latest
8+
runs-on: macOS-11
99

1010
strategy:
1111
matrix:
1212
platform: [macOS, iOS, tvOS]
1313
include:
1414
- platform: macOS
15-
sdk: macosx
15+
sdk: macosx11.3
1616
destination: "arch=x86_64"
1717

1818
- platform: iOS
19-
sdk: iphonesimulator
20-
destination: "name=iPhone 11"
19+
sdk: iphoneos15.0
20+
destination: "name=iPhone 13"
2121

2222
- platform: tvOS
23-
sdk: appletvsimulator
23+
sdk: appletvsimulator15.0
2424
destination: "name=Apple TV"
2525

2626
steps:
2727
- uses: actions/checkout@v2
28-
- name: Select Xcode 12 (beta)
29-
run: sudo xcode-select -s /Applications/Xcode_12_beta.app
30-
- name: Generate project
31-
run: make project
28+
- name: Select Xcode 13
29+
run: sudo xcode-select -s /Applications/Xcode_13.0.app
3230
- name: Run tests
33-
run: set -o pipefail && xcodebuild -project CombineExt.xcodeproj -scheme CombineExt-Package -enableCodeCoverage YES -sdk ${{ matrix.sdk }} -destination "${{ matrix.destination }}" test | xcpretty -c -r html --output logs/${{ matrix.platform }}.html
31+
run: set -o pipefail && xcodebuild -scheme CombineExt-Package -enableCodeCoverage YES -sdk ${{ matrix.sdk }} -destination "${{ matrix.destination }}" test | xcpretty -c -r html --output logs/${{ matrix.platform }}.html
3432
- uses: codecov/[email protected]
3533
with:
3634
token: 1519d58c-6fb9-483f-af6c-7f6f0b384345
@@ -39,12 +37,3 @@ jobs:
3937
with:
4038
name: build-logs-${{ github.run_id }}
4139
path: logs
42-
43-
SPM:
44-
name: "Test (SPM)"
45-
runs-on: macOS-latest
46-
47-
steps:
48-
- uses: actions/checkout@v2
49-
- name: Run tests
50-
run: set -o pipefail && swift test
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Scheme
3+
LastUpgradeVersion = "1300"
4+
version = "1.3">
5+
<BuildAction
6+
parallelizeBuildables = "YES"
7+
buildImplicitDependencies = "YES">
8+
<BuildActionEntries>
9+
<BuildActionEntry
10+
buildForTesting = "YES"
11+
buildForRunning = "YES"
12+
buildForProfiling = "YES"
13+
buildForArchiving = "YES"
14+
buildForAnalyzing = "YES">
15+
<BuildableReference
16+
BuildableIdentifier = "primary"
17+
BlueprintIdentifier = "CombineExt"
18+
BuildableName = "CombineExt"
19+
BlueprintName = "CombineExt"
20+
ReferencedContainer = "container:">
21+
</BuildableReference>
22+
</BuildActionEntry>
23+
<BuildActionEntry
24+
buildForTesting = "YES"
25+
buildForRunning = "YES"
26+
buildForProfiling = "NO"
27+
buildForArchiving = "NO"
28+
buildForAnalyzing = "YES">
29+
<BuildableReference
30+
BuildableIdentifier = "primary"
31+
BlueprintIdentifier = "CombineExtTests"
32+
BuildableName = "CombineExtTests"
33+
BlueprintName = "CombineExtTests"
34+
ReferencedContainer = "container:">
35+
</BuildableReference>
36+
</BuildActionEntry>
37+
</BuildActionEntries>
38+
</BuildAction>
39+
<TestAction
40+
buildConfiguration = "Debug"
41+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
42+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
43+
shouldUseLaunchSchemeArgsEnv = "YES">
44+
<Testables>
45+
<TestableReference
46+
skipped = "NO">
47+
<BuildableReference
48+
BuildableIdentifier = "primary"
49+
BlueprintIdentifier = "CombineExtTests"
50+
BuildableName = "CombineExtTests"
51+
BlueprintName = "CombineExtTests"
52+
ReferencedContainer = "container:">
53+
</BuildableReference>
54+
</TestableReference>
55+
</Testables>
56+
</TestAction>
57+
<LaunchAction
58+
buildConfiguration = "Debug"
59+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
60+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
61+
launchStyle = "0"
62+
useCustomWorkingDirectory = "NO"
63+
ignoresPersistentStateOnLaunch = "NO"
64+
debugDocumentVersioning = "YES"
65+
debugServiceExtension = "internal"
66+
allowLocationSimulation = "YES">
67+
</LaunchAction>
68+
<ProfileAction
69+
buildConfiguration = "Release"
70+
shouldUseLaunchSchemeArgsEnv = "YES"
71+
savedToolIdentifier = ""
72+
useCustomWorkingDirectory = "NO"
73+
debugDocumentVersioning = "YES">
74+
<MacroExpansion>
75+
<BuildableReference
76+
BuildableIdentifier = "primary"
77+
BlueprintIdentifier = "CombineExt"
78+
BuildableName = "CombineExt"
79+
BlueprintName = "CombineExt"
80+
ReferencedContainer = "container:">
81+
</BuildableReference>
82+
</MacroExpansion>
83+
</ProfileAction>
84+
<AnalyzeAction
85+
buildConfiguration = "Debug">
86+
</AnalyzeAction>
87+
<ArchiveAction
88+
buildConfiguration = "Release"
89+
revealArchiveInOrganizer = "YES">
90+
</ArchiveAction>
91+
</Scheme>

CombineExt.podspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ Pod::Spec.new do |s|
1818
s.source = { :git => "https://github.com/CombineCommunity/CombineExt.git", :tag => s.version }
1919
s.source_files = 'Sources/**/*.swift'
2020
s.frameworks = ['Combine']
21-
s.swift_version = '5.1'
21+
s.swift_version = '5.5'
2222
end

README.md

+95
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,101 @@ subscription = ints
755755
.finished
756756
```
757757

758+
------
759+
760+
### fromAsync(priority:_:)
761+
762+
Creates a Combine Publisher from an async function. The Publisher emits a value and then completes when the async function returns its result.
763+
The task that supports the async function is canceled when the publisher's subscription is canceled.
764+
An optional priority indicates the priority of the Task supporting the execution of the async function.
765+
766+
```swift
767+
var value: Int {
768+
get async {
769+
3
770+
}
771+
}
772+
773+
Publishers
774+
.fromAsync {
775+
await value
776+
}.sink {
777+
print($0)
778+
} receiveValue: {
779+
print($0)
780+
}
781+
```
782+
783+
#### Output:
784+
785+
```none
786+
3
787+
finished
788+
```
789+
790+
------
791+
792+
### fromThrowingAsync(priority:_:)
793+
794+
Creates a Combine Publisher from a throwing async function
795+
The Publisher emits a value and completes or fail according the the async function execution result.
796+
The task that supports the async function is canceled when the publisher's subscription is canceled.
797+
An optional priority indicates the priority of the Task supporting the execution of the async function.
798+
799+
```swift
800+
struct MyError: Error, CustomStringConvertible {
801+
var description: String {
802+
"Async Error"
803+
}
804+
}
805+
806+
Publishers
807+
.fromAsync { () async throws -> String in
808+
throw MyError()
809+
}.sink {
810+
print($0)
811+
} receiveValue: {
812+
print($0)
813+
}
814+
```
815+
816+
#### Output:
817+
818+
```none
819+
failure(Async Error)
820+
```
821+
822+
### fromAsyncSequence(priority:_:)
823+
824+
Creates a Combine Publisher from an async sequence.
825+
The Publisher emits values or fail according the the async sequence execution result.
826+
An optional priority indicates the priority of the Task supporting the execution of the async sequence.
827+
828+
```swift
829+
let sequence = AsyncStream(Int.self) { continuation in
830+
continuation.yield(1)
831+
continuation.yield(2)
832+
continuation.yield(3)
833+
continuation.finish()
834+
}
835+
836+
Publishers
837+
.fromAsyncSequence(sequence).sink {
838+
print($0)
839+
} receiveValue: {
840+
print($0)
841+
}
842+
```
843+
844+
#### Output:
845+
846+
```none
847+
1
848+
2
849+
3
850+
finished
851+
```
852+
758853
## Publishers
759854

760855
This section outlines some of the custom Combine publishers CombineExt provides

0 commit comments

Comments
 (0)