Skip to content

Commit

Permalink
Merge pull request #1507 from apollographql/fix/websocket-callback-queue
Browse files Browse the repository at this point in the history
Fix callback queue for Websocket
  • Loading branch information
designatednerd authored Nov 10, 2020
2 parents 0de2954 + 2feb366 commit 85caac5
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions Sources/ApolloWebSocket/WebSocketTransport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,15 @@ extension WebSocketTransport: NetworkTransport {
contextIdentifier: UUID? = nil,
callbackQueue: DispatchQueue = .main,
completionHandler: @escaping (Result<GraphQLResult<Operation.Data>, Error>) -> Void) -> Cancellable {

func callCompletion(with result: Result<GraphQLResult<Operation.Data>, Error>) {
callbackQueue.async {
completionHandler(result)
}
}

if let error = self.error.value {
completionHandler(.failure(error))
callCompletion(with: .failure(error))
return EmptyCancellable()
}

Expand All @@ -371,12 +378,12 @@ extension WebSocketTransport: NetworkTransport {
let response = GraphQLResponse(operation: operation, body: jsonBody)
do {
let graphQLResult = try response.parseResultFast()
completionHandler(.success(graphQLResult))
callCompletion(with: .success(graphQLResult))
} catch {
completionHandler(.failure(error))
callCompletion(with: .failure(error))
}
case .failure(let error):
completionHandler(.failure(error))
callCompletion(with: .failure(error))
}
}
}
Expand Down

0 comments on commit 85caac5

Please sign in to comment.