Skip to content

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

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

grdsdev
Copy link
Collaborator

@grdsdev grdsdev commented May 29, 2025

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 a BroadcastChange, 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.

@grdsdev grdsdev requested a review from Copilot May 29, 2025 11:45
Copy link
Contributor

@Copilot Copilot AI left a 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))
Copy link
Preview

Copilot AI May 29, 2025

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)
Copy link
Preview

Copilot AI May 29, 2025

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.

Suggested change
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.

@coveralls
Copy link

coveralls commented May 29, 2025

Pull Request Test Coverage Report for Build 15325396284

Details

  • 62 of 75 (82.67%) changed or added relevant lines in 5 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.3%) to 75.933%

Changes Missing Coverage Covered Lines Changed/Added Lines %
Sources/Realtime/RealtimeChannel+AsyncAwait.swift 0 2 0.0%
Sources/Realtime/RealtimeChannelV2.swift 0 2 0.0%
Sources/Helpers/AnyJSON/AnyJSON.swift 7 11 63.64%
Sources/Realtime/Types.swift 54 59 91.53%
Totals Coverage Status
Change from base Build 15301720131: 0.3%
Covered Lines: 5291
Relevant Lines: 6968

💛 - Coveralls

@grdsdev grdsdev marked this pull request as ready for review May 29, 2025 12:11
@grdsdev grdsdev marked this pull request as draft May 29, 2025 13:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants