Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
30 changes: 30 additions & 0 deletions Sources/Rx+Combine/Infallible+Combine.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// 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> {
asObservable()
.asPublisher()
.assertNoFailure("Infallible should not fail")
.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<Element, Swift.Never> {
publisher
}
}

#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