RequestTaskMap.swift 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //
  2. // RequestTaskMap.swift
  3. //
  4. // Copyright (c) 2014-2018 Alamofire Software Foundation (http://alamofire.org/)
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. //
  24. import Foundation
  25. /// A type that maintains a two way, one to one map of `URLSessionTask`s to `Request`s.
  26. struct RequestTaskMap {
  27. private var requests: [URLSessionTask: Request]
  28. private var tasks: [Request: URLSessionTask]
  29. init(requests: [URLSessionTask: Request] = [:], tasks: [Request: URLSessionTask] = [:]) {
  30. self.requests = requests
  31. self.tasks = tasks
  32. }
  33. subscript(_ request: Request) -> URLSessionTask? {
  34. get { return tasks[request] }
  35. set {
  36. guard let newValue = newValue else {
  37. guard let task = tasks[request] else {
  38. fatalError("RequestTaskMap consistency error: no task corresponding to request found.")
  39. }
  40. tasks.removeValue(forKey: request)
  41. requests.removeValue(forKey: task)
  42. return
  43. }
  44. tasks[request] = newValue
  45. requests[newValue] = request
  46. }
  47. }
  48. subscript(_ task: URLSessionTask) -> Request? {
  49. get { return requests[task] }
  50. set {
  51. guard let newValue = newValue else {
  52. guard let request = requests[task] else {
  53. fatalError("RequestTaskMap consistency error: no request corresponding to task found.")
  54. }
  55. requests.removeValue(forKey: task)
  56. tasks.removeValue(forKey: request)
  57. return
  58. }
  59. requests[task] = newValue
  60. tasks[newValue] = task
  61. }
  62. }
  63. var count: Int {
  64. precondition(requests.count == tasks.count,
  65. "RequestTaskMap.count invalid, requests.count: \(requests.count) != tasks.count: \(tasks.count)")
  66. return requests.count
  67. }
  68. var isEmpty: Bool {
  69. precondition(requests.isEmpty == tasks.isEmpty,
  70. "RequestTaskMap.isEmpty invalid, requests.isEmpty: \(requests.isEmpty) != tasks.isEmpty: \(tasks.isEmpty)")
  71. return requests.isEmpty
  72. }
  73. }