Guest User

Untitled

a guest
May 20th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. @objc class URLRouter: URLProtocol, URLSessionDataDelegate, URLSessionTaskDelegate {
  2.  
  3. // TODO: This is synchronisyng only write access, which might be inadequate in many cases
  4. // Need to be replaced with proper full generic implementation of synchronized collection
  5. private (set) var moduleQueue = DispatchQueue(label: "com.tandem.module.queue")
  6.  
  7. // MARK: URLProtocol methods overriding
  8.  
  9. override class func canInit(with task: URLSessionTask) -> Bool {
  10.  
  11. // Check if there's internal app schema that matches the one in the URL
  12. guard let url = task.originalRequest?.url,
  13. url.containsInAppSchema() else {
  14. return false
  15. }
  16.  
  17. // Check if there's a path in the module that matches the one in the URL
  18. guard let module = ApplicationRouter.shared.instantiatedModules.filter({ $0.route == task.originalRequest?.url?.host }).first,
  19. let _ = module.paths.filter({ $0 == task.originalRequest?.url?.path }).first else {
  20. return false
  21. }
  22. return true
  23. }
  24.  
  25. override class func canonicalRequest(for request: URLRequest) -> URLRequest {
  26. return request
  27. }
  28.  
  29.  
  30. override func startLoading() {
  31.  
  32. guard let url = request.url else {
  33. return
  34. }
  35.  
  36. ApplicationRouter.shared.open(url: url) { (response, data, urlResponse, error) in
  37.  
  38. // TODO: Calling URLSessionDataDelegate methods to return the response
  39. }
  40. }
  41.  
  42. override func stopLoading() {
  43. }
  44. }
Add Comment
Please, Sign In to add comment