-
-
Notifications
You must be signed in to change notification settings - Fork 147
feat(realtime): add convenience types for handling broadcast changes #723
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
base: main
Are you sure you want to change the base?
Conversation
Add usage example in SlackClone app
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.
Pull Request Overview
This PR introduces convenience types for handling realtime broadcast changes and updates the corresponding APIs across the codebase, including the SlackClone example app. Key changes include:
- Adding BroadcastEvent and BroadcastChange types in Sources/Realtime/Types.swift.
- Updating channel subscription and broadcast handling in RealtimeChannelV2.swift, RealtimeChannel+AsyncAwait.swift, and CallbackManager.swift.
- Updating dependencies, configuration, and project files (including the swift-async-algorithms dependency) to support the new broadcast types.
Reviewed Changes
Copilot reviewed 14 out of 14 changed files in this pull request and generated 2 comments.
Show a summary per file
File | Description |
---|---|
Supabase.xcworkspace/xcshareddata/swiftpm/Package.resolved | Added swift-async-algorithms dependency. |
Sources/Realtime/Types.swift | Introduced new broadcast types for decoding broadcast events. |
Sources/Realtime/RealtimeChannelV2.swift | Updated broadcast event processing to use the new types. |
Sources/Realtime/RealtimeChannel+AsyncAwait.swift | Changed broadcastStream to emit BroadcastEvent. |
Sources/Realtime/Deprecated/Deprecated.swift | Provided deprecated overloads to bridge JSONObject and BroadcastEvent. |
Sources/Realtime/CallbackManager.swift | Updated broadcast callback handling to support BroadcastEvent. |
Sources/Helpers/AnyJSON/AnyJSON.swift | Added AnyCodingKey for JSON decoding. |
Examples/SlackClone/** | Updated migrations, configuration, and example channel store to work with broadcast changes. |
Examples/Examples.xcodeproj/project.pbxproj | Updated package product dependencies for swift-async-algorithms. |
callback: @escaping @Sendable (JSONObject) -> Void | ||
) -> RealtimeSubscription { | ||
self.onBroadcast(event: event) { (payload: BroadcastEvent) in | ||
callback(try! JSONObject(payload)) |
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.
Using force try here may result in runtime crashes if the conversion fails. Consider using safe error handling (e.g., do-catch or try?) to improve the robustness of the deprecated API.
Copilot uses AI. Check for mistakes.
@available(*, deprecated, message: "Use `broadcastStream(event:)` with `BroadcastEvent` instead.") | ||
public func broadcastStream(event: String) -> AsyncStream<JSONObject> { | ||
self.broadcastStream(event: event).map { (payload: BroadcastEvent) in | ||
try! JSONObject(payload) |
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.
Force unwrapping with try! in this mapping could lead to crashes if decoding fails. It is recommended to handle decoding errors safely rather than force casting.
try! JSONObject(payload) | |
do { | |
return try JSONObject(payload) | |
} catch { | |
// Log the error and provide a fallback value | |
print("Failed to decode payload into JSONObject: \(error)") | |
return JSONObject() // Provide an empty JSONObject as a fallback | |
} |
Copilot uses AI. Check for mistakes.
Pull Request Test Coverage Report for Build 15325396284Details
💛 - Coveralls |
What kind of change does this PR introduce?
Feature and refactor
What is the current behavior?
When receiving a broadcast event, the payload is a raw
JSONObject
.What is the new behavior?
Add a new type
BroadcastEvent
with a convenience method for decoding the event into aBroadcastChange
, making it easier to integrate database changes through broadcasts in a Swift app.Also, refactor the SlackClone sample to use these new tools.
Additional context
Add any other context or screenshots.