Skip to content
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

operators: add fromAsync, fromThrowingAsync, fromAsyncSequence #106

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 8 additions & 19 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,30 @@ on: [push, pull_request, workflow_dispatch]
jobs:
xcode-tests:
name: "Test"
runs-on: macOS-latest
runs-on: macOS-11

strategy:
matrix:
platform: [macOS, iOS, tvOS]
include:
- platform: macOS
sdk: macosx
sdk: macosx11.3
destination: "arch=x86_64"

- platform: iOS
sdk: iphonesimulator
destination: "name=iPhone 11"
sdk: iphoneos15.0
destination: "name=iPhone 13"

- platform: tvOS
sdk: appletvsimulator
sdk: appletvsimulator15.0
destination: "name=Apple TV"

steps:
- uses: actions/checkout@v2
- name: Select Xcode 12 (beta)
run: sudo xcode-select -s /Applications/Xcode_12_beta.app
- name: Generate project
run: make project
- name: Select Xcode 13
run: sudo xcode-select -s /Applications/Xcode_13.0.app
- name: Run tests
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
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
- uses: codecov/[email protected]
with:
token: 1519d58c-6fb9-483f-af6c-7f6f0b384345
Expand All @@ -39,12 +37,3 @@ jobs:
with:
name: build-logs-${{ github.run_id }}
path: logs

SPM:
name: "Test (SPM)"
runs-on: macOS-latest

steps:
- uses: actions/checkout@v2
- name: Run tests
run: set -o pipefail && swift test
91 changes: 91 additions & 0 deletions .swiftpm/xcode/xcshareddata/xcschemes/CombineExt.xcscheme
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?xml version="1.0" encoding="UTF-8"?>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should not commit this file.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add this to gitignore:

# Ignore all schemes located in swiftpm
**/.swiftpm/**/xcschemes/

<Scheme
LastUpgradeVersion = "1300"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "CombineExt"
BuildableName = "CombineExt"
BlueprintName = "CombineExt"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "NO"
buildForArchiving = "NO"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "CombineExtTests"
BuildableName = "CombineExtTests"
BlueprintName = "CombineExtTests"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "CombineExtTests"
BuildableName = "CombineExtTests"
BlueprintName = "CombineExtTests"
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "CombineExt"
BuildableName = "CombineExt"
BlueprintName = "CombineExt"
ReferencedContainer = "container:">
</BuildableReference>
</MacroExpansion>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
2 changes: 1 addition & 1 deletion CombineExt.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ Pod::Spec.new do |s|
s.source = { :git => "https://github.com/CombineCommunity/CombineExt.git", :tag => s.version }
s.source_files = 'Sources/**/*.swift'
s.frameworks = ['Combine']
s.swift_version = '5.1'
s.swift_version = '5.5'
end
95 changes: 95 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,101 @@ subscription = ints
.finished
```

------

### fromAsync(priority:_:)

Creates a Combine Publisher from an async function. The Publisher emits a value and then completes when the async function returns its result.
The task that supports the async function is canceled when the publisher's subscription is canceled.
An optional priority indicates the priority of the Task supporting the execution of the async function.

```swift
var value: Int {
get async {
3
}
}

Publishers
.fromAsync {
await value
}.sink {
print($0)
} receiveValue: {
print($0)
}
```

#### Output:

```none
3
finished
```

------

### fromThrowingAsync(priority:_:)

Creates a Combine Publisher from a throwing async function
The Publisher emits a value and completes or fail according the the async function execution result.
The task that supports the async function is canceled when the publisher's subscription is canceled.
An optional priority indicates the priority of the Task supporting the execution of the async function.

```swift
struct MyError: Error, CustomStringConvertible {
var description: String {
"Async Error"
}
}

Publishers
.fromAsync { () async throws -> String in
throw MyError()
}.sink {
print($0)
} receiveValue: {
print($0)
}
```

#### Output:

```none
failure(Async Error)
```

### fromAsyncSequence(priority:_:)

Creates a Combine Publisher from an async sequence.
The Publisher emits values or fail according the the async sequence execution result.
An optional priority indicates the priority of the Task supporting the execution of the async sequence.

```swift
let sequence = AsyncStream(Int.self) { continuation in
continuation.yield(1)
continuation.yield(2)
continuation.yield(3)
continuation.finish()
}

Publishers
.fromAsyncSequence(sequence).sink {
print($0)
} receiveValue: {
print($0)
}
```

#### Output:

```none
1
2
3
finished
```

## Publishers

This section outlines some of the custom Combine publishers CombineExt provides
Expand Down
Loading