Skip to content

Commit

Permalink
fix: rebase RTConnectionProvider+Websocket to Async version (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
lawmicha committed Jul 11, 2022
1 parent 876f40f commit a1d49a1
Showing 1 changed file with 13 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ extension RealtimeConnectionProviderAsync: AppSyncWebsocketDelegate {
taskQueue.async { [weak self] in
self?.handleError(response: response)
}
case .connectionError:
AppSyncLogger.verbose("[RealtimeConnectionProvider] received error")
taskQueue.async { [weak self] in
self?.handleError(response: response)
}
case .subscriptionAck, .unsubscriptionAck, .data:
if let appSyncResponse = response.toAppSyncResponse() {
updateCallback(event: .data(appSyncResponse))
Expand Down Expand Up @@ -107,42 +112,18 @@ extension RealtimeConnectionProviderAsync: AppSyncWebsocketDelegate {
///
/// - Warning: This method must be invoked on the `connectionQueue`
func handleError(response: RealtimeConnectionProviderResponse) {
// If we get an error in connection inprogress state, return back as connection error.
guard status != .inProgress else {
// If we get an error while the connection was inProgress state,
let error = response.toConnectionProviderError(connectionState: status)
if status == .inProgress {
status = .notConnected
updateCallback(event: .error(ConnectionProviderError.connection))
return
}

if response.isLimitExceededError() {
let limitExceedError = ConnectionProviderError.limitExceeded(response.id)

guard response.id == nil else {
updateCallback(event: .error(limitExceedError))
return
}

limitExceededSubject.send(limitExceedError)
return
}

if response.isMaxSubscriptionReachedError() {
let limitExceedError = ConnectionProviderError.limitExceeded(response.id)
updateCallback(event: .error(limitExceedError))
return
// If limit exceeded is for a particular subscription identifier, throttle using `limitExceededSubject`
if case .limitExceeded(let id) = error, id == nil {
self.limitExceededSubject.send(error)
} else {
updateCallback(event: .error(error))
}

// If the type of error is not handled (by checking `isLimitExceededError`, `isMaxSubscriptionReachedError`,
// etc), and is not for a specific subscription, then return a generic error
guard let identifier = response.id else {
let genericError = ConnectionProviderError.other
updateCallback(event: .error(genericError))
return
}

// Default scenario - return the error with subscription id and error payload.
let subscriptionError = ConnectionProviderError.subscription(identifier, response.payload)
updateCallback(event: .error(subscriptionError))
}

}
Expand Down

0 comments on commit a1d49a1

Please sign in to comment.