@@ -32,7 +32,7 @@ public struct RealtimeChannelConfig: Sendable {
32
32
33
33
struct Socket : Sendable {
34
34
var broadcastURL : @Sendable ( ) -> URL
35
- var status : @Sendable ( ) -> RealtimeClientV2 . Status
35
+ var status : @Sendable ( ) -> RealtimeClientStatus
36
36
var options : @Sendable ( ) -> RealtimeClientOptions
37
37
var accessToken : @Sendable ( ) -> String ?
38
38
var apiKey : @Sendable ( ) -> String ?
@@ -64,16 +64,6 @@ extension Socket {
64
64
}
65
65
66
66
public final class RealtimeChannelV2 : Sendable {
67
- @available ( * , deprecated, renamed: " RealtimeSubscription " )
68
- public typealias Subscription = ObservationToken
69
-
70
- public enum Status : Sendable {
71
- case unsubscribed
72
- case subscribing
73
- case subscribed
74
- case unsubscribing
75
- }
76
-
77
67
struct MutableState {
78
68
var clientChanges : [ PostgresJoinConfig ] = [ ]
79
69
var joinRef : String ?
@@ -88,14 +78,14 @@ public final class RealtimeChannelV2: Sendable {
88
78
let socket : Socket
89
79
90
80
let callbackManager = CallbackManager ( )
91
- private let statusEventEmitter = EventEmitter < Status > ( initialEvent: . unsubscribed)
81
+ private let statusEventEmitter = EventEmitter < RealtimeChannelStatus > ( initialEvent: . unsubscribed)
92
82
93
- public private( set) var status : Status {
83
+ public private( set) var status : RealtimeChannelStatus {
94
84
get { statusEventEmitter. lastEvent }
95
85
set { statusEventEmitter. emit ( newValue) }
96
86
}
97
87
98
- public var statusChange : AsyncStream < Status > {
88
+ public var statusChange : AsyncStream < RealtimeChannelStatus > {
99
89
statusEventEmitter. stream ( )
100
90
}
101
91
@@ -105,7 +95,7 @@ public final class RealtimeChannelV2: Sendable {
105
95
///
106
96
/// - Note: Use ``statusChange`` if you prefer to use Async/Await.
107
97
public func onStatusChange(
108
- _ listener: @escaping @Sendable ( Status ) -> Void
98
+ _ listener: @escaping @Sendable ( RealtimeChannelStatus ) -> Void
109
99
) -> ObservationToken {
110
100
statusEventEmitter. attach ( listener)
111
101
}
@@ -137,10 +127,15 @@ public final class RealtimeChannelV2: Sendable {
137
127
await socket. connect ( )
138
128
}
139
129
130
+ guard status != . subscribed else {
131
+ logger? . warning ( " Channel \( topic) is already subscribed " )
132
+ return
133
+ }
134
+
140
135
socket. addChannel ( self )
141
136
142
137
status = . subscribing
143
- logger? . debug ( " subscribing to channel \( topic) " )
138
+ logger? . debug ( " Subscribing to channel \( topic) " )
144
139
145
140
let joinConfig = RealtimeJoinConfig (
146
141
broadcast: config. broadcast,
@@ -157,7 +152,7 @@ public final class RealtimeChannelV2: Sendable {
157
152
let joinRef = socket. makeRef ( ) . description
158
153
mutableState. withValue { $0. joinRef = joinRef }
159
154
160
- logger? . debug ( " subscribing to channel with body: \( joinConfig) " )
155
+ logger? . debug ( " Subscribing to channel with body: \( joinConfig) " )
161
156
162
157
await push (
163
158
RealtimeMessageV2 (
@@ -175,17 +170,17 @@ public final class RealtimeChannelV2: Sendable {
175
170
}
176
171
} catch {
177
172
if error is TimeoutError {
178
- logger? . debug ( " subscribe timed out." )
173
+ logger? . debug ( " Subscribe timed out." )
179
174
await subscribe ( )
180
175
} else {
181
- logger? . error ( " subscribe failed: \( error) " )
176
+ logger? . error ( " Subscribe failed: \( error) " )
182
177
}
183
178
}
184
179
}
185
180
186
181
public func unsubscribe( ) async {
187
182
status = . unsubscribing
188
- logger? . debug ( " unsubscribing from channel \( topic) " )
183
+ logger? . debug ( " Unsubscribing from channel \( topic) " )
189
184
190
185
await push (
191
186
RealtimeMessageV2 (
@@ -324,7 +319,7 @@ public final class RealtimeChannelV2: Sendable {
324
319
)
325
320
}
326
321
327
- func onMessage( _ message: RealtimeMessageV2 ) {
322
+ func onMessage( _ message: RealtimeMessageV2 ) async {
328
323
do {
329
324
guard let eventType = message. eventType else {
330
325
logger? . debug ( " Received message without event type: \( message) " )
@@ -349,7 +344,7 @@ public final class RealtimeChannelV2: Sendable {
349
344
throw RealtimeError ( " Received a reply with unexpected payload: \( message) " )
350
345
}
351
346
352
- didReceiveReply ( ref: ref, status: status)
347
+ await didReceiveReply ( ref: ref, status: status)
353
348
354
349
if message. payload [ " response " ] ? . objectValue? . keys
355
350
. contains ( ChannelEvent . postgresChanges) == true
@@ -409,16 +404,6 @@ public final class RealtimeChannelV2: Sendable {
409
404
)
410
405
)
411
406
412
- case " SELECT " :
413
- action = . select(
414
- SelectAction (
415
- columns: postgresActions. columns,
416
- commitTimestamp: postgresActions. commitTimestamp,
417
- record: postgresActions. record ?? [ : ] ,
418
- rawMessage: message
419
- )
420
- )
421
-
422
407
default :
423
408
throw RealtimeError ( " Unknown event type: \( postgresActions. type) " )
424
409
}
@@ -435,13 +420,9 @@ public final class RealtimeChannelV2: Sendable {
435
420
callbackManager. triggerBroadcast ( event: event, json: payload)
436
421
437
422
case . close:
438
- Task { [ weak self] in
439
- guard let self else { return }
440
-
441
- await socket. removeChannel ( self )
442
- logger? . debug ( " Unsubscribed from channel \( message. topic) " )
443
- status = . unsubscribed
444
- }
423
+ await socket. removeChannel ( self )
424
+ logger? . debug ( " Unsubscribed from channel \( message. topic) " )
425
+ status = . unsubscribed
445
426
446
427
case . error:
447
428
logger? . debug (
@@ -601,12 +582,10 @@ public final class RealtimeChannelV2: Sendable {
601
582
return await push. send ( )
602
583
}
603
584
604
- private func didReceiveReply( ref: String , status: String ) {
605
- Task {
606
- let push = mutableState. withValue {
607
- $0. pushes. removeValue ( forKey: ref)
608
- }
609
- await push? . didReceive ( status: PushStatus ( rawValue: status) ?? . ok)
585
+ private func didReceiveReply( ref: String , status: String ) async {
586
+ let push = mutableState. withValue {
587
+ $0. pushes. removeValue ( forKey: ref)
610
588
}
589
+ await push? . didReceive ( status: PushStatus ( rawValue: status) ?? . ok)
611
590
}
612
591
}
0 commit comments