RequestTaskMap.swift 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 tasksToRequests: [URLSessionTask: Request]
  28. private var requestsToTasks: [Request: URLSessionTask]
  29. private var taskEvents: [URLSessionTask: (completed: Bool, metricsGathered: Bool)]
  30. var requests: [Request] {
  31. return Array(tasksToRequests.values)
  32. }
  33. init(tasksToRequests: [URLSessionTask: Request] = [:],
  34. requestsToTasks: [Request: URLSessionTask] = [:],
  35. taskEvents: [URLSessionTask: (completed: Bool, metricsGathered: Bool)] = [:]) {
  36. self.tasksToRequests = tasksToRequests
  37. self.requestsToTasks = requestsToTasks
  38. self.taskEvents = taskEvents
  39. }
  40. subscript(_ request: Request) -> URLSessionTask? {
  41. get { return requestsToTasks[request] }
  42. set {
  43. guard let newValue = newValue else {
  44. guard let task = requestsToTasks[request] else {
  45. fatalError("RequestTaskMap consistency error: no task corresponding to request found.")
  46. }
  47. requestsToTasks.removeValue(forKey: request)
  48. tasksToRequests.removeValue(forKey: task)
  49. taskEvents.removeValue(forKey: task)
  50. return
  51. }
  52. requestsToTasks[request] = newValue
  53. tasksToRequests[newValue] = request
  54. taskEvents[newValue] = (completed: false, metricsGathered: false)
  55. }
  56. }
  57. subscript(_ task: URLSessionTask) -> Request? {
  58. get { return tasksToRequests[task] }
  59. set {
  60. guard let newValue = newValue else {
  61. guard let request = tasksToRequests[task] else {
  62. fatalError("RequestTaskMap consistency error: no request corresponding to task found.")
  63. }
  64. tasksToRequests.removeValue(forKey: task)
  65. requestsToTasks.removeValue(forKey: request)
  66. taskEvents.removeValue(forKey: task)
  67. return
  68. }
  69. tasksToRequests[task] = newValue
  70. requestsToTasks[newValue] = task
  71. taskEvents[task] = (completed: false, metricsGathered: false)
  72. }
  73. }
  74. var count: Int {
  75. precondition(tasksToRequests.count == requestsToTasks.count,
  76. "RequestTaskMap.count invalid, requests.count: \(tasksToRequests.count) != tasks.count: \(requestsToTasks.count)")
  77. return tasksToRequests.count
  78. }
  79. var eventCount: Int {
  80. precondition(taskEvents.count == count, "RequestTaskMap.eventCount invalid, count: \(count) != taskEvents.count: \(taskEvents.count)")
  81. return taskEvents.count
  82. }
  83. var isEmpty: Bool {
  84. precondition(tasksToRequests.isEmpty == requestsToTasks.isEmpty,
  85. "RequestTaskMap.isEmpty invalid, requests.isEmpty: \(tasksToRequests.isEmpty) != tasks.isEmpty: \(requestsToTasks.isEmpty)")
  86. return tasksToRequests.isEmpty
  87. }
  88. var isEventsEmpty: Bool {
  89. precondition(taskEvents.isEmpty == isEmpty, "RequestTaskMap.isEventsEmpty invalid, isEmpty: \(isEmpty) != taskEvents.isEmpty: \(taskEvents.isEmpty)")
  90. return taskEvents.isEmpty
  91. }
  92. mutating func disassociateIfNecessaryAfterGatheringMetricsForTask(_ task: URLSessionTask) {
  93. guard let events = taskEvents[task] else {
  94. fatalError("RequestTaskMap consistency error: no events corresponding to task found.")
  95. }
  96. switch (events.completed, events.metricsGathered) {
  97. case (_, true): fatalError("RequestTaskMap consistency error: duplicate metricsGatheredForTask call.")
  98. case (false, false): taskEvents[task] = (completed: false, metricsGathered: true)
  99. case (true, false): self[task] = nil
  100. }
  101. }
  102. mutating func disassociateIfNecessaryAfterCompletingTask(_ task: URLSessionTask) {
  103. guard let events = taskEvents[task] else {
  104. fatalError("RequestTaskMap consistency error: no events corresponding to task found.")
  105. }
  106. switch (events.completed, events.metricsGathered) {
  107. case (true, _): fatalError("RequestTaskMap consistency error: duplicate completionReceivedForTask call.")
  108. case (false, false): taskEvents[task] = (completed: true, metricsGathered: false)
  109. case (false, true): self[task] = nil
  110. }
  111. }
  112. }