Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add overrides to support Infallible #35

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions Sources/Rx+Combine/Infallible+Combine.swift
Original file line number Diff line number Diff line change
@@ -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<Element, Swift.Never> {
RxInfalliblePublisher(upstream: self).eraseToAnyPublisher()
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is great. Another option could probably just do asPublisher().assertNoFailure("Infallible should not fail") and that way you don't need another custom Publisher type. WDYT?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. Could do that too. But RxInfallibleSubscriber already exists so why not use that? Seems a cleaner solution.
Either way. If you prefer assertNoFailure I’ll update to that

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you mean RxInfalliblePublisher ? it didn't exist, you just created it. I'm not keen on adding a new publisher like this because it inherently doesn't "do" anything and we erase the type anyways.

Copy link
Author

@daleswift daleswift May 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I was referring to RxInfallibleSubscription which does already exist but is not used anywhere.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anyway, I've changed it to use assertNoFailure as you suggested


/// 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<Element, Swift.Never> {
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<Upstream: ObservableConvertibleType>: Publisher {
public typealias Output = Upstream.Element
public typealias Failure = Swift.Never

private let upstream: Upstream

init(upstream: Upstream) {
self.upstream = upstream
}

public func receive<S: Subscriber>(subscriber: S) where Failure == S.Failure, Output == S.Input {
subscriber.receive(subscription: RxInfallibleSubscription(upstream: upstream,
downstream: subscriber))
}
}
#endif
15 changes: 15 additions & 0 deletions Tests/ObservableAsPublisherTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down