-
-
Notifications
You must be signed in to change notification settings - Fork 154
fix(realtime): make Push associated to MainActor #721
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -32,13 +32,15 @@ | |||||||||
var pushes: [String: PushV2] = [:] | ||||||||||
} | ||||||||||
|
||||||||||
private let mutableState = LockIsolated(MutableState()) | ||||||||||
@MainActor | ||||||||||
private var mutableState = MutableState() | ||||||||||
|
||||||||||
let topic: String | ||||||||||
let config: RealtimeChannelConfig | ||||||||||
let logger: (any SupabaseLogger)? | ||||||||||
let socket: RealtimeClientV2 | ||||||||||
var joinRef: String? { mutableState.joinRef } | ||||||||||
|
||||||||||
@MainActor var joinRef: String? { mutableState.joinRef } | ||||||||||
|
||||||||||
let callbackManager = CallbackManager() | ||||||||||
private let statusSubject = AsyncValueSubject<RealtimeChannelStatus>(.unsubscribed) | ||||||||||
|
@@ -81,6 +83,7 @@ | |||||||||
} | ||||||||||
|
||||||||||
/// Subscribes to the channel | ||||||||||
@MainActor | ||||||||||
public func subscribe() async { | ||||||||||
if socket.status != .connected { | ||||||||||
if socket.options.connectOnSubscribe != true { | ||||||||||
|
@@ -109,7 +112,7 @@ | |||||||||
) | ||||||||||
|
||||||||||
let joinRef = socket.makeRef() | ||||||||||
mutableState.withValue { $0.joinRef = joinRef } | ||||||||||
mutableState.joinRef = joinRef | ||||||||||
|
||||||||||
logger?.debug("Subscribing to channel with body: \(joinConfig)") | ||||||||||
|
||||||||||
|
@@ -497,8 +500,8 @@ | |||||||||
filter: filter | ||||||||||
) | ||||||||||
|
||||||||||
mutableState.withValue { | ||||||||||
$0.clientChanges.append(config) | ||||||||||
Task { @MainActor in | ||||||||||
mutableState.clientChanges.append(config) | ||||||||||
} | ||||||||||
Comment on lines
+503
to
505
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since the enclosing context is already on the MainActor, consider appending directly to mutableState.clientChanges instead of dispatching via a Task, to simplify execution and ensure immediate consistency.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||
|
||||||||||
let id = callbackManager.addPostgresCallback(filter: config, callback: callback) | ||||||||||
|
@@ -538,32 +541,28 @@ | |||||||||
self.onSystem { _ in callback() } | ||||||||||
} | ||||||||||
|
||||||||||
@MainActor | ||||||||||
@discardableResult | ||||||||||
func push(_ event: String, ref: String? = nil, payload: JSONObject = [:]) async -> PushStatus { | ||||||||||
let push = mutableState.withValue { | ||||||||||
let message = RealtimeMessageV2( | ||||||||||
joinRef: $0.joinRef, | ||||||||||
ref: ref ?? socket.makeRef(), | ||||||||||
topic: self.topic, | ||||||||||
event: event, | ||||||||||
payload: payload | ||||||||||
) | ||||||||||
|
||||||||||
let push = PushV2(channel: self, message: message) | ||||||||||
if let ref = message.ref { | ||||||||||
$0.pushes[ref] = push | ||||||||||
} | ||||||||||
let message = RealtimeMessageV2( | ||||||||||
joinRef: joinRef, | ||||||||||
ref: ref ?? socket.makeRef(), | ||||||||||
topic: self.topic, | ||||||||||
event: event, | ||||||||||
payload: payload | ||||||||||
) | ||||||||||
|
||||||||||
return push | ||||||||||
let push = PushV2(channel: self, message: message) | ||||||||||
if let ref = message.ref { | ||||||||||
mutableState.pushes[ref] = push | ||||||||||
} | ||||||||||
|
||||||||||
return await push.send() | ||||||||||
} | ||||||||||
|
||||||||||
private func didReceiveReply(ref: String, status: String) async { | ||||||||||
let push = mutableState.withValue { | ||||||||||
$0.pushes.removeValue(forKey: ref) | ||||||||||
} | ||||||||||
await push?.didReceive(status: PushStatus(rawValue: status) ?? .ok) | ||||||||||
@MainActor | ||||||||||
private func didReceiveReply(ref: String, status: String) { | ||||||||||
let push = mutableState.pushes.removeValue(forKey: ref) | ||||||||||
push?.didReceive(status: PushStatus(rawValue: status) ?? .ok) | ||||||||||
} | ||||||||||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't a breaking change since the method is
async
, the call site already have to call this method with anawait
independent of the running actor.