From 7ccbf9d0e7f692d3f8b67b22216663a591c54e8c Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Wed, 1 Apr 2020 15:43:47 +0100 Subject: [PATCH] Move Combine code to separate CombineMoya target --- Package.swift | 4 +++- .../AnyPublisher+Response.swift | 1 + .../MoyaProvider+Combine.swift | 1 + .../Combine => CombineMoya}/MoyaPublisher.swift | 9 +++++---- Tests/MoyaTests/MoyaProvider+CombineSpec.swift | 17 +++++++++-------- 5 files changed, 19 insertions(+), 13 deletions(-) rename Sources/{Moya/Combine => CombineMoya}/AnyPublisher+Response.swift (99%) rename Sources/{Moya/Combine => CombineMoya}/MoyaProvider+Combine.swift (99%) rename Sources/{Moya/Combine => CombineMoya}/MoyaPublisher.swift (86%) diff --git a/Package.swift b/Package.swift index 3908ec522..62e574844 100644 --- a/Package.swift +++ b/Package.swift @@ -12,6 +12,7 @@ let package = Package( ], products: [ .library(name: "Moya", targets: ["Moya"]), + .library(name: "CombineMoya", targets: ["CombineMoya"]), .library(name: "ReactiveMoya", targets: ["ReactiveMoya"]), .library(name: "RxMoya", targets: ["RxMoya"]) ], @@ -26,9 +27,10 @@ let package = Package( ], targets: [ .target(name: "Moya", dependencies: ["Alamofire"]), + .target(name: "CombineMoya", dependencies: ["Moya"]), .target(name: "ReactiveMoya", dependencies: ["Moya", "ReactiveSwift"]), .target(name: "RxMoya", dependencies: ["Moya", "RxSwift"]), - .testTarget(name: "MoyaTests", dependencies: ["Moya", "RxMoya", "ReactiveMoya", "Quick", "Nimble", "OHHTTPStubsSwift"]) // dev + .testTarget(name: "MoyaTests", dependencies: ["Moya", "CombineMoya", "RxMoya", "ReactiveMoya", "Quick", "Nimble", "OHHTTPStubsSwift"]) // dev ] ) diff --git a/Sources/Moya/Combine/AnyPublisher+Response.swift b/Sources/CombineMoya/AnyPublisher+Response.swift similarity index 99% rename from Sources/Moya/Combine/AnyPublisher+Response.swift rename to Sources/CombineMoya/AnyPublisher+Response.swift index b10704fa1..aff8c9c08 100644 --- a/Sources/Moya/Combine/AnyPublisher+Response.swift +++ b/Sources/CombineMoya/AnyPublisher+Response.swift @@ -2,6 +2,7 @@ import Foundation import Combine +import Moya #if canImport(UIKit) import UIKit.UIImage diff --git a/Sources/Moya/Combine/MoyaProvider+Combine.swift b/Sources/CombineMoya/MoyaProvider+Combine.swift similarity index 99% rename from Sources/Moya/Combine/MoyaProvider+Combine.swift rename to Sources/CombineMoya/MoyaProvider+Combine.swift index 0b66bba35..6c27f2cbe 100644 --- a/Sources/Moya/Combine/MoyaProvider+Combine.swift +++ b/Sources/CombineMoya/MoyaProvider+Combine.swift @@ -2,6 +2,7 @@ import Foundation import Combine +import Moya @available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) public extension MoyaProvider { diff --git a/Sources/Moya/Combine/MoyaPublisher.swift b/Sources/CombineMoya/MoyaPublisher.swift similarity index 86% rename from Sources/Moya/Combine/MoyaPublisher.swift rename to Sources/CombineMoya/MoyaPublisher.swift index 139d477a1..b45b278d8 100644 --- a/Sources/Moya/Combine/MoyaPublisher.swift +++ b/Sources/CombineMoya/MoyaPublisher.swift @@ -1,6 +1,7 @@ #if canImport(Combine) import Combine +import Moya // This should be already provided in Combine, but it's not. // Ideally we would like to remove it, in favor of a framework-provided solution, ASAP. @@ -12,9 +13,9 @@ internal class MoyaPublisher: Publisher { private class Subscription: Combine.Subscription { - private let cancellable: Cancellable? + private let cancellable: Moya.Cancellable? - init(subscriber: AnySubscriber, callback: @escaping (AnySubscriber) -> Cancellable?) { + init(subscriber: AnySubscriber, callback: @escaping (AnySubscriber) -> Moya.Cancellable?) { self.cancellable = callback(subscriber) } @@ -27,9 +28,9 @@ internal class MoyaPublisher: Publisher { } } - private let callback: (AnySubscriber) -> Cancellable? + private let callback: (AnySubscriber) -> Moya.Cancellable? - init(callback: @escaping (AnySubscriber) -> Cancellable?) { + init(callback: @escaping (AnySubscriber) -> Moya.Cancellable?) { self.callback = callback } diff --git a/Tests/MoyaTests/MoyaProvider+CombineSpec.swift b/Tests/MoyaTests/MoyaProvider+CombineSpec.swift index cb630e956..71945a5a4 100644 --- a/Tests/MoyaTests/MoyaProvider+CombineSpec.swift +++ b/Tests/MoyaTests/MoyaProvider+CombineSpec.swift @@ -2,6 +2,7 @@ import Quick import Nimble import Combine +import CombineMoya #if canImport(OHHTTPStubs) import OHHTTPStubs @@ -135,8 +136,8 @@ final class MoyaProviderCombineSpec: QuickSpec { var provider: MoyaProvider! beforeEach { - OHHTTPStubs.stubRequests(passingTest: {$0.url!.path == "/zen"}, withStubResponse: { _ in - return OHHTTPStubsResponse(data: GitHub.zen.sampleData, statusCode: 200, headers: nil) + HTTPStubs.stubRequests(passingTest: {$0.url!.path == "/zen"}, withStubResponse: { _ in + return HTTPStubsResponse(data: GitHub.zen.sampleData, statusCode: 200, headers: nil) }) provider = MoyaProvider(trackInflights: true) } @@ -196,8 +197,8 @@ final class MoyaProviderCombineSpec: QuickSpec { try? FileManager.default.removeItem(at: file) //`responseTime(-4)` equals to 1000 bytes at a time. The sample data is 4000 bytes. - OHHTTPStubs.stubRequests(passingTest: {$0.url!.path.hasSuffix("logo_github.png")}, withStubResponse: { _ in - return OHHTTPStubsResponse(data: GitHubUserContent.downloadMoyaWebContent("logo_github.png").sampleData, statusCode: 200, headers: nil).responseTime(-4) + HTTPStubs.stubRequests(passingTest: {$0.url!.path.hasSuffix("logo_github.png")}, withStubResponse: { _ in + return HTTPStubsResponse(data: GitHubUserContent.downloadMoyaWebContent("logo_github.png").sampleData, statusCode: 200, headers: nil).responseTime(-4) }) provider = MoyaProvider() } @@ -240,16 +241,16 @@ final class MoyaProviderCombineSpec: QuickSpec { } describe("a custom callback queue") { - var stubDescriptor: OHHTTPStubsDescriptor! + var stubDescriptor: HTTPStubsDescriptor! beforeEach { - stubDescriptor = OHHTTPStubs.stubRequests(passingTest: {$0.url!.path == "/zen"}, withStubResponse: { _ in - return OHHTTPStubsResponse(data: GitHub.zen.sampleData, statusCode: 200, headers: nil) + stubDescriptor = HTTPStubs.stubRequests(passingTest: {$0.url!.path == "/zen"}, withStubResponse: { _ in + return HTTPStubsResponse(data: GitHub.zen.sampleData, statusCode: 200, headers: nil) }) } afterEach { - OHHTTPStubs.removeStub(stubDescriptor) + HTTPStubs.removeStub(stubDescriptor) } describe("a provider with a predefined callback queue") {