Skip to content

Commit 3b61ae2

Browse files
author
Guilherme Souza
committed
fix parsing of tokenExpired event type
1 parent 7c0a197 commit 3b61ae2

File tree

5 files changed

+47
-46
lines changed

5 files changed

+47
-46
lines changed

Package.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ let package = Package(
120120
name: "Realtime",
121121
dependencies: [
122122
.product(name: "ConcurrencyExtras", package: "swift-concurrency-extras"),
123+
.product(name: "IssueReporting", package: "xctest-dynamic-overlay"),
123124
"Helpers",
124125
]
125126
),

Sources/Realtime/V2/CallbackManager.swift

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
//
2-
// CallbackManager.swift
3-
//
4-
//
5-
// Created by Guilherme Souza on 24/12/23.
6-
//
7-
81
import ConcurrencyExtras
92
import Foundation
103
import Helpers
@@ -26,6 +19,10 @@ final class CallbackManager: Sendable {
2619
mutableState.callbacks
2720
}
2821

22+
deinit {
23+
reset()
24+
}
25+
2926
@discardableResult
3027
func addBroadcastCallback(
3128
event: String,

Sources/Realtime/V2/PushV2.swift

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,31 @@ actor PushV2 {
2727
}
2828

2929
func send() async -> PushStatus {
30-
await channel?.socket.push(message)
31-
32-
if channel?.config.broadcast.acknowledgeBroadcasts == true {
33-
do {
34-
return try await withTimeout(interval: channel?.socket.options().timeoutInterval ?? 10) {
35-
await withCheckedContinuation {
36-
self.receivedContinuation = $0
37-
}
30+
guard let channel = channel else {
31+
return .error
32+
}
33+
34+
await channel.socket.push(message)
35+
36+
if !channel.config.broadcast.acknowledgeBroadcasts {
37+
// channel was configured with `ack = false`,
38+
// don't wait for a response and return `ok`.
39+
return .ok
40+
}
41+
42+
do {
43+
return try await withTimeout(interval: channel.socket.options().timeoutInterval) {
44+
await withCheckedContinuation { continuation in
45+
self.receivedContinuation = continuation
3846
}
39-
} catch is TimeoutError {
40-
channel?.logger?.debug("Push timed out.")
41-
return .timeout
42-
} catch {
43-
channel?.logger?.error("Error sending push: \(error)")
44-
return .error
4547
}
48+
} catch is TimeoutError {
49+
channel.logger?.debug("Push timed out.")
50+
return .timeout
51+
} catch {
52+
channel.logger?.error("Error sending push: \(error.localizedDescription)")
53+
return .error
4654
}
47-
48-
return .ok
4955
}
5056

5157
func didReceive(status: PushStatus) {

Sources/Realtime/V2/RealtimeChannelV2.swift

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
1-
//
2-
// RealtimeChannelV2.swift
3-
//
4-
//
5-
// Created by Guilherme Souza on 26/12/23.
6-
//
7-
81
import ConcurrencyExtras
92
import Foundation
103
import HTTPTypes
114
import Helpers
5+
import IssueReporting
126

137
#if canImport(FoundationNetworking)
148
import FoundationNetworking
@@ -123,9 +117,10 @@ public final class RealtimeChannelV2: Sendable {
123117
public func subscribe() async {
124118
if socket.status() != .connected {
125119
if socket.options().connectOnSubscribe != true {
126-
fatalError(
120+
reportIssue(
127121
"You can't subscribe to a channel while the realtime client is not connected. Did you forget to call `realtime.connect()`?"
128122
)
123+
return
129124
}
130125
await socket.connect()
131126
}
@@ -261,15 +256,21 @@ public final class RealtimeChannelV2: Sendable {
261256
}
262257
}
263258

259+
/// Tracks the given state in the channel.
260+
/// - Parameter state: The state to be tracked, conforming to `Codable`.
261+
/// - Throws: An error if the tracking fails.
264262
public func track(_ state: some Codable) async throws {
265263
try await track(state: JSONObject(state))
266264
}
267265

266+
/// Tracks the given state in the channel.
267+
/// - Parameter state: The state to be tracked as a `JSONObject`.
268268
public func track(state: JSONObject) async {
269-
assert(
270-
status == .subscribed,
271-
"You can only track your presence after subscribing to the channel. Did you forget to call `channel.subscribe()`?"
272-
)
269+
if status != .subscribed {
270+
reportIssue(
271+
"You can only track your presence after subscribing to the channel. Did you forget to call `channel.subscribe()`?"
272+
)
273+
}
273274

274275
await push(
275276
ChannelEvent.presence,
@@ -281,6 +282,7 @@ public final class RealtimeChannelV2: Sendable {
281282
)
282283
}
283284

285+
/// Stops tracking the current state in the channel.
284286
public func untrack() async {
285287
await push(
286288
ChannelEvent.presence,
@@ -515,10 +517,12 @@ public final class RealtimeChannelV2: Sendable {
515517
filter: String?,
516518
callback: @escaping @Sendable (AnyAction) -> Void
517519
) -> RealtimeSubscription {
518-
precondition(
519-
status != .subscribed,
520-
"You cannot call postgresChange after joining the channel"
521-
)
520+
guard status == .subscribed else {
521+
reportIssue(
522+
"You cannot call postgresChange after joining the channel, this won't work as expected."
523+
)
524+
return RealtimeSubscription {}
525+
}
522526

523527
let config = PostgresJoinConfig(
524528
event: event,

Sources/Realtime/V2/RealtimeMessageV2.swift

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
//
2-
// RealtimeMessageV2.swift
3-
//
4-
//
5-
// Created by Guilherme Souza on 11/01/24.
6-
//
7-
81
import Foundation
92
import Helpers
103

0 commit comments

Comments
 (0)