Skip to content

Commit fcd7152

Browse files
committed
fixed retries handling and error dispatching
1 parent 57e1859 commit fcd7152

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

Sources/EventSource/EventSource.swift

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,8 @@ public final class EventSource {
128128
// Send error event
129129
if let error {
130130
await sendErrorEvent(with: error)
131-
} else {
132-
await sendErrorEvent(with: EventSourceError.undefinedConnectionError)
133131
}
134-
132+
135133
// Retry connection or close
136134
if currentRetryCount < maxRetryCount {
137135
currentRetryCount += 1
@@ -155,11 +153,17 @@ public final class EventSource {
155153
completionHandler(.cancel)
156154
return
157155
}
158-
156+
159157
// Stop connection when 204 response code, otherwise keep open
160-
if httpResponse.statusCode != 204, 200...299 ~= httpResponse.statusCode {
158+
guard httpResponse.statusCode != 204 else {
159+
completionHandler(.cancel)
160+
await close()
161+
return
162+
}
163+
164+
if 200...299 ~= httpResponse.statusCode {
161165
// Reset current retries count to allow retry on the next error
162-
currentRetryCount = 0
166+
currentRetryCount = 1
163167

164168
if readyState != .open {
165169
await setOpen()
@@ -207,12 +211,6 @@ public final class EventSource {
207211
}
208212

209213
// MARK: - Fileprivate
210-
211-
fileprivate func setClosed() async {
212-
readyState = .closed
213-
214-
await events.send(.closed)
215-
}
216214

217215
fileprivate func setOpen() async {
218216
readyState = .open

0 commit comments

Comments
 (0)