Skip to content
CombineX compatibility shims
Swift Ruby
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
CXCompatible.xcodeproj
Sources/CXCompatible
Tests
.gitignore
.travis.yml
CXCompatible.podspec
LICENSE
Package.swift
README.md
README.zh_cn.md

README.md

CXCompatible(中文)

travis release install platform license dicord

CXCompatible provides compatibility shims for CombineX. Using it, you can write code that is compatible with both Combine and CombineX, in other words, you can freely switch dependencies between Combine and CombineX without modifying the code base.

#if USE_COMBINE
import CXFoundation
#else
import CXCompatible
#endif

let task = Timer.cx.publish(every: 1, on: RunLoop.main, in: .default)
    .sink { date in
        // task
    }

Why do I need CXCompatible

You should already know CombineX, which is an open source implementation of Combine. One of its purposes is to provide an API that is completely consistent with Combine, but unfortunately this is somewhat impossible.

Combine directly extends some system types and provides them with a pub-sub interface, such as:

let nums = [0, 1, 2].publisher

let task = URLSession.share.dataTaskPublisher(for: endpoint)

CombineX tries to provide the same functionality, but it can't use the same API, such as:

extension Sequence {
    // Error: ambiguous use of ...
    public var publisher: Publishers.Sequence {
        // ...
    }
}

extension URLSession {
    // Swift doesn't allow you to redefine types of the same name, even in a different framework.
    public struct DataTaskPublisher: Publisher {
        // ...
    }
}

CombineX's solution is cx:

let nums = [1, 2, 3].cx.publisher

let task = URLSession.share.cx.dataTaskPublisher(for: endpoint)

Yes, this breaks the consistency of CombineX and Combine. To make this code compatible with Combine, CXCompatible implements an alternative implementation of all cx interfaces, but based on Combine.

Install

Swift Package Manager

dependencies.append(
    .package(url: "https://github.com/cx-org/CXCompatible", .branch("master"))
)

CocoaPods

pod 'CXCompatible', :git => 'https://github.com/cx-org/CXCompatible.git', :branch => 'master'

Carthage

github "cx-org/CXCompatible" "master"

Related

You can’t perform that action at this time.