Skip to content

Commit 997e27a

Browse files
authored
Merge pull request #1224 from appwrite/add-swift-realtime-callbacks
2 parents 0c4f514 + e278eb2 commit 997e27a

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

templates/swift/Sources/Services/Realtime.swift.twig

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ open class Realtime : Service {
2222
private var reconnectAttempts = 0
2323
private var subscriptionsCounter = 0
2424
private var reconnect = true
25+
26+
private var onErrorCallbacks: [((Swift.Error?, HTTPResponseStatus?) -> Void)] = []
27+
private var onCloseCallbacks: [(() -> Void)] = []
28+
29+
public func onError(_ callback: @escaping (Swift.Error?, HTTPResponseStatus?) -> Void) {
30+
self.onErrorCallbacks.append(callback)
31+
}
32+
33+
public func onClose(_ callback: @escaping () -> Void) {
34+
self.onCloseCallbacks.append(callback)
35+
}
2536

2637
private func startHeartbeat() {
2738
stopHeartbeat()
@@ -212,6 +223,7 @@ extension Realtime: WebSocketClientDelegate {
212223
stopHeartbeat()
213224

214225
if (!reconnect) {
226+
onCloseCallbacks.forEach { $0() }
215227
reconnect = true
216228
return
217229
}
@@ -230,6 +242,8 @@ extension Realtime: WebSocketClientDelegate {
230242
public func onError(error: Swift.Error?, status: HTTPResponseStatus?) {
231243
stopHeartbeat()
232244
print(error?.localizedDescription ?? "Unknown error")
245+
246+
onErrorCallbacks.forEach { $0(error, status) }
233247
}
234248

235249
func handleResponseError(from json: [String: Any]) throws {

0 commit comments

Comments
 (0)