Skip to content

Commit cb34c4b

Browse files
committed
Use guard syntax in attemptReconnect function
1 parent f4b0e9f commit cb34c4b

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

Source/PusherWebsocketDelegate.swift

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -73,25 +73,29 @@ extension PusherConnection: WebSocketDelegate {
7373
Attempt to reconnect triggered by a disconnection
7474
*/
7575
internal func attemptReconnect() {
76-
if connectionState != .Connected {
77-
if (reconnectAttemptsMax == nil || reconnectAttempts < reconnectAttemptsMax!) {
78-
let reconnectInterval = Double(reconnectAttempts * reconnectAttempts)
79-
80-
let timeInterval = maxReconnectGapInSeconds != nil ? min(reconnectInterval, maxReconnectGapInSeconds!)
81-
: reconnectInterval
82-
83-
self.debugLogger?("[PUSHER DEBUG] Waiting \(timeInterval) seconds before attempting to reconnect (attempt \(reconnectAttempts + 1) of \(reconnectAttemptsMax))")
84-
85-
reconnectTimer = NSTimer.scheduledTimerWithTimeInterval(
86-
timeInterval,
87-
target: self,
88-
selector: #selector(connect),
89-
userInfo: nil,
90-
repeats: false
91-
)
92-
reconnectAttempts += 1
93-
}
76+
guard connectionState != .Connected else {
77+
return
78+
}
79+
80+
guard reconnectAttemptsMax == nil || reconnectAttempts < reconnectAttemptsMax! else {
81+
return
9482
}
83+
84+
let reconnectInterval = Double(reconnectAttempts * reconnectAttempts)
85+
86+
let timeInterval = maxReconnectGapInSeconds != nil ? min(reconnectInterval, maxReconnectGapInSeconds!)
87+
: reconnectInterval
88+
89+
self.debugLogger?("[PUSHER DEBUG] Waiting \(timeInterval) seconds before attempting to reconnect (attempt \(reconnectAttempts + 1) of \(reconnectAttemptsMax))")
90+
91+
reconnectTimer = NSTimer.scheduledTimerWithTimeInterval(
92+
timeInterval,
93+
target: self,
94+
selector: #selector(connect),
95+
userInfo: nil,
96+
repeats: false
97+
)
98+
reconnectAttempts += 1
9599
}
96100

97101

0 commit comments

Comments
 (0)