Skip to content

Commit a2c5c7a

Browse files
committed
fixed notification sending
Signed-off-by: Quickthyme <[email protected]>
1 parent 3445d15 commit a2c5c7a

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

Sources/WebRequest/Manager/WebRequestManager.swift

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ public class WebRequestManager: WebRequestManaging {
2727

2828
internal var requests: Set<Wrapper> = Set<Wrapper>()
2929

30+
private var readyRequests: Set<Wrapper> {
31+
return self.requests.filter { $0.state == .ready || $0.state == .unauthorized }
32+
}
33+
3034
public var timeoutInterval: TimeInterval = 60.0
3135

3236
public var lastRefresh: TimeInterval = 0.0
@@ -126,15 +130,10 @@ private extension WebRequestManager {
126130
func begin() {
127131
guard (!isRefreshing) else { return }
128132
accessQueue.sync {
129-
let readyRequests = self.requests
130-
.filter { $0.state == .ready || $0.state == .unauthorized }
133+
let readyRequests = self.readyRequests
131134

132135
guard let session = self.sessionProvider?.current else {
133-
if let anyRequest = readyRequests.first?.originalRequest {
134-
self.fail(request: anyRequest, withStatus: 401)
135-
}
136-
for wrapper in readyRequests { wrapper.state = .cancelled }
137-
notificationCenter.post(UnauthorizedResponseNotification)
136+
self.failUnauthorized()
138137
return
139138
}
140139

@@ -160,11 +159,13 @@ private extension WebRequestManager {
160159
return
161160
}
162161

163-
try wrapper.originalRequest.completion?(actualResult, wrapper.originalRequest)
164-
165162
if wrapper.state == .unauthorized {
166-
notificationCenter.post(UnauthorizedResponseNotification)
163+
DispatchQueue.main.async {
164+
self.notificationCenter.post(self.UnauthorizedResponseNotification)
165+
}
167166
}
167+
168+
try wrapper.originalRequest.completion?(actualResult, wrapper.originalRequest)
168169
}
169170

170171
func shouldRefresh(since timestamp: TimeInterval) -> Bool {
@@ -173,7 +174,9 @@ private extension WebRequestManager {
173174

174175
func performRefresh() {
175176
isRefreshing = true
176-
sessionProvider.refresh()
177+
execQueue.async {
178+
self.sessionProvider.refresh()
179+
}
177180
}
178181

179182
func remove(_ wrapper: Wrapper) {
@@ -187,6 +190,15 @@ private extension WebRequestManager {
187190
func fail(request: WebRequest, withStatus status: Int) {
188191
try? request.completion?(WebRequest.Result(status: status), request)
189192
}
193+
194+
func failUnauthorized() {
195+
let readyRequests = self.readyRequests
196+
if let anyRequest = readyRequests.first {
197+
self.fail(request: anyRequest.originalRequest, withStatus: 401)
198+
}
199+
for wrapper in readyRequests { wrapper.state = .cancelled }
200+
notificationCenter.post(UnauthorizedResponseNotification)
201+
}
190202
}
191203

192204
extension WebRequestManager: WebRequestSessionProvidingDelegate {

Tests/WebRequestTests/Manager/WebRequestManagerTests.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,16 @@ class WebRequestManagerTests: XCTestCase {
152152
XCTAssertTrue(mockDelivery.didCall_deliver)
153153
XCTAssertEqual(mockSessionProvider.timesCalled_refresh, 1)
154154
XCTAssert(receivedResult!.status == StatusCode.unauthorized.rawValue)
155+
156+
wait(for: 0.1)
157+
155158
XCTAssertTrue(mockNotificationCenter.didPost)
156159
XCTAssertEqual(mockNotificationCenter.postedNotificationName, WebRequestUnauthorizedResponseNotification)
157160
}
161+
162+
func wait(for timeout: TimeInterval) {
163+
let expectMainThread = expectation(description: "auto timeout")
164+
DispatchQueue.main.asyncAfter(deadline: .now() + timeout) { expectMainThread.fulfill() }
165+
wait(for: [expectMainThread], timeout: timeout + 0.5)
166+
}
158167
}

0 commit comments

Comments
 (0)