@@ -32,13 +32,15 @@ public final class RealtimeChannelV2: Sendable {
32
32
var pushes : [ String : PushV2 ] = [ : ]
33
33
}
34
34
35
- private let mutableState = LockIsolated ( MutableState ( ) )
35
+ @MainActor
36
+ private var mutableState = MutableState ( )
36
37
37
38
let topic : String
38
39
let config : RealtimeChannelConfig
39
40
let logger : ( any SupabaseLogger ) ?
40
41
let socket : RealtimeClientV2
41
- var joinRef : String ? { mutableState. joinRef }
42
+
43
+ @MainActor var joinRef : String ? { mutableState. joinRef }
42
44
43
45
let callbackManager = CallbackManager ( )
44
46
private let statusSubject = AsyncValueSubject < RealtimeChannelStatus > ( . unsubscribed)
@@ -81,6 +83,7 @@ public final class RealtimeChannelV2: Sendable {
81
83
}
82
84
83
85
/// Subscribes to the channel
86
+ @MainActor
84
87
public func subscribe( ) async {
85
88
if socket. status != . connected {
86
89
if socket. options. connectOnSubscribe != true {
@@ -109,7 +112,7 @@ public final class RealtimeChannelV2: Sendable {
109
112
)
110
113
111
114
let joinRef = socket. makeRef ( )
112
- mutableState. withValue { $0 . joinRef = joinRef }
115
+ mutableState. joinRef = joinRef
113
116
114
117
logger? . debug ( " Subscribing to channel with body: \( joinConfig) " )
115
118
@@ -497,8 +500,8 @@ public final class RealtimeChannelV2: Sendable {
497
500
filter: filter
498
501
)
499
502
500
- mutableState . withValue {
501
- $0 . clientChanges. append ( config)
503
+ Task { @ MainActor in
504
+ mutableState . clientChanges. append ( config)
502
505
}
503
506
504
507
let id = callbackManager. addPostgresCallback ( filter: config, callback: callback)
@@ -538,32 +541,28 @@ public final class RealtimeChannelV2: Sendable {
538
541
self . onSystem { _ in callback ( ) }
539
542
}
540
543
544
+ @MainActor
541
545
@discardableResult
542
546
func push( _ event: String , ref: String ? = nil , payload: JSONObject = [ : ] ) async -> PushStatus {
543
- let push = mutableState. withValue {
544
- let message = RealtimeMessageV2 (
545
- joinRef: $0. joinRef,
546
- ref: ref ?? socket. makeRef ( ) ,
547
- topic: self . topic,
548
- event: event,
549
- payload: payload
550
- )
551
-
552
- let push = PushV2 ( channel: self , message: message)
553
- if let ref = message. ref {
554
- $0. pushes [ ref] = push
555
- }
547
+ let message = RealtimeMessageV2 (
548
+ joinRef: joinRef,
549
+ ref: ref ?? socket. makeRef ( ) ,
550
+ topic: self . topic,
551
+ event: event,
552
+ payload: payload
553
+ )
556
554
557
- return push
555
+ let push = PushV2 ( channel: self , message: message)
556
+ if let ref = message. ref {
557
+ mutableState. pushes [ ref] = push
558
558
}
559
559
560
560
return await push. send ( )
561
561
}
562
562
563
- private func didReceiveReply( ref: String , status: String ) async {
564
- let push = mutableState. withValue {
565
- $0. pushes. removeValue ( forKey: ref)
566
- }
567
- await push? . didReceive ( status: PushStatus ( rawValue: status) ?? . ok)
563
+ @MainActor
564
+ private func didReceiveReply( ref: String , status: String ) {
565
+ let push = mutableState. pushes. removeValue ( forKey: ref)
566
+ push? . didReceive ( status: PushStatus ( rawValue: status) ?? . ok)
568
567
}
569
568
}
0 commit comments