diff --git a/CHANGELOG.md b/CHANGELOG.md index 6e682d42..9cb1ee6f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ ## Next +- Add new helpers for Combine+Concurrency ([#147](https://github.com/AckeeCZ/ACKategories/pull/147), kudos to @olejnjak) + ## 6.13.0 - Merge [iOS template lib](https://github.com/AckeeCZ/iOS-MVVM-ProjectTemplate) ([#143](https://github.com/AckeeCZ/ACKategories/pull/143), kudos to @olejnjak) - add Networking module diff --git a/Sources/ACKategories/Combine+Concurrency.swift b/Sources/ACKategories/Combine+Concurrency.swift index 23fa732a..eda736d1 100644 --- a/Sources/ACKategories/Combine+Concurrency.swift +++ b/Sources/ACKategories/Combine+Concurrency.swift @@ -1,8 +1,10 @@ import Combine @available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) -public extension Future where Failure == Error { - convenience init(operation: @escaping () async throws -> Output) { +public extension Future { + /// Run async operation as Publisher + /// - Parameter operation: Operation to be run + convenience init(operation: @escaping () async throws -> Output) where Failure == Error { self.init { promise in Task { do { @@ -13,11 +15,41 @@ public extension Future where Failure == Error { } } } + + /// Run async operation as Publisher + /// - Parameter operation: Operation to be run + convenience init(operation: @escaping () async -> Result) { + self.init { promise in + Task { promise(await operation()) } + } + } + + /// Run async operation as Publisher + /// - Parameter operation: Operation to be run + convenience init(operation: @escaping () async -> Output) where Failure == Never { + self.init { promise in + Task { promise(.success(await operation())) } + } + } } @available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) -public extension AnyPublisher where Failure == Error { - init(operation: @escaping () async throws -> Output) { +public extension AnyPublisher { + /// Run async operation as Publisher + /// - Parameter operation: Operation to be run + init(operation: @escaping () async throws -> Output) where Failure == Error { self = Future { try await operation() }.eraseToAnyPublisher() } + + /// Run async operation as Publisher + /// - Parameter operation: Operation to be run + init(operation: @escaping () async -> Result) { + self = Future { await operation() }.eraseToAnyPublisher() + } + + /// Run async operation as Publisher + /// - Parameter operation: Operation to be run + init(operation: @escaping () async -> Output) where Failure == Never { + self = Future { await operation() }.eraseToAnyPublisher() + } }