Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions templates/swift/Sources/Services/Realtime.swift.twig
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add a e2e test for it? Is it easy?

Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ open class Realtime : Service {
private var reconnectAttempts = 0
private var subscriptionsCounter = 0
private var reconnect = true

private var onErrorCallbacks: [((Swift.Error?, HTTPResponseStatus?) -> Void)] = []
private var onCloseCallbacks: [(() -> Void)] = []

public func onError(_ callback: @escaping (Swift.Error?, HTTPResponseStatus?) -> Void) {
self.onErrorCallbacks.append(callback)
}

public func onClose(_ callback: @escaping () -> Void) {
self.onCloseCallbacks.append(callback)
}

private func startHeartbeat() {
stopHeartbeat()
Expand Down Expand Up @@ -212,6 +223,7 @@ extension Realtime: WebSocketClientDelegate {
stopHeartbeat()

if (!reconnect) {
onCloseCallbacks.forEach { $0() }
reconnect = true
return
}
Expand All @@ -230,6 +242,8 @@ extension Realtime: WebSocketClientDelegate {
public func onError(error: Swift.Error?, status: HTTPResponseStatus?) {
stopHeartbeat()
print(error?.localizedDescription ?? "Unknown error")

onErrorCallbacks.forEach { $0(error, status) }
}

func handleResponseError(from json: [String: Any]) throws {
Expand Down
Loading