Skip to content

Commit

Permalink
Merge pull request cx-org#97 from cx-org/xcode12
Browse files Browse the repository at this point in the history
Implement remaining new interface from Xcode12
  • Loading branch information
ddddxxx authored Nov 18, 2020
2 parents e54a462 + 633659b commit 480e577
Show file tree
Hide file tree
Showing 10 changed files with 422 additions and 96 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ jobs:
run: |
swift -version
swift package --version
xcodebuild -version
- name: Build and Test
run: swift test

Expand Down Expand Up @@ -76,6 +77,7 @@ jobs:
- name: Swift Version
run: |
sw_vers -productVersion
xcodebuild -version
swift -version
swift package --version
- name: Build and Test
Expand Down
4 changes: 2 additions & 2 deletions [email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ let shimTarget = package.targets.first(where: { $0.name == "CXShim" })!
shimTarget.dependencies = combineImp.shimTargetDependencies
shimTarget.swiftSettings.append(contentsOf: combineImp.swiftSettings)

let testUtilityTarget = package.targets.first(where: { $0.name == "CXTestUtility" })!
testUtilityTarget.swiftSettings.append(contentsOf: combineImp.swiftSettings)
let testTargets = package.targets.filter { $0.name == "CXTestUtility" || $0.isTest }
testTargets.forEach { $0.swiftSettings.append(contentsOf: combineImp.swiftSettings) }

if combineImp == .combine && isCI {
package.platforms = [.macOS("10.15"), .iOS("13.0"), .tvOS("13.0"), .watchOS("6.0")]
Expand Down
33 changes: 0 additions & 33 deletions Sources/CXCompatible/Polyfill/FlatMap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,6 @@ import Combine
@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
extension Publisher {

/// Transforms all elements from an upstream publisher into a new publisher
/// up to a maximum number of publishers you specify.
///
/// - Parameters:
/// - maxPublishers: Specifies the maximum number of concurrent publisher
/// subscriptions, or ``Combine/Subscribers/Demand/unlimited`` if
/// unspecified.
/// - transform: A closure that takes an element as a parameter and
/// returns a publisher that produces elements of that type.
/// - Returns: A publisher that transforms elements from an upstream
/// publisher into a publisher of that element’s type.
@available(macOS, obsoleted: 11.0)
@available(iOS, obsoleted: 14.0)
@available(tvOS, obsoleted: 14.0)
Expand All @@ -33,17 +22,6 @@ extension Publisher {
@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
extension Publisher where Failure == Never {

/// Transforms all elements from an upstream publisher into a new publisher
/// up to a maximum number of publishers you specify.
///
/// - Parameters:
/// - maxPublishers: Specifies the maximum number of concurrent publisher
/// subscriptions, or ``Combine/Subscribers/Demand/unlimited`` if
/// unspecified.
/// - transform: A closure that takes an element as a parameter and
/// returns a publisher that produces elements of that type.
/// - Returns: A publisher that transforms elements from an upstream
/// publisher into a publisher of that element’s type.
@available(macOS, obsoleted: 11.0)
@available(iOS, obsoleted: 14.0)
@available(tvOS, obsoleted: 14.0)
Expand All @@ -56,17 +34,6 @@ extension Publisher where Failure == Never {
.flatMap(maxPublishers: maxPublishers, transform)
}

/// Transforms all elements from an upstream publisher into a new publisher
/// up to a maximum number of publishers you specify.
///
/// - Parameters:
/// - maxPublishers: Specifies the maximum number of concurrent publisher
/// subscriptions, or ``Combine/Subscribers/Demand/unlimited`` if
/// unspecified.
/// - transform: A closure that takes an element as a parameter and
/// returns a publisher that produces elements of that type.
/// - Returns: A publisher that transforms elements from an upstream
/// publisher into a publisher of that element’s type.
@available(macOS, obsoleted: 11.0)
@available(iOS, obsoleted: 14.0)
@available(tvOS, obsoleted: 14.0)
Expand Down
43 changes: 43 additions & 0 deletions Sources/CXCompatible/Polyfill/SwitchToLatest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#if canImport(Combine)

import Combine

@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
extension Publisher where Output: Publisher, Output.Failure == Never {

@available(macOS, obsoleted: 11.0)
@available(iOS, obsoleted: 14.0)
@available(tvOS, obsoleted: 14.0)
@available(watchOS, obsoleted: 7.0)
public func switchToLatest() -> Publishers.SwitchToLatest<Publishers.SetFailureType<Output, Failure>, Publishers.Map<Self, Publishers.SetFailureType<Output, Failure>>> {
return map { $0.setFailureType(to: Failure.self) }
.switchToLatest()
}
}

@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
extension Publisher where Failure == Never, Output: Publisher {

@available(macOS, obsoleted: 11.0)
@available(iOS, obsoleted: 14.0)
@available(tvOS, obsoleted: 14.0)
@available(watchOS, obsoleted: 7.0)
public func switchToLatest() -> Publishers.SwitchToLatest<Output, Publishers.SetFailureType<Self, Output.Failure>> {
return setFailureType(to: Output.Failure.self)
.switchToLatest()
}
}

@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
extension Publisher where Failure == Never, Output: Publisher, Output.Failure == Never {

@available(macOS, obsoleted: 11.0)
@available(iOS, obsoleted: 14.0)
@available(tvOS, obsoleted: 14.0)
@available(watchOS, obsoleted: 7.0)
public func switchToLatest() -> Publishers.SwitchToLatest<Output, Self> {
return .init(upstream: self)
}
}

#endif
4 changes: 2 additions & 2 deletions Sources/CombineX/Internal/Runtime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ struct PublishedFieldsEnumerator: Sequence {

struct Iterator: IteratorProtocol {

typealias Element = (storage: UnsafeMutableRawPointer, type: _PublishedProtocol.Type)
typealias Element = (storage: UnsafeMutableRawPointer, type: _ObservableObjectProperty.Type)

private let object: UnsafeMutableRawPointer
private var inheritanceIterator: InheritanceSequence.Iterator
Expand All @@ -19,7 +19,7 @@ struct PublishedFieldsEnumerator: Sequence {

mutating func next() -> Element? {
while let (offset, type) = nextField() {
if let pType = type as? _PublishedProtocol.Type {
if let pType = type as? _ObservableObjectProperty.Type {
let storage = object.advanced(by: offset)
return (storage, pType)
}
Expand Down
27 changes: 25 additions & 2 deletions Sources/CombineX/ObserableObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ public protocol ObservableObject: AnyObject {
var objectWillChange: ObjectWillChangePublisher { get }
}

private let globalObjectWillChangeCache = ObservableObjectPublisherCache<AnyObject, ObservableObjectPublisher>()

extension ObservableObject where ObjectWillChangePublisher == ObservableObjectPublisher {

public var objectWillChange: ObservableObjectPublisher {
Expand Down Expand Up @@ -83,3 +81,28 @@ public final class ObservableObjectPublisher: Publisher {
self.subject.send()
}
}

// MARK: - Helpers

private let globalObjectWillChangeCache = ObservableObjectPublisherCache<AnyObject, ObservableObjectPublisher>()

protocol _ObservableObjectProperty {
var objectWillChange: ObservableObjectPublisher? { get set }
}

private extension _ObservableObjectProperty {

static func getPublisher(for ptr: UnsafeMutableRawPointer) -> ObservableObjectPublisher? {
return ptr.assumingMemoryBound(to: Self.self)
.pointee
.objectWillChange
}

static func setPublisher(_ publisher: ObservableObjectPublisher, on ptr: UnsafeMutableRawPointer) {
ptr.assumingMemoryBound(to: Self.self)
.pointee
.objectWillChange = publisher
}
}

extension Published: _ObservableObjectProperty {}
Loading

0 comments on commit 480e577

Please sign in to comment.