@@ -25,46 +25,6 @@ public struct RealtimeChannelConfig: Sendable {
25
25
public var isPrivate : Bool
26
26
}
27
27
28
- struct Socket : Sendable {
29
- var broadcastURL : @Sendable ( ) -> URL
30
- var status : @Sendable ( ) -> RealtimeClientStatus
31
- var options : @Sendable ( ) -> RealtimeClientOptions
32
- var accessToken : @Sendable ( ) async -> String ?
33
- var apiKey : @Sendable ( ) -> String ?
34
- var makeRef : @Sendable ( ) -> Int
35
-
36
- var connect : @Sendable ( ) async -> Void
37
- var addChannel : @Sendable ( _ channel: RealtimeChannelV2 ) -> Void
38
- var removeChannel : @Sendable ( _ channel: RealtimeChannelV2 ) async -> Void
39
- var push : @Sendable ( _ message: RealtimeMessageV2 ) async -> Void
40
- var httpSend : @Sendable ( _ request: Helpers . HTTPRequest ) async throws -> Helpers . HTTPResponse
41
- }
42
-
43
- extension Socket {
44
- init ( client: RealtimeClientV2 ) {
45
- self . init (
46
- broadcastURL: { [ weak client] in client? . broadcastURL ?? URL ( string: " http://localhost " ) ! } ,
47
- status: { [ weak client] in client? . status ?? . disconnected } ,
48
- options: { [ weak client] in client? . options ?? . init( ) } ,
49
- accessToken: { [ weak client] in
50
- if let accessToken = try ? await client? . options. accessToken ? ( ) {
51
- return accessToken
52
- }
53
- return client? . mutableState. accessToken
54
- } ,
55
- apiKey: { [ weak client] in client? . apikey } ,
56
- makeRef: { [ weak client] in client? . makeRef ( ) ?? 0 } ,
57
- connect: { [ weak client] in await client? . connect ( ) } ,
58
- addChannel: { [ weak client] in client? . addChannel ( $0) } ,
59
- removeChannel: { [ weak client] in await client? . removeChannel ( $0) } ,
60
- push: { [ weak client] in await client? . push ( $0) } ,
61
- httpSend: { [ weak client] in
62
- try await client? . http. send ( $0) ?? . init( data: Data ( ) , response: HTTPURLResponse ( ) )
63
- }
64
- )
65
- }
66
- }
67
-
68
28
public final class RealtimeChannelV2 : Sendable {
69
29
struct MutableState {
70
30
var clientChanges : [ PostgresJoinConfig ] = [ ]
@@ -77,7 +37,8 @@ public final class RealtimeChannelV2: Sendable {
77
37
let topic : String
78
38
let config : RealtimeChannelConfig
79
39
let logger : ( any SupabaseLogger ) ?
80
- let socket : Socket
40
+ let socket : RealtimeClientV2
41
+ var joinRef : String ? { mutableState. joinRef }
81
42
82
43
let callbackManager = CallbackManager ( )
83
44
private let statusEventEmitter = EventEmitter < RealtimeChannelStatus > ( initialEvent: . unsubscribed)
@@ -105,7 +66,7 @@ public final class RealtimeChannelV2: Sendable {
105
66
init (
106
67
topic: String ,
107
68
config: RealtimeChannelConfig ,
108
- socket: Socket ,
69
+ socket: RealtimeClientV2 ,
109
70
logger: ( any SupabaseLogger ) ?
110
71
) {
111
72
self . topic = topic
@@ -120,8 +81,8 @@ public final class RealtimeChannelV2: Sendable {
120
81
121
82
/// Subscribes to the channel
122
83
public func subscribe( ) async {
123
- if socket. status ( ) != . connected {
124
- if socket. options ( ) . connectOnSubscribe != true {
84
+ if socket. status != . connected {
85
+ if socket. options. connectOnSubscribe != true {
125
86
reportIssue (
126
87
" You can't subscribe to a channel while the realtime client is not connected. Did you forget to call `realtime.connect()`? "
127
88
)
@@ -130,8 +91,6 @@ public final class RealtimeChannelV2: Sendable {
130
91
await socket. connect ( )
131
92
}
132
93
133
- socket. addChannel ( self )
134
-
135
94
status = . subscribing
136
95
logger? . debug ( " Subscribing to channel \( topic) " )
137
96
@@ -144,10 +103,10 @@ public final class RealtimeChannelV2: Sendable {
144
103
145
104
let payload = RealtimeJoinPayload (
146
105
config: joinConfig,
147
- accessToken: await socket. accessToken ( )
106
+ accessToken: await socket. _getAccessToken ( )
148
107
)
149
108
150
- let joinRef = socket. makeRef ( ) . description
109
+ let joinRef = socket. makeRef ( )
151
110
mutableState. withValue { $0. joinRef = joinRef }
152
111
153
112
logger? . debug ( " Subscribing to channel with body: \( joinConfig) " )
@@ -159,7 +118,7 @@ public final class RealtimeChannelV2: Sendable {
159
118
)
160
119
161
120
do {
162
- try await withTimeout ( interval: socket. options ( ) . timeoutInterval) { [ self ] in
121
+ try await withTimeout ( interval: socket. options. timeoutInterval) { [ self ] in
163
122
_ = await statusChange. first { @Sendable in $0 == . subscribed }
164
123
}
165
124
} catch {
@@ -215,17 +174,17 @@ public final class RealtimeChannelV2: Sendable {
215
174
}
216
175
217
176
var headers : HTTPFields = [ . contentType: " application/json " ]
218
- if let apiKey = socket. apiKey ( ) {
177
+ if let apiKey = socket. options . apikey {
219
178
headers [ . apiKey] = apiKey
220
179
}
221
- if let accessToken = await socket. accessToken ( ) {
180
+ if let accessToken = await socket. _getAccessToken ( ) {
222
181
headers [ . authorization] = " Bearer \( accessToken) "
223
182
}
224
183
225
184
let task = Task { [ headers] in
226
- _ = try ? await socket. httpSend (
185
+ _ = try ? await socket. http . send (
227
186
HTTPRequest (
228
- url: socket. broadcastURL ( ) ,
187
+ url: socket. broadcastURL,
229
188
method: . post,
230
189
headers: headers,
231
190
body: JSONEncoder ( ) . encode (
@@ -245,7 +204,7 @@ public final class RealtimeChannelV2: Sendable {
245
204
}
246
205
247
206
if config. broadcast. acknowledgeBroadcasts {
248
- try ? await withTimeout ( interval: socket. options ( ) . timeoutInterval) {
207
+ try ? await withTimeout ( interval: socket. options. timeoutInterval) {
249
208
await task. value
250
209
}
251
210
}
@@ -406,7 +365,7 @@ public final class RealtimeChannelV2: Sendable {
406
365
callbackManager. triggerBroadcast ( event: event, json: payload)
407
366
408
367
case . close:
409
- await socket. removeChannel ( self )
368
+ socket. _remove ( self )
410
369
logger? . debug ( " Unsubscribed from channel \( message. topic) " )
411
370
status = . unsubscribed
412
371
@@ -582,7 +541,7 @@ public final class RealtimeChannelV2: Sendable {
582
541
let push = mutableState. withValue {
583
542
let message = RealtimeMessageV2 (
584
543
joinRef: $0. joinRef,
585
- ref: ref ?? socket. makeRef ( ) . description ,
544
+ ref: ref ?? socket. makeRef ( ) ,
586
545
topic: self . topic,
587
546
event: event,
588
547
payload: payload
0 commit comments