From 4f0fad4bba768413cc86dd349240e9e46310915e Mon Sep 17 00:00:00 2001 From: Doug Date: Wed, 18 Jul 2018 17:44:51 -0700 Subject: [PATCH 1/3] 'Cancel' for PromiseKit --- Cartfile | 2 + Cartfile.resolved | 3 +- .../project.pbxproj | 4 ++ Sources/SCNetworkReachability+Promise.swift | 50 +++++++++++++++---- Tests/SCTests.swift | 1 + 5 files changed, 48 insertions(+), 12 deletions(-) diff --git a/Cartfile b/Cartfile index 2bfea98..c2d2433 100644 --- a/Cartfile +++ b/Cartfile @@ -1 +1,3 @@ github "mxcl/PromiseKit" ~> 6.0 +#github "PromiseKit/Cancel" ~> 1.0 +github "dougzilla32/Cancel" ~> 1.0 diff --git a/Cartfile.resolved b/Cartfile.resolved index a1be206..6d512a1 100644 --- a/Cartfile.resolved +++ b/Cartfile.resolved @@ -1 +1,2 @@ -github "mxcl/PromiseKit" "6.3.3" +github "dougzilla32/Cancel" "1.0.0" +github "mxcl/PromiseKit" "6.3.4" diff --git a/PMKSystemConfiguration.xcodeproj/project.pbxproj b/PMKSystemConfiguration.xcodeproj/project.pbxproj index f59f77d..7dabf8d 100644 --- a/PMKSystemConfiguration.xcodeproj/project.pbxproj +++ b/PMKSystemConfiguration.xcodeproj/project.pbxproj @@ -12,6 +12,7 @@ 6362F84F1D5D934C0021D2DD /* SCNetworkReachability+AnyPromise.m in Sources */ = {isa = PBXBuildFile; fileRef = 6362F84D1D5D934C0021D2DD /* SCNetworkReachability+AnyPromise.m */; }; 6383E14B1D611E0D00897651 /* SCTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6383E14A1D611E0D00897651 /* SCTests.swift */; }; 63C7FFF71D5C020D003BAE60 /* PMKSystemConfiguration.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 63C7FFA71D5BEE09003BAE60 /* PMKSystemConfiguration.framework */; }; + 911E6FC820F578E2006F49B9 /* SCNetworkReachability+Promise.swift in Sources */ = {isa = PBXBuildFile; fileRef = 911E6FC720F578E2006F49B9 /* SCNetworkReachability+Promise.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -34,6 +35,7 @@ 63C7FFF21D5C020D003BAE60 /* PMKSCTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = PMKSCTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 63CCF8121D5C0C4E00503216 /* Cartfile */ = {isa = PBXFileReference; lastKnownFileType = text; path = Cartfile; sourceTree = ""; }; 63CCF8171D5C11B500503216 /* Carthage.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = Carthage.xcconfig; sourceTree = ""; }; + 911E6FC720F578E2006F49B9 /* SCNetworkReachability+Promise.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "SCNetworkReachability+Promise.swift"; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -82,6 +84,7 @@ 6358AB791D5D4B6700B9B157 /* PMKSystemConfiguration.h */, 6362F84C1D5D934C0021D2DD /* SCNetworkReachability+AnyPromise.h */, 6362F84D1D5D934C0021D2DD /* SCNetworkReachability+AnyPromise.m */, + 911E6FC720F578E2006F49B9 /* SCNetworkReachability+Promise.swift */, ); path = Sources; sourceTree = ""; @@ -208,6 +211,7 @@ buildActionMask = 2147483647; files = ( 6362F84F1D5D934C0021D2DD /* SCNetworkReachability+AnyPromise.m in Sources */, + 911E6FC820F578E2006F49B9 /* SCNetworkReachability+Promise.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Sources/SCNetworkReachability+Promise.swift b/Sources/SCNetworkReachability+Promise.swift index d2d8291..f7f304e 100644 --- a/Sources/SCNetworkReachability+Promise.swift +++ b/Sources/SCNetworkReachability+Promise.swift @@ -1,5 +1,8 @@ import SystemConfiguration +#if !PMKCocoaPods +import PMKCancel import PromiseKit +#endif public extension SCNetworkReachability { @@ -9,23 +12,31 @@ public extension SCNetworkReachability { static func promise() -> Promise { do { - var zeroAddress = sockaddr() - zeroAddress.sa_len = UInt8(MemoryLayout.size) - zeroAddress.sa_family = sa_family_t(AF_INET) - guard let ref = SCNetworkReachabilityCreateWithAddress(nil, &zeroAddress) else { - throw PMKError.couldNotInitializeReachability - } - - var flags = SCNetworkReachabilityFlags() - if SCNetworkReachabilityGetFlags(ref, &flags), flags.contains(.reachable) { + if let promise = try pending()?.promise { + return promise + } else { return Promise() } - - return try Helper(ref: ref).pending.promise } catch { return Promise(error: error) } } + + fileprivate static func pending() throws -> (promise: Promise, resolver: Resolver)? { + var zeroAddress = sockaddr() + zeroAddress.sa_len = UInt8(MemoryLayout.size) + zeroAddress.sa_family = sa_family_t(AF_INET) + guard let ref = SCNetworkReachabilityCreateWithAddress(nil, &zeroAddress) else { + throw PMKError.couldNotInitializeReachability + } + + var flags = SCNetworkReachabilityFlags() + if SCNetworkReachabilityGetFlags(ref, &flags), flags.contains(.reachable) { + return nil + } + + return try Helper(ref: ref).pending + } } private func callback(reachability: SCNetworkReachability, flags: SCNetworkReachabilityFlags, info: UnsafeMutableRawPointer?) { @@ -58,3 +69,20 @@ private class Helper { } } } + +//////////////////////////////////////////////////////////// Cancellation + +public extension SCNetworkReachability { + static func promiseCC() -> CancellablePromise { + do { + if let pending = try pending() { + return CancellablePromise(promise: pending.promise, resolver: pending.resolver) + } else { + return CancellablePromise() + } + } catch { + return CancellablePromise(error: error) + } + } +} + diff --git a/Tests/SCTests.swift b/Tests/SCTests.swift index 9494601..27ab0e2 100644 --- a/Tests/SCTests.swift +++ b/Tests/SCTests.swift @@ -1,3 +1,4 @@ +import PMKCancel import PMKSystemConfiguration import XCTest From f8630ac78abfc52b8f3c8262cc98d66c0d0ea93c Mon Sep 17 00:00:00 2001 From: Doug Date: Wed, 18 Jul 2018 23:11:34 -0700 Subject: [PATCH 2/3] 'Cancel' for PromiseKit -- + for PMKCancel, change watchOS deployment target from 3.0 to 2.0 + include PMKCancel in the 'Embed Carthage Frameworks' build phase script for all extensions + include PMKCancel in SPM config for Alamofire and Foundation --- PMKSystemConfiguration.xcodeproj/project.pbxproj | 1 + 1 file changed, 1 insertion(+) diff --git a/PMKSystemConfiguration.xcodeproj/project.pbxproj b/PMKSystemConfiguration.xcodeproj/project.pbxproj index 7dabf8d..dddca4f 100644 --- a/PMKSystemConfiguration.xcodeproj/project.pbxproj +++ b/PMKSystemConfiguration.xcodeproj/project.pbxproj @@ -195,6 +195,7 @@ ); inputPaths = ( PromiseKit, + PMKCancel, ); name = "Embed Carthage Frameworks"; outputPaths = ( From d1c0d48166c75787c80654da4f89f5a29b6658b1 Mon Sep 17 00:00:00 2001 From: dougzilla Date: Thu, 19 Jul 2018 15:07:37 -0700 Subject: [PATCH 3/3] 'Cancel' for PromiseKit -- for all Extensions point Cartfile at the forked version of PromiseKit --- Cartfile | 3 ++- Cartfile.resolved | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Cartfile b/Cartfile index c2d2433..cdd4145 100644 --- a/Cartfile +++ b/Cartfile @@ -1,3 +1,4 @@ -github "mxcl/PromiseKit" ~> 6.0 +#github "mxcl/PromiseKit" ~> 6.0 +github "dougzilla32/PromiseKit" "PMKCancel" #github "PromiseKit/Cancel" ~> 1.0 github "dougzilla32/Cancel" ~> 1.0 diff --git a/Cartfile.resolved b/Cartfile.resolved index 6d512a1..e1cadd5 100644 --- a/Cartfile.resolved +++ b/Cartfile.resolved @@ -1,2 +1,2 @@ github "dougzilla32/Cancel" "1.0.0" -github "mxcl/PromiseKit" "6.3.4" +github "dougzilla32/PromiseKit" "a0217bd7b69af68237dcdeee0197e63259b9d445"