Skip to content

feat(realtime): enhance RealtimeChannel type #459

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

kakeluh
Copy link

@kakeluh kakeluh commented Apr 3, 2025

What kind of change does this PR introduce?

infer payload type sent by realtime.broadcast_changes

What is the current behavior?

  on<{ record: Tables<"todos"> }>("broadcast", { event: "INSERT" }, ({ payload: { record } }) => {})

What is the new behavior?

  on<Tables<"todos">>("broadcast", { event: "INSERT" }, ({ payload: { record } }) => {})

Additional context

@grdsdev grdsdev requested a review from Copilot June 2, 2025 12:22
Copy link

@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

Enhances the RealtimeChannel typing so that broadcast payloads infer the record shape directly from the generic parameter.

  • Introduces RealtimeBroadcast*Payload types with a shared base and specific insert/update/delete variants.
  • Adds overloaded on<T> methods for BROADCAST events (ALL, INSERT, UPDATE, DELETE) returning strongly-typed payloads.
Comments suppressed due to low confidence (2)

src/RealtimeChannel.ts:441

  • These new generic overloads and payload types should be covered by unit tests to ensure type inference works as expected and that events dispatch correct payload shapes.
on<T extends { [key: string]: any }>(

src/RealtimeChannel.ts:443

  • For the ALL overload, the event field is typed as ALL but at runtime the payload.event will be the specific INSERT/UPDATE/DELETE. Update the type to a union of specific events or rename to avoid misleading consumers.
filter: { event: REALTIME_POSTGRES_CHANGES_LISTEN_EVENT.ALL },

Comment on lines +34 to +39
type RealtimeBroadcastChangesPayloadBase = {
id: string
schema: string
table: string
}

Copy link
Preview

Copilot AI Jun 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] RealtimeBroadcastChangesPayloadBase overlaps with RealtimePostgresChangesPayloadBase (schema/table). Consider merging shared fields or extending one from the other to reduce duplication.

Suggested change
type RealtimeBroadcastChangesPayloadBase = {
id: string
schema: string
table: string
}
type RealtimeChangesPayloadBase = {
schema: string
table: string
}
type RealtimeBroadcastChangesPayloadBase = RealtimeChangesPayloadBase & {
id: string
}

Copilot uses AI. Check for mistakes.

@@ -406,6 +438,42 @@ export default class RealtimeChannel {
payload: T
}) => void
): RealtimeChannel
on<T extends { [key: string]: any }>(
Copy link
Preview

Copilot AI Jun 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Using T extends { [key: string]: any } is very loose. Consider constraining to Record<string, unknown> or a more specific shape to improve type safety.

Suggested change
on<T extends { [key: string]: any }>(
on<T extends Record<string, unknown>>(

Copilot uses AI. Check for mistakes.

@@ -406,6 +438,42 @@ export default class RealtimeChannel {
payload: T
}) => void
): RealtimeChannel
on<T extends { [key: string]: any }>(
Copy link
Preview

Copilot AI Jun 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The multiple overloads for broadcast events are largely repetitive. You could consolidate them using a single overload with conditional types on filter.event to reduce duplication.

Copilot uses AI. Check for mistakes.

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.

1 participant