From 96fb5736b202a0be1763be4d59206192609c5db1 Mon Sep 17 00:00:00 2001 From: Jakub Olejnik Date: Thu, 14 Mar 2024 17:15:38 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20new=20extensions=20for=20Comb?= =?UTF-8?q?ine+Concurrency?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 2 + .../ACKategories/Combine+Concurrency.swift | 40 +++++++++++++++++-- 2 files changed, 38 insertions(+), 4 deletions(-) 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() + } }