-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,38 @@ import Foundation | |
#if swift(>=5.6) && canImport(_Concurrency) && !os(Linux) | ||
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) | ||
public extension PrimitiveSequenceType where Trait == SingleTrait { | ||
/** | ||
Creates an `Single` from the result of an asynchronous operation | ||
|
||
- seealso: [create operator on reactivex.io](http://reactivex.io/documentation/operators/create.html) | ||
|
||
- parameter work: An `async` closure expected to return an element of type `Element` | ||
|
||
- returns: A `Single` of the `async` closure's element type | ||
*/ | ||
@_disfavoredOverload | ||
static func create( | ||
detached: Bool = false, | ||
priority: TaskPriority? = nil, | ||
work: @Sendable @escaping () async throws -> Element | ||
) -> PrimitiveSequence<Trait, Element> { | ||
.create { single in | ||
let operation: () async throws -> Void = { | ||
await single( | ||
Result { try await work() } | ||
) | ||
} | ||
|
||
let task = if detached { | ||
Task.detached(priority: priority, operation: operation) | ||
Check warning on line 37 in RxSwift/Traits/PrimitiveSequence/PrimitiveSequence+Concurrency.swift GitHub Actions / Xcode 15 (Unix)
Check warning on line 37 in RxSwift/Traits/PrimitiveSequence/PrimitiveSequence+Concurrency.swift GitHub Actions / Xcode 15 (Unix)
Check warning on line 37 in RxSwift/Traits/PrimitiveSequence/PrimitiveSequence+Concurrency.swift GitHub Actions / Xcode 15 (Unix)
Check warning on line 37 in RxSwift/Traits/PrimitiveSequence/PrimitiveSequence+Concurrency.swift GitHub Actions / Xcode 15 (iOS-Example)
Check warning on line 37 in RxSwift/Traits/PrimitiveSequence/PrimitiveSequence+Concurrency.swift GitHub Actions / Xcode 15 (tvOS)
Check warning on line 37 in RxSwift/Traits/PrimitiveSequence/PrimitiveSequence+Concurrency.swift GitHub Actions / Xcode 15 (iOS)
Check warning on line 37 in RxSwift/Traits/PrimitiveSequence/PrimitiveSequence+Concurrency.swift GitHub Actions / Xcode 15 (watchOS)
|
||
} else { | ||
Task(priority: priority, operation: operation) | ||
Check warning on line 39 in RxSwift/Traits/PrimitiveSequence/PrimitiveSequence+Concurrency.swift GitHub Actions / Xcode 15 (Unix)
Check warning on line 39 in RxSwift/Traits/PrimitiveSequence/PrimitiveSequence+Concurrency.swift GitHub Actions / Xcode 15 (Unix)
Check warning on line 39 in RxSwift/Traits/PrimitiveSequence/PrimitiveSequence+Concurrency.swift GitHub Actions / Xcode 15 (Unix)
Check warning on line 39 in RxSwift/Traits/PrimitiveSequence/PrimitiveSequence+Concurrency.swift GitHub Actions / Xcode 15 (iOS-Example)
Check warning on line 39 in RxSwift/Traits/PrimitiveSequence/PrimitiveSequence+Concurrency.swift GitHub Actions / Xcode 15 (tvOS)
Check warning on line 39 in RxSwift/Traits/PrimitiveSequence/PrimitiveSequence+Concurrency.swift GitHub Actions / Xcode 15 (iOS)
Check warning on line 39 in RxSwift/Traits/PrimitiveSequence/PrimitiveSequence+Concurrency.swift GitHub Actions / Xcode 15 (watchOS)
|
||
} | ||
|
||
return Disposables.create { task.cancel() } | ||
} | ||
} | ||
|
||
/// Allows awaiting the success or failure of this `Single` | ||
/// asynchronously via Swift's concurrency features (`async/await`) | ||
/// | ||
|
@@ -161,4 +193,16 @@ public extension PrimitiveSequenceType where Trait == CompletableTrait, Element | |
} | ||
} | ||
} | ||
|
||
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) | ||
extension Result where Failure == Swift.Error { | ||
@_disfavoredOverload | ||
init(catching body: () async throws -> Success) async { | ||
do { | ||
self = try await .success(body()) | ||
} catch { | ||
self = .failure(error) | ||
} | ||
} | ||
} | ||
#endif |