From 55497aa820e4d639d04a7954eb9dec4d593ad225 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 | 28 ++++++++++++++++--- 2 files changed, 26 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..d03397ff 100644 --- a/Sources/ACKategories/Combine+Concurrency.swift +++ b/Sources/ACKategories/Combine+Concurrency.swift @@ -1,8 +1,8 @@ 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 { + convenience init(operation: @escaping () async throws -> Output) where Failure == Error { self.init { promise in Task { do { @@ -13,11 +13,31 @@ public extension Future where Failure == Error { } } } + + convenience init(operation: @escaping () async -> Result) { + self.init { promise in + Task { promise(await operation()) } + } + } + + 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 { + init(operation: @escaping () async throws -> Output) where Failure == Error { self = Future { try await operation() }.eraseToAnyPublisher() } + + init(operation: @escaping () async -> Result) { + self = Future { await operation() }.eraseToAnyPublisher() + } + + init(operation: @escaping () async -> Output) where Failure == Never { + self = Future { await operation() }.eraseToAnyPublisher() + } }