Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Composite delegation #13

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import Foundation

extension URLSessionConfiguration {

@objc func fakeProcotolClasses() -> [AnyClass]? {
guard let fakeProcotolClasses = self.fakeProcotolClasses() else {
@objc func fakeProtocolClasses() -> [AnyClass]? {
guard let fakeProtocolClasses = self.fakeProtocolClasses() else {
return []
}
var originalProtocolClasses = fakeProcotolClasses.filter {
var originalProtocolClasses = fakeProtocolClasses.filter {
return $0 != NetworkInterceptorUrlProtocol.self && $0 != NetworkLoggerUrlProtocol.self && $0 != NetwrokListenerUrlProtocol.self
}
if NetShears.shared.loggerEnable {
Expand Down
25 changes: 0 additions & 25 deletions Sources/NetShears/NetworkInterceptor.swift

This file was deleted.

2 changes: 1 addition & 1 deletion Sources/NetShears/NetworkRequestInterceptor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import Foundation
let uRLSessionConfigurationClass: AnyClass = object_getClass(instance)!

let method1: Method = class_getInstanceMethod(uRLSessionConfigurationClass, #selector(getter: uRLSessionConfigurationClass.protocolClasses))!
let method2: Method = class_getInstanceMethod(URLSessionConfiguration.self, #selector(URLSessionConfiguration.fakeProcotolClasses))!
let method2: Method = class_getInstanceMethod(URLSessionConfiguration.self, #selector(URLSessionConfiguration.fakeProtocolClasses))!

method_exchangeImplementations(method1, method2)
}
Expand Down
18 changes: 9 additions & 9 deletions Sources/NetShears/Observable/RequestBroadcast.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,25 @@ public protocol RequestBroadcastDelegate: AnyObject {
public final class RequestBroadcast: RequestObserverProtocol {
static public let shared = RequestBroadcast()

var delegate = ThreadSafe<RequestBroadcastDelegate?>(nil)
private var delegates = ThreadSafe<NSHashTable<AnyObject>>(.weakObjects())

private init() {}

public func setDelegate(_ newDelegate: RequestBroadcastDelegate) {
delegate.atomically { delegate in
delegate = newDelegate
public func addDelegate(_ newDelegate: RequestBroadcastDelegate) {
delegates.atomically { delegates in
delegates.add(newDelegate)
}
}

public func removeDelegate() {
delegate.atomically { delegate in
delegate = nil
public func removeDelegate(_ delegateToRemove: RequestBroadcastDelegate) {
delegates.atomically { delegates in
delegates.remove(delegateToRemove)
}
}

func newRequestArrived(_ request: NetShearsRequestModel) {
delegate.atomically { delegate in
delegate?.newRequestArrived(request)
delegates.atomically { delegates in
delegates.allObjects.reversed().forEach { ($0 as! RequestBroadcastDelegate).newRequestArrived(request) }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ class NetworkInterceptorUrlProtocol: URLProtocol {
}

override class func canInit(with request: URLRequest) -> Bool {
guard NetworkInterceptor.shared.shouldRequestModify(urlRequest: request) else { return false }
let shouldRequestModify: (URLRequest) -> Bool = { urlRequest in
for modifer in NetShears.shared.config.modifiers {
if modifer.isActionAllowed(urlRequest: urlRequest) { return true }
}
return false
}
guard shouldRequestModify(request) else { return false }

if NetworkInterceptorUrlProtocol.property(forKey: Constants.RequestHandledKey, in: request) != nil {
return false
Expand All @@ -36,7 +42,7 @@ class NetworkInterceptorUrlProtocol: URLProtocol {

open override class func canonicalRequest(for request: URLRequest) -> URLRequest {
let mutableRequest: NSMutableURLRequest = (request as NSURLRequest).mutableCopy() as! NSMutableURLRequest
URLProtocol.setProperty("YES", forKey: "NetworkInterceptorUrlProtocol", in: mutableRequest)
URLProtocol.setProperty(true, forKey: Constants.RequestHandledKey, in: mutableRequest)
return mutableRequest.copy() as! URLRequest
}

Expand Down