RxCombine provides bi-directional type bridging between RxSwift and Apple's Combine framework.
Note: This is highly experimental, and basically just a quickly-put-together PoC. I gladly except PRs, ideas, opinions, or improvements. Thank you ! :)
Check out the Example App in the ExampleApp folder. Run pod install
before opening the project.
Add the following line to your Podfile:
pod 'RxCombine'
Add the following dependency to your Package.swift file:
.package(url: "https://github.com/freak4pc/RxCombine.git", from: "1.0.0")
No Carthage support yet. I hope to have the time to take care of it soon.
Feel free to open a PR !
RxCombine provides several helpers and conversions to help you bridge your existing RxSwift types to Combine.
Note: If you want to learn more about the parallel operators in Combine from RxSwift, check out my RxSwift to Combine Cheat Sheet (or on GitHub).
Observable
(and otherObservableConvertibleType
s) have apublisher
property which returns aAnyPublisher<Element, Swift.Error>
mirroring the underlyingObservable
.
let observable = Observable.just("Hello, Combine!")
observable
.publisher // AnyPublisher<String, Swift.Error>
.sink(receiveValue: { value in ... })
Relays
andSubjects
conform toCombine.Subject
, so you can use them as if they are regular Combine Subjects.
let relay = BehaviorRelay<Int>(value: 0)
// Use `sink` on RxSwift relay
relay
.sink(receiveValue: { value in ... })
// Use `send(value:)` on RxSwift relay
relay.send(1)
relay.send(2)
relay.send(3)
RxCombine provides several helpers and conversions to help you bridge Combine code and types into your existing RxSwift codebase.
Publisher
s have aasObservable()
method, providing anObservable<Output>
mirroring the underlyingPublisher
.
// A publisher publishing numbers from 0 to 100.
let publisher = AnyPublisher<Int, Swift.Error> { subscriber in
(0...100).forEach { _ = subscriber.receive($0) }
subscriber.receive(completion: .finished)
}
publisher
.asObservable() // Observable<Int>
.subscribe(onNext: { num in ... })
PassthroughSubject
andCurrentValueSubject
both have aasAnyObserver()
method which returns aAnyObserver<Output>
. Binding to it from your RxSwift code pushes the events to the underlying Combine Subject.
// Combine Subject
let subject = PassthroughSubject<Int, Swift.Error>()
// A publisher publishing numbers from 0 to 100.
let publisher = AnyPublisher<Int, Swift.Error> { subscriber in
(0...100).forEach { _ = subscriber.receive($0) }
subscriber.receive(completion: .finished)
}
// Convert a Publisher to an Observable and bind it
// back to a Combine Subject 🤯🤯🤯
publisher.asObservable()
.bind(to: subject)
Observable.of(10, 5, 7, 4, 1, 6)
.subscribe(subject.asAnyObserver())
- Add CI / Tests
- Carthage Support
- Bridge SwiftUI with RxCocoa/RxSwift
- Partial Backpressure support, perhaps?
- ... your ideas? :)
MIT, of course ;-) See the LICENSE file.
The Apple logo and the Combine framework are property of Apple Inc.