Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @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() {
- }
- }
Add Comment
Please, Sign In to add comment