|
7 | 7 |
|
8 | 8 | import ConcurrencyExtras
|
9 | 9 | import Foundation
|
10 |
| -import Helpers |
11 | 10 | import HTTPTypes
|
| 11 | +import Helpers |
12 | 12 |
|
13 | 13 | #if canImport(FoundationNetworking)
|
14 | 14 | import FoundationNetworking
|
@@ -59,7 +59,9 @@ extension Socket {
|
59 | 59 | addChannel: { [weak client] in client?.addChannel($0) },
|
60 | 60 | removeChannel: { [weak client] in await client?.removeChannel($0) },
|
61 | 61 | push: { [weak client] in await client?.push($0) },
|
62 |
| - httpSend: { [weak client] in try await client?.http.send($0) ?? .init(data: Data(), response: HTTPURLResponse()) } |
| 62 | + httpSend: { [weak client] in |
| 63 | + try await client?.http.send($0) ?? .init(data: Data(), response: HTTPURLResponse()) |
| 64 | + } |
63 | 65 | )
|
64 | 66 | }
|
65 | 67 | }
|
@@ -185,7 +187,8 @@ public final class RealtimeChannelV2: Sendable {
|
185 | 187 | @available(
|
186 | 188 | *,
|
187 | 189 | deprecated,
|
188 |
| - message: "manually updating auth token per channel is not recommended, please use `setAuth` in RealtimeClient instead." |
| 190 | + message: |
| 191 | + "manually updating auth token per channel is not recommended, please use `setAuth` in RealtimeClient instead." |
189 | 192 | )
|
190 | 193 | public func updateAuth(jwt: String?) async {
|
191 | 194 | logger?.debug("Updating auth token for channel \(topic)")
|
@@ -238,8 +241,8 @@ public final class RealtimeChannelV2: Sendable {
|
238 | 241 | event: event,
|
239 | 242 | payload: message,
|
240 | 243 | private: config.isPrivate
|
241 |
| - ), |
242 |
| - ], |
| 244 | + ) |
| 245 | + ] |
243 | 246 | ]
|
244 | 247 | )
|
245 | 248 | )
|
@@ -295,20 +298,27 @@ public final class RealtimeChannelV2: Sendable {
|
295 | 298 |
|
296 | 299 | func onMessage(_ message: RealtimeMessageV2) async {
|
297 | 300 | do {
|
298 |
| - guard let eventType = message.eventType else { |
| 301 | + guard let eventType = message._eventType else { |
299 | 302 | logger?.debug("Received message without event type: \(message)")
|
300 | 303 | return
|
301 | 304 | }
|
302 | 305 |
|
303 | 306 | switch eventType {
|
304 | 307 | case .tokenExpired:
|
305 |
| - logger?.debug( |
306 |
| - "Received token expired event. This should not happen, please report this warning." |
307 |
| - ) |
| 308 | + // deprecated type |
| 309 | + break |
308 | 310 |
|
309 | 311 | case .system:
|
310 |
| - logger?.debug("Subscribed to channel \(message.topic)") |
311 |
| - status = .subscribed |
| 312 | + if message.status == .ok { |
| 313 | + logger?.debug("Subscribed to channel \(message.topic)") |
| 314 | + status = .subscribed |
| 315 | + } else { |
| 316 | + logger?.debug( |
| 317 | + "Failed to subscribe to channel \(message.topic): \(message.payload)" |
| 318 | + ) |
| 319 | + } |
| 320 | + |
| 321 | + callbackManager.triggerSystem(message: message) |
312 | 322 |
|
313 | 323 | case .reply:
|
314 | 324 | guard
|
@@ -545,6 +555,24 @@ public final class RealtimeChannelV2: Sendable {
|
545 | 555 | }
|
546 | 556 | }
|
547 | 557 |
|
| 558 | + /// Listen for `system` event. |
| 559 | + public func onSystem( |
| 560 | + callback: @escaping @Sendable (RealtimeMessageV2) -> Void |
| 561 | + ) -> RealtimeSubscription { |
| 562 | + let id = callbackManager.addSystemCallback(callback: callback) |
| 563 | + return RealtimeSubscription { [weak callbackManager, logger] in |
| 564 | + logger?.debug("Removing system callback with id: \(id)") |
| 565 | + callbackManager?.removeCallback(id: id) |
| 566 | + } |
| 567 | + } |
| 568 | + |
| 569 | + /// Listen for `system` event. |
| 570 | + public func onSystem( |
| 571 | + callback: @escaping @Sendable () -> Void |
| 572 | + ) -> RealtimeSubscription { |
| 573 | + self.onSystem { _ in callback() } |
| 574 | + } |
| 575 | + |
548 | 576 | @discardableResult
|
549 | 577 | func push(_ event: String, ref: String? = nil, payload: JSONObject = [:]) async -> PushStatus {
|
550 | 578 | let push = mutableState.withValue {
|
|
0 commit comments