Code Monkey home page Code Monkey logo

rxcombine's Introduction

RxCombine



Build Status Code Coverage for RxCombine on codecov
RxCombine supports CocoaPods RxCombine supports Swift Package Manager (SPM) RxCombine supports Carthage

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 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:

pod 'RxCombine'

Swift Package Manager

Add the following dependency to your Package.swift file:

.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 (or on GitHub).

  • Observable (and other ObservableConvertibleTypes) have a publisher property which returns a AnyPublisher<Element, Swift.Error> mirroring the underlying Observable.
let observable = Observable.just("Hello, Combine!")

observable
    .publisher // AnyPublisher<String, Swift.Error>
    .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.
let relay = BehaviorRelay<Int>(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.

  • Publishers have a asObservable() method, providing an Observable<Output> mirroring the underlying Publisher.
// 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 and CurrentValueSubject both have a asAnyObserver() method which returns a AnyObserver<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())

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 file.

The Apple logo and the Combine framework are property of Apple Inc.

rxcombine's People

Contributors

freak4pc avatar fredyshox avatar lucianopalmeida avatar masters3d avatar epatey avatar schlossm avatar ttozzi avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.