Created
May 15, 2018 13:02
-
-
Save poksi592/5770cb1c2265569ccd0e32b88bd6a63d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@objc class URLRouter: URLProtocol, URLSessionDataDelegate, URLSessionTaskDelegate { | |
// TODO: This is synchronisyng only write access, which might be inadequate in many cases | |
// Need to be replaced with proper full generic implementation of synchronized collection | |
private (set) var moduleQueue = DispatchQueue(label: "com.tandem.module.queue") | |
// MARK: URLProtocol methods overriding | |
override class func canInit(with task: URLSessionTask) -> Bool { | |
// Check if there's internal app schema that matches the one in the URL | |
guard let url = task.originalRequest?.url, | |
url.containsInAppSchema() else { | |
return false | |
} | |
// Check if there's a path in the module that matches the one in the URL | |
guard let module = ApplicationRouter.shared.instantiatedModules.filter({ $0.route == task.originalRequest?.url?.host }).first, | |
let _ = module.paths.filter({ $0 == task.originalRequest?.url?.path }).first else { | |
return false | |
} | |
return true | |
} | |
override class func canonicalRequest(for request: URLRequest) -> URLRequest { | |
return request | |
} | |
override func startLoading() { | |
guard let url = request.url else { | |
return | |
} | |
ApplicationRouter.shared.open(url: url) { (response, data, urlResponse, error) in | |
// TODO: Calling URLSessionDataDelegate methods to return the response | |
} | |
} | |
override func stopLoading() { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment