Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/combinecommunity/rxcombine
Bi-directional type bridging between RxSwift and Apple's Combine framework
https://github.com/combinecommunity/rxcombine
applecombine combine frp ios linux macos reactive reactive-programming reactive-streams rxswift swift watchos
Last synced: 21 days ago
JSON representation
Bi-directional type bridging between RxSwift and Apple's Combine framework
- Host: GitHub
- URL: https://github.com/combinecommunity/rxcombine
- Owner: CombineCommunity
- License: mit
- Created: 2019-06-11T19:25:48.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2024-04-21T15:21:37.000Z (7 months ago)
- Last Synced: 2024-10-15T06:02:05.465Z (21 days ago)
- Topics: applecombine, combine, frp, ios, linux, macos, reactive, reactive-programming, reactive-streams, rxswift, swift, watchos
- Language: Swift
- Size: 914 KB
- Stars: 1,034
- Watchers: 24
- Forks: 86
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# RxCombine
RxCombine provides bi-directional type bridging between [RxSwift](https://github.com/ReactiveX/RxSwift.git) and Apple's [Combine](https://developer.apple.com/documentation/combine) framework.
**Note**: This is highly experimental, and basically just a quickly-put-together PoC. I gladly accept PRs, ideas, opinions, or improvements. Thank you ! :)
## Basic Examples
Check out the Example App in the **ExampleApp** folder. Run `pod install` before opening the project.
## Installation
### CocoaPods
Add the following line to your **Podfile**:
```rb
pod 'RxCombine'
```### Swift Package Manager
Add the following dependency to your **Package.swift** file:
```swift
.package(url: "https://github.com/CombineCommunity/RxCombine.git", from: "1.6.0")
```### Carthage
Carthage support is offered as a prebuilt binary.
Add the following to your **Cartfile**:
```
github "CombineCommunity/RxCombine"
```## I want to ...
### Use RxSwift in my Combine code
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](https://medium.com/gett-engineering/rxswift-to-apples-combine-cheat-sheet-e9ce32b14c5b) *(or on [GitHub](https://github.com/freak4pc/rxswift-to-combine-cheatsheet))*.
* `Observable` (and other `ObservableConvertibleType`s) have a `publisher` property which returns a `AnyPublisher` mirroring the underlying `Observable`.
```swift
let observable = Observable.just("Hello, Combine!")observable
.publisher // AnyPublisher
.sink(receiveValue: { value in ... })
```* `Relays` and `Subjects` can be converted to their Combine-counterparts using the `toCombine()` method, so you can use them as if they are regular Combine Subjects, and have them connected to your existing subjects.
```swift
let relay = BehaviorRelay(value: 0)// Use `sink` on RxSwift relay
let combineSubject = relay.toCombine()combineSubject.sink(receiveValue: { value in ... })
// Use `send(value:)` on RxSwift relay
combineSubject.send(1)
combineSubject.send(2)
combineSubject.send(3)
```### Use Combine in my RxSwift code
RxCombine provides several helpers and conversions to help you bridge Combine code and types into your existing RxSwift codebase.
* `Publisher`s have a `asObservable()` method, providing an `Observable` mirroring the underlying `Publisher`.
```swift
// A publisher publishing numbers from 0 to 100.
let publisher = AnyPublisher { subscriber in
(0...100).forEach { _ = subscriber.receive($0) }
subscriber.receive(completion: .finished)
}publisher
.asObservable() // Observable
.subscribe(onNext: { num in ... })
```* `PassthroughSubject` and `CurrentValueSubject` both have a `asAnyObserver()` method which returns a `AnyObserver`. Binding to it from your RxSwift code pushes the events to the underlying Combine Subject.
```swift
// Combine Subject
let subject = PassthroughSubject()// A publisher publishing numbers from 0 to 100.
let publisher = AnyPublisher { 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())
```## Future ideas
* ~~Add CI / Tests~~
* ~~Carthage Support~~
* Bridge SwiftUI with RxCocoa/RxSwift
* ~~Partial Backpressure support, perhaps?~~
* ... your ideas? :)## License
MIT, of course ;-) See the [LICENSE](LICENSE) file.
The Apple logo and the Combine framework are property of Apple Inc.