RequestTaskMap.swift 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 on 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. // TODO: Investigate whether we could make stronger guarantees. Would likely need to abandon subscripts.
  34. subscript(_ request: Request) -> URLSessionTask? {
  35. get { return tasks[request] }
  36. set {
  37. guard let newValue = newValue else {
  38. guard let task = tasks[request] else {
  39. fatalError("RequestTaskMap consistency error: no task corresponding to request found.")
  40. }
  41. tasks.removeValue(forKey: request)
  42. requests.removeValue(forKey: task)
  43. return
  44. }
  45. tasks[request] = newValue
  46. requests[newValue] = request
  47. }
  48. }
  49. subscript(_ task: URLSessionTask) -> Request? {
  50. get { return requests[task] }
  51. set {
  52. guard let newValue = newValue else {
  53. guard let request = requests[task] else {
  54. fatalError("RequestTaskMap consistency error: no request corresponding to task found.")
  55. }
  56. requests.removeValue(forKey: task)
  57. tasks.removeValue(forKey: request)
  58. return
  59. }
  60. requests[task] = newValue
  61. tasks[newValue] = task
  62. }
  63. }
  64. var count: Int {
  65. precondition(requests.count == tasks.count,
  66. "RequestTaskMap.count invalid, requests.count: \(requests.count) != tasks.count: \(tasks.count)")
  67. return requests.count
  68. }
  69. var isEmpty: Bool {
  70. precondition(requests.isEmpty == tasks.isEmpty,
  71. "RequestTaskMap.isEmpty invalid, requests.isEmpty: \(requests.isEmpty) != tasks.isEmpty: \(tasks.isEmpty)")
  72. return requests.isEmpty
  73. }
  74. }