From f6a49d13228079e78bccd5486720325208e8e8fa Mon Sep 17 00:00:00 2001 From: Dale Swift Date: Thu, 12 May 2022 14:37:02 +1000 Subject: [PATCH 1/2] Add overrides to support Infallible --- Sources/Rx+Combine/Infallible+Combine.swift | 44 +++++++++++++++++++++ Tests/ObservableAsPublisherTests.swift | 15 +++++++ 2 files changed, 59 insertions(+) create mode 100644 Sources/Rx+Combine/Infallible+Combine.swift diff --git a/Sources/Rx+Combine/Infallible+Combine.swift b/Sources/Rx+Combine/Infallible+Combine.swift new file mode 100644 index 0000000..3c84ed5 --- /dev/null +++ b/Sources/Rx+Combine/Infallible+Combine.swift @@ -0,0 +1,44 @@ +// +// Infallible+Combine.swift +// RxCombine + +#if canImport(Combine) +import Combine +import RxSwift + +@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) +public extension Infallible { + /// An `AnyPublisher` of the underlying Observable's Element type + /// so the Infallible pushes events to the Publisher. + var publisher: AnyPublisher { + RxInfalliblePublisher(upstream: self).eraseToAnyPublisher() + } + + /// Returns a `AnyPublisher` of the underlying Observable's Element type + /// so the Infallible pushes events to the Publisher. + /// + /// - returns: AnyPublisher of the underlying Observable's Element type. + /// - note: This is an alias for the `publisher` property. + func asPublisher() -> AnyPublisher { + publisher + } +} + +/// A Publisher of failure type Never pushing RxSwift events to a Downstream Combine subscriber. +@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) +public class RxInfalliblePublisher: Publisher { + public typealias Output = Upstream.Element + public typealias Failure = Swift.Never + + private let upstream: Upstream + + init(upstream: Upstream) { + self.upstream = upstream + } + + public func receive(subscriber: S) where Failure == S.Failure, Output == S.Input { + subscriber.receive(subscription: RxInfallibleSubscription(upstream: upstream, + downstream: subscriber)) + } +} +#endif diff --git a/Tests/ObservableAsPublisherTests.swift b/Tests/ObservableAsPublisherTests.swift index 838ec7b..be694b0 100644 --- a/Tests/ObservableAsPublisherTests.swift +++ b/Tests/ObservableAsPublisherTests.swift @@ -86,6 +86,21 @@ class ObservableAsPublisherTests: XCTestCase { XCTAssertEqual(values, Array(1...10)) XCTAssertTrue(completed) } + + func testStringInfallible() { + let input = "Hello world I'm a RxSwift Infallible".components(separatedBy: " ") + let source = Infallible.from(input) + var values = [String]() + var completed = false + + subscription = source + .asPublisher() + .handleEvents(receiveCompletion: { _ in completed = true }) + .sink(receiveValue: { values.append($0) }) // this syntax is only allowed when Failure == Never + + XCTAssertEqual(values, input) + XCTAssertTrue(completed) + } } enum FakeError: Swift.Error { From 6767071890045be0b4ad84803498ca4cb98cff69 Mon Sep 17 00:00:00 2001 From: Dale Swift Date: Fri, 20 May 2022 12:09:43 +1000 Subject: [PATCH 2/2] Add overrides to support Infallible Simplify the code --- Sources/Rx+Combine/Infallible+Combine.swift | 22 ++++----------------- 1 file changed, 4 insertions(+), 18 deletions(-) diff --git a/Sources/Rx+Combine/Infallible+Combine.swift b/Sources/Rx+Combine/Infallible+Combine.swift index 3c84ed5..7742627 100644 --- a/Sources/Rx+Combine/Infallible+Combine.swift +++ b/Sources/Rx+Combine/Infallible+Combine.swift @@ -11,7 +11,10 @@ public extension Infallible { /// An `AnyPublisher` of the underlying Observable's Element type /// so the Infallible pushes events to the Publisher. var publisher: AnyPublisher { - RxInfalliblePublisher(upstream: self).eraseToAnyPublisher() + asObservable() + .asPublisher() + .assertNoFailure("Infallible should not fail") + .eraseToAnyPublisher() } /// Returns a `AnyPublisher` of the underlying Observable's Element type @@ -24,21 +27,4 @@ public extension Infallible { } } -/// A Publisher of failure type Never pushing RxSwift events to a Downstream Combine subscriber. -@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) -public class RxInfalliblePublisher: Publisher { - public typealias Output = Upstream.Element - public typealias Failure = Swift.Never - - private let upstream: Upstream - - init(upstream: Upstream) { - self.upstream = upstream - } - - public func receive(subscriber: S) where Failure == S.Failure, Output == S.Input { - subscriber.receive(subscription: RxInfallibleSubscription(upstream: upstream, - downstream: subscriber)) - } -} #endif