|
77 | 77 | ) async throws -> (Data, URLResponse) { |
78 | 78 | let helper = URLSessionTaskCancellationHelper() |
79 | 79 |
|
80 | | - return try await withTaskCancellationHandler(operation: { |
| 80 | + return try await withTaskCancellationHandler( |
| 81 | + operation: { |
| 82 | + try await withCheckedThrowingContinuation { continuation in |
| 83 | + let task = dataTask( |
| 84 | + with: request, |
| 85 | + completionHandler: { data, response, error in |
| 86 | + if let error { |
| 87 | + continuation.resume(throwing: error) |
| 88 | + } else if let data, let response { |
| 89 | + continuation.resume(returning: (data, response)) |
| 90 | + } else { |
| 91 | + continuation.resume(throwing: URLSessionPolyfillError.noDataNoErrorReturned) |
| 92 | + } |
| 93 | + }) |
| 94 | + |
| 95 | + helper.register(task) |
| 96 | + |
| 97 | + task.resume() |
| 98 | + } |
| 99 | + }, |
| 100 | + onCancel: { |
| 101 | + helper.cancel() |
| 102 | + }) |
| 103 | + } |
| 104 | + |
| 105 | + public func data( |
| 106 | + from url: URL, |
| 107 | + delegate _: (any URLSessionTaskDelegate)? = nil |
| 108 | + ) async throws -> (Data, URLResponse) { |
| 109 | + let helper = URLSessionTaskCancellationHelper() |
| 110 | + return try await withTaskCancellationHandler { |
81 | 111 | try await withCheckedThrowingContinuation { continuation in |
82 | | - let task = dataTask(with: request, completionHandler: { data, response, error in |
| 112 | + let task = dataTask(with: url) { data, response, error in |
83 | 113 | if let error { |
84 | 114 | continuation.resume(throwing: error) |
85 | 115 | } else if let data, let response { |
86 | 116 | continuation.resume(returning: (data, response)) |
87 | 117 | } else { |
88 | 118 | continuation.resume(throwing: URLSessionPolyfillError.noDataNoErrorReturned) |
89 | 119 | } |
90 | | - }) |
| 120 | + } |
91 | 121 |
|
92 | 122 | helper.register(task) |
93 | | - |
94 | 123 | task.resume() |
95 | 124 | } |
96 | | - }, onCancel: { |
| 125 | + } onCancel: { |
97 | 126 | helper.cancel() |
98 | | - }) |
| 127 | + } |
99 | 128 | } |
100 | 129 |
|
101 | 130 | public func upload( |
|
105 | 134 | ) async throws -> (Data, URLResponse) { |
106 | 135 | let helper = URLSessionTaskCancellationHelper() |
107 | 136 |
|
108 | | - return try await withTaskCancellationHandler(operation: { |
109 | | - try await withCheckedThrowingContinuation { continuation in |
110 | | - let task = uploadTask( |
111 | | - with: request, |
112 | | - from: bodyData, |
113 | | - completionHandler: { data, response, error in |
114 | | - if let error { |
115 | | - continuation.resume(throwing: error) |
116 | | - } else if let data, let response { |
117 | | - continuation.resume(returning: (data, response)) |
118 | | - } else { |
119 | | - continuation.resume(throwing: URLSessionPolyfillError.noDataNoErrorReturned) |
| 137 | + return try await withTaskCancellationHandler( |
| 138 | + operation: { |
| 139 | + try await withCheckedThrowingContinuation { continuation in |
| 140 | + let task = uploadTask( |
| 141 | + with: request, |
| 142 | + from: bodyData, |
| 143 | + completionHandler: { data, response, error in |
| 144 | + if let error { |
| 145 | + continuation.resume(throwing: error) |
| 146 | + } else if let data, let response { |
| 147 | + continuation.resume(returning: (data, response)) |
| 148 | + } else { |
| 149 | + continuation.resume(throwing: URLSessionPolyfillError.noDataNoErrorReturned) |
| 150 | + } |
120 | 151 | } |
121 | | - } |
122 | | - ) |
| 152 | + ) |
123 | 153 |
|
124 | | - helper.register(task) |
| 154 | + helper.register(task) |
125 | 155 |
|
126 | | - task.resume() |
127 | | - } |
128 | | - }, onCancel: { |
129 | | - helper.cancel() |
130 | | - }) |
| 156 | + task.resume() |
| 157 | + } |
| 158 | + }, |
| 159 | + onCancel: { |
| 160 | + helper.cancel() |
| 161 | + }) |
131 | 162 | } |
132 | 163 | } |
133 | 164 |
|
|
0 commit comments