diff --git a/Package.swift b/Package.swift index dc7e3a1..787fe5f 100644 --- a/Package.swift +++ b/Package.swift @@ -2,6 +2,16 @@ import PackageDescription +var dependencies: [Package.Dependency] = [ + .package(url: "https://github.com/ReactiveX/RxSwift.git", from: "6.0.0") +] +var targetDependencies: [Target.Dependency] = ["RxSwift", "RxRelay"] + +#if os(Linux) +dependencies.append(.package(url: "https://github.com/cx-org/CombineX", from: "0.4.0")) +targetDependencies.append("CombineX") +#endif + let package = Package( name: "RxCombine", platforms: [ @@ -10,15 +20,14 @@ let package = Package( products: [ .library( name: "RxCombine", - targets: ["RxCombine"]), - ], - dependencies: [ - .package(url: "https://github.com/ReactiveX/RxSwift.git", from: "6.0.0") + targets: ["RxCombine"] + ), ], + dependencies: dependencies, targets: [ .target( name: "RxCombine", - dependencies: ["RxSwift", "RxRelay"], + dependencies: targetDependencies, path: "Sources"), .testTarget( name: "RxCombineTests", diff --git a/Sources/Combine+Rx/Publisher+Rx.swift b/Sources/Combine+Rx/Publisher+Rx.swift index 771e280..2cff8a0 100644 --- a/Sources/Combine+Rx/Publisher+Rx.swift +++ b/Sources/Combine+Rx/Publisher+Rx.swift @@ -6,8 +6,12 @@ // Copyright © 2019 Combine Community. All rights reserved. // +#if canImport(Combine) || canImport(CombineX) #if canImport(Combine) import Combine +#elseif canImport(CombineX) +import CombineX +#endif import RxSwift @available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) diff --git a/Sources/Combine+Rx/Subject+Rx.swift b/Sources/Combine+Rx/Subject+Rx.swift index 5d48d70..811a77f 100644 --- a/Sources/Combine+Rx/Subject+Rx.swift +++ b/Sources/Combine+Rx/Subject+Rx.swift @@ -6,8 +6,15 @@ // Copyright © 2019 Combine Community. All rights reserved. // +#if canImport(Combine) || canImport(CombineX) #if canImport(Combine) import Combine +@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) +public typealias Subject = Combine.Subject +#elseif canImport(CombineX) +import CombineX +public typealias Subject = CombineX.Subject +#endif import RxSwift import RxRelay @@ -17,7 +24,7 @@ import RxRelay /// - note: This only works when the underlying Failure is Swift.Error, /// since RxSwift has no typed errors. @available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) -public protocol AnyObserverConvertible: Combine.Subject where Failure == Swift.Error { +public protocol AnyObserverConvertible: Subject where Failure == Swift.Error { associatedtype Output /// Returns a RxSwift `AnyObserver` wrapping the Subject diff --git a/Sources/Common/DemandBuffer.swift b/Sources/Common/DemandBuffer.swift index 0c8807a..3b8c27e 100644 --- a/Sources/Common/DemandBuffer.swift +++ b/Sources/Common/DemandBuffer.swift @@ -6,8 +6,12 @@ // Copyright © 2020 Combine Community. All rights reserved. // +#if canImport(Combine) || canImport(CombineX) #if canImport(Combine) import Combine +#elseif canImport(CombineX) +import CombineX +#endif import class Foundation.NSRecursiveLock /// A buffer responsible for managing the demand of a downstream diff --git a/Sources/Rx+Combine/BehaviorRelay+Combine.swift b/Sources/Rx+Combine/BehaviorRelay+Combine.swift index 85d31e4..5b04105 100644 --- a/Sources/Rx+Combine/BehaviorRelay+Combine.swift +++ b/Sources/Rx+Combine/BehaviorRelay+Combine.swift @@ -5,8 +5,12 @@ // Created by Shai Mishali on 10/05/2020. // +#if canImport(Combine) || canImport(CombineX) #if canImport(Combine) import Combine +#elseif canImport(CombineX) +import CombineX +#endif import RxSwift import RxRelay @@ -14,7 +18,7 @@ import RxRelay /// A bi-directional wrapper for a RxSwift Behavior Relay @available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) -public final class RxCurrentValueRelay: Combine.Subject { +public final class RxCurrentValueRelay: Subject { private let rxRelay: BehaviorRelay private let subject: CurrentValueSubject private let subscription: AnyCancellable? diff --git a/Sources/Rx+Combine/BehaviorSubject+Combine.swift b/Sources/Rx+Combine/BehaviorSubject+Combine.swift index 62eea2d..644a3a9 100644 --- a/Sources/Rx+Combine/BehaviorSubject+Combine.swift +++ b/Sources/Rx+Combine/BehaviorSubject+Combine.swift @@ -6,15 +6,19 @@ // Copyright © 2019 Combine Community. All rights reserved. // +#if canImport(Combine) || canImport(CombineX) #if canImport(Combine) import Combine +#elseif canImport(CombineX) +import CombineX +#endif import RxSwift // MARK: - Behavior Subject as Combine Subject /// A bi-directional wrapper for a RxSwift Behavior Subject @available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) -public final class RxCurrentValueSubject: Combine.Subject { +public final class RxCurrentValueSubject: Subject { private let rxSubject: BehaviorSubject private let subject: CurrentValueSubject private let subscription: AnyCancellable? diff --git a/Sources/Rx+Combine/Observable+Combine.swift b/Sources/Rx+Combine/Observable+Combine.swift index 7c609c0..3d9872f 100644 --- a/Sources/Rx+Combine/Observable+Combine.swift +++ b/Sources/Rx+Combine/Observable+Combine.swift @@ -6,8 +6,12 @@ // Copyright © 2019 Combine Community. All rights reserved. // +#if canImport(Combine) || canImport(CombineX) #if canImport(Combine) import Combine +#elseif canImport(CombineX) +import CombineX +#endif import RxSwift @available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) diff --git a/Sources/Rx+Combine/PublishRelay+Combine.swift b/Sources/Rx+Combine/PublishRelay+Combine.swift index 196043c..15c96ac 100644 --- a/Sources/Rx+Combine/PublishRelay+Combine.swift +++ b/Sources/Rx+Combine/PublishRelay+Combine.swift @@ -5,8 +5,12 @@ // Created by Shai Mishali on 10/05/2020. // +#if canImport(Combine) || canImport(CombineX) #if canImport(Combine) import Combine +#elseif canImport(CombineX) +import CombineX +#endif import RxSwift import RxRelay @@ -14,7 +18,7 @@ import RxRelay /// A bi-directional wrapper for a RxSwift Publish Relay @available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) -public final class RxPassthroughRelay: Combine.Subject { +public final class RxPassthroughRelay: Subject { private let rxRelay: PublishRelay private let subject = PassthroughSubject() private let subscription: AnyCancellable? diff --git a/Sources/Rx+Combine/PublishSubject+Combine.swift b/Sources/Rx+Combine/PublishSubject+Combine.swift index 41770cb..c15d92f 100644 --- a/Sources/Rx+Combine/PublishSubject+Combine.swift +++ b/Sources/Rx+Combine/PublishSubject+Combine.swift @@ -6,15 +6,19 @@ // Copyright © 2019 Combine Community. All rights reserved. // +#if canImport(Combine) || canImport(CombineX) #if canImport(Combine) import Combine +#elseif canImport(CombineX) +import CombineX +#endif import RxSwift // MARK: - Behavior Subject as Combine Subject /// A bi-directional wrapper for a RxSwift Publish Subject @available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) -public final class RxPassthroughSubject: Combine.Subject { +public final class RxPassthroughSubject: Subject { private let rxSubject: PublishSubject private let subject = PassthroughSubject() private let subscription: AnyCancellable? diff --git a/Sources/Rx+Combine/Relays+Combine.swift b/Sources/Rx+Combine/Relays+Combine.swift index 6a56598..0cc4844 100644 --- a/Sources/Rx+Combine/Relays+Combine.swift +++ b/Sources/Rx+Combine/Relays+Combine.swift @@ -6,8 +6,12 @@ // Copyright © 2019 Combine Community. All rights reserved. // +#if canImport(Combine) || canImport(CombineX) #if canImport(Combine) import Combine +#elseif canImport(CombineX) +import CombineX +#endif import RxSwift import RxRelay diff --git a/Sources/Rx+Combine/RxSubscription.swift b/Sources/Rx+Combine/RxSubscription.swift index 3d91787..34f83b4 100644 --- a/Sources/Rx+Combine/RxSubscription.swift +++ b/Sources/Rx+Combine/RxSubscription.swift @@ -6,13 +6,20 @@ // Copyright © 2020 Combine Community. All rights reserved. // +#if canImport(Combine) || canImport(CombineX) #if canImport(Combine) import Combine +@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) +private typealias Subscription = Combine.Subscription +#elseif canImport(CombineX) +import CombineX +private typealias Subscription = CombineX.Subscription +#endif import RxSwift // MARK: - Fallible @available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) -class RxSubscription: Combine.Subscription where Downstream.Input == Upstream.Element, Downstream.Failure == Swift.Error { +class RxSubscription: Subscription where Downstream.Input == Upstream.Element, Downstream.Failure == Swift.Error { private var disposable: Disposable? private let buffer: DemandBuffer @@ -52,7 +59,7 @@ extension RxSubscription: CustomStringConvertible { // MARK: - Infallible @available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) -class RxInfallibleSubscription: Combine.Subscription where Downstream.Input == Upstream.Element, Downstream.Failure == Never { +class RxInfallibleSubscription: Subscription where Downstream.Input == Upstream.Element, Downstream.Failure == Never { private var disposable: Disposable? private let buffer: DemandBuffer diff --git a/Tests/ObservableAsPublisherTests.swift b/Tests/ObservableAsPublisherTests.swift index 838ec7b..738e624 100644 --- a/Tests/ObservableAsPublisherTests.swift +++ b/Tests/ObservableAsPublisherTests.swift @@ -6,11 +6,16 @@ // Copyright © 2019 Combine Community. All rights reserved. // -#if !os(watchOS) +#if !os(watchOS) && (canImport(Combine) || canImport(CombineX)) import XCTest import RxCombine import RxSwift + +#if canImport(Combine) import Combine +#elseif canImport(CombineX) +import CombineX +#endif @available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) class ObservableAsPublisherTests: XCTestCase { diff --git a/Tests/PublisherAsObservableTests.swift b/Tests/PublisherAsObservableTests.swift index 1b34ceb..f7888d0 100644 --- a/Tests/PublisherAsObservableTests.swift +++ b/Tests/PublisherAsObservableTests.swift @@ -5,11 +5,16 @@ // Created by Shai Mishali on 21/03/2020. // -#if !os(watchOS) +#if !os(watchOS) && (canImport(Combine) || canImport(CombineX)) import XCTest import RxCombine import RxSwift + +#if canImport(Combine) import Combine +#elseif canImport(CombineX) +import CombineX +#endif @available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) class PublisherAsObservableTests: XCTestCase { @@ -19,6 +24,7 @@ class PublisherAsObservableTests: XCTestCase { disposeBag = .init() } +#if canImport(Combine) func testIntPublisher() { let source = (1...100).publisher var events = [RxSwift.Event]() @@ -62,6 +68,51 @@ class PublisherAsObservableTests: XCTestCase { XCTAssertEqual(events, (1...14).map { .next($0) } + [.error(FakeError.ohNo)]) } +#elseif canImport(CombineX) + func testIntPublisher() { + let source = (1...100).cx.publisher + var events = [RxSwift.Event]() + + source + .asObservable() + .subscribe { events.append($0) } + .disposed(by: disposeBag) + + XCTAssertEqual(events, + (1...100).map { .next($0) } + [.completed]) + } + + func testStringPublisher() { + let input = "Hello world I'm a RxSwift Observable".components(separatedBy: " ") + let source = input.cx.publisher + var events = [RxSwift.Event]() + + source + .asObservable() + .subscribe { events.append($0) } + .disposed(by: disposeBag) + + XCTAssertEqual(events, input.map { .next($0) } + [.completed]) + } + + func testFailingPublisher() { + let source = (1...100).cx.publisher + var events = [RxSwift.Event]() + + source + .setFailureType(to: FakeError.self) + .tryMap { val -> Int in + guard val < 15 else { throw FakeError.ohNo } + return val + } + .asObservable() + .subscribe { events.append($0) } + .disposed(by: disposeBag) + + + XCTAssertEqual(events, (1...14).map { .next($0) } + [.error(FakeError.ohNo)]) + } +#endif } diff --git a/Tests/RxRelaysToCombineTests.swift b/Tests/RxRelaysToCombineTests.swift index 23db3ce..834744a 100644 --- a/Tests/RxRelaysToCombineTests.swift +++ b/Tests/RxRelaysToCombineTests.swift @@ -5,12 +5,17 @@ // Created by Shai Mishali on 21/03/2020. // -#if !os(watchOS) +#if !os(watchOS) && (canImport(Combine) || canImport(CombineX)) import XCTest import RxCombine import RxSwift import RxRelay + +#if canImport(Combine) import Combine +#elseif canImport(CombineX) +import CombineX +#endif @available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) class RxRelaysToCombineTests: XCTestCase { diff --git a/Tests/RxSubjectsToCombineTests.swift b/Tests/RxSubjectsToCombineTests.swift index 9158d76..fbaf8ab 100644 --- a/Tests/RxSubjectsToCombineTests.swift +++ b/Tests/RxSubjectsToCombineTests.swift @@ -6,11 +6,16 @@ // Copyright © 2019 Combine Community. All rights reserved. // -#if !os(watchOS) +#if !os(watchOS) && (canImport(Combine) || canImport(CombineX)) import XCTest import RxCombine import RxSwift + +#if canImport(Combine) import Combine +#elseif canImport(CombineX) +import CombineX +#endif @available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) class RxBehaviorSubjectToCombineTests: XCTestCase {