Skip to content

Commit c6d594e

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

File tree

6 files changed

+663
-24
lines changed

6 files changed

+663
-24
lines changed

.github/workflows/tests.yml

+13-23
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,31 @@ 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:
12-
platform: [macOS, iOS, tvOS]
12+
# platform: [macOS, iOS, tvOS]
13+
platform: [iOS, tvOS]
1314
include:
14-
- platform: macOS
15-
sdk: macosx
16-
destination: "arch=x86_64"
17-
15+
# - platform: macOS
16+
# sdk: macosx11.3
17+
# destination: "arch=x86_64"
18+
#
1819
- platform: iOS
19-
sdk: iphonesimulator
20-
destination: "name=iPhone 11"
20+
sdk: iphoneos15.0
21+
destination: "name=iPhone 13"
2122

2223
- platform: tvOS
23-
sdk: appletvsimulator
24+
sdk: appletvsimulator15.0
2425
destination: "name=Apple TV"
2526

2627
steps:
2728
- 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
29+
- name: Select Xcode 13
30+
run: sudo xcode-select -s /Applications/Xcode_13.0.app
3231
- 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
32+
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
3433
- uses: codecov/[email protected]
3534
with:
3635
token: 1519d58c-6fb9-483f-af6c-7f6f0b384345
@@ -39,12 +38,3 @@ jobs:
3938
with:
4039
name: build-logs-${{ github.run_id }}
4140
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

.github/workflows/tests.yml~

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
name: CombineExt
2+
3+
on: [push, pull_request, workflow_dispatch]
4+
5+
jobs:
6+
xcode-tests:
7+
name: "Test"
8+
runs-on: macOS-11
9+
10+
strategy:
11+
matrix:
12+
# platform: [macOS, iOS, tvOS]
13+
platform: [iOS, tvOS]
14+
include:
15+
# - platform: macOS
16+
# sdk: macosx11.3
17+
# destination: "arch=x86_64"
18+
#
19+
- platform: iOS
20+
sdk: iphoneos15.0
21+
destination: "name=iPhone 13"
22+
23+
- platform: tvOS
24+
sdk: appletvsimulator15.0
25+
destination: "name=Apple TV"
26+
27+
steps:
28+
- uses: actions/checkout@v2
29+
- name: Select Xcode 13
30+
run: sudo xcode-select -s /Applications/Xcode_13.0.app
31+
# - name: Generate project
32+
# run: make project
33+
- name: Run tests
34+
run: set -o pipefail && xcodebuild -scheme CombineExt -enableCodeCoverage YES -sdk ${{ matrix.sdk }} -destination "${{ matrix.destination }}" test | xcpretty -c -r html --output logs/${{ matrix.platform }}.html
35+
- uses: codecov/[email protected]
36+
with:
37+
token: 1519d58c-6fb9-483f-af6c-7f6f0b384345
38+
name: CombineExt
39+
- uses: actions/upload-artifact@v1
40+
with:
41+
name: build-logs-${{ github.run_id }}
42+
path: logs
43+
44+
# SPM:
45+
# name: "Test (SPM)"
46+
# runs-on: macOS-11
47+
#
48+
# steps:
49+
# - uses: fwal/setup-swift@v1
50+
# with:
51+
# swift-version: "5.5.0"
52+
# - uses: actions/checkout@v2
53+
# - name: Run tests
54+
# run: set -o pipefail && swift test

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)