Git Product home page Git Product logo

combinerx's Introduction

CombineRx

Run Tests License codecov

The CombineRx library contains a series of functions that help with the interoperability between RxSwift and Apple's Combine frameworks.

Interoperability between Combine and RxSwift

Combine to RxSwift

In order to convert Combine Publishers to RxSwift Observables, you can make use of the asObservable() function. This can be done as follows:

import Combine
import RxSwift
import CombineRx

let myBridgedObservable = Just(0).asObservable()

RxSwift to Combine

In order to convert RxSwift Observables to Combine Publishers, you can make use of the asPublisher(withBufferSize:andBridgeBufferingStrategy:) function. This can be done as follows:

import Combine
import RxSwift
import CombineRx

let myBridgedPublisher1 = Observable.just(0).asPublisher(withBufferSize: 1, andBridgeBufferingStrategy: .error)
let myBridgedPublisher2 = Observable.from([0, 1, 2, 3]).asPublisher(withBufferSize: 4, andBridgeBufferingStrategy: .error)

One difference between RxSwift and Combine is that Combine adheres to the mechanism of backpressure in order to ensure that Publishers only produce as many elements that Subscribers have requested. This prevents the case where elements might build up in a Publishers buffer faster than they can be processed downstream by a subscriber as this could lead to out-of-memory errors and degradation in performance due to high system resource consumption. Combine applies this backpressure upstream through a contractual obligation by Publishers to only emit an element when it is requested by Subscribers through Subscribers.Demand requests.

RxSwift Observables differ in this regard as they rely on a source with an unbounded rate of production and therefore when bridging to a Combine Publisher, we must maintain a buffer or drop elements accordingly in order to satisfy the requirements of downstream subscribers.

This is the reason for the required withBufferSize and andBridgeBufferingStrategy parameters for asPublisher(withBufferSize:andBridgeBufferingStrategy:). withBufferSize is where the buffer size should manually be set (ideally based directly on the number of expected elements in the sequence). andBridgeBufferingStrategy is the strategy to employ when the maximum buffer capacity is reached. Keeping in line with native Combine strategies, this can either be error, where any buffer overflow is treated as an error, dropNewest where the elements already present in the buffer are maintained and any new elements are ignored, or finally dropOldest where new elements are added to the buffer and replace older elements that were already present.

Additional information on Combine's use of backpressure can be found here.

Installation

It is currently possible to install this library using Swift Package Manager. In order to do so, please add the current repository as a package dependency using Xcode or include the following in your Package.swift file:

import PackageDescription

let package = Package(
    ...
    dependencies: [
        .package(url: "https://github.com/Jackstone92/CombineRx", .upToNextMajor(from: "0.1.0")),
    ],
    ...
    targets: [
        .target(name: "MyTarget", dependencies: ["CombineRx"]),
    ]
)

Copywrite and license information

Copyright 2020 © Jack Stone

CombineRx is made available under the MIT License

combinerx's People

Contributors

jackstone92 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

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