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
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions src/RealtimeChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,38 @@ export type RealtimeChannelOptions = {
}
}

type RealtimeBroadcastChangesPayloadBase = {
id: string
schema: string
table: string
}

Comment on lines +34 to +39
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.

export type RealtimeBroadcastInsertPayload<T extends { [key: string]: any }> =
RealtimeBroadcastChangesPayloadBase & {
operation: `${REALTIME_POSTGRES_CHANGES_LISTEN_EVENT.INSERT}`
record: T
old_record: null
}

export type RealtimeBroadcastUpdatePayload<T extends { [key: string]: any }> =
RealtimeBroadcastChangesPayloadBase & {
operation: `${REALTIME_POSTGRES_CHANGES_LISTEN_EVENT.UPDATE}`
record: T
old_record: T
}

export type RealtimeBroadcastDeletePayload<T extends { [key: string]: any }> =
RealtimeBroadcastChangesPayloadBase & {
operation: `${REALTIME_POSTGRES_CHANGES_LISTEN_EVENT.DELETE}`
record: null
old_record: T
}

export type RealtimeBroadcastPayload<T extends { [key: string]: any }> =
| RealtimeBroadcastInsertPayload<T>
| RealtimeBroadcastUpdatePayload<T>
| RealtimeBroadcastDeletePayload<T>

type RealtimePostgresChangesPayloadBase = {
schema: string
table: string
Expand Down Expand Up @@ -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.

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.

type: `${REALTIME_LISTEN_TYPES.BROADCAST}`,
filter: { event: REALTIME_POSTGRES_CHANGES_LISTEN_EVENT.ALL },
callback: (payload: {
type: `${REALTIME_LISTEN_TYPES.BROADCAST}`
event: REALTIME_POSTGRES_CHANGES_LISTEN_EVENT.ALL
payload: RealtimeBroadcastPayload<T>
}) => void
): RealtimeChannel
on<T extends { [key: string]: any }>(
type: `${REALTIME_LISTEN_TYPES.BROADCAST}`,
filter: { event: REALTIME_POSTGRES_CHANGES_LISTEN_EVENT.INSERT },
callback: (payload: {
type: `${REALTIME_LISTEN_TYPES.BROADCAST}`
event: REALTIME_POSTGRES_CHANGES_LISTEN_EVENT.INSERT
payload: RealtimeBroadcastInsertPayload<T>
}) => void
): RealtimeChannel
on<T extends { [key: string]: any }>(
type: `${REALTIME_LISTEN_TYPES.BROADCAST}`,
filter: { event: REALTIME_POSTGRES_CHANGES_LISTEN_EVENT.UPDATE },
callback: (payload: {
type: `${REALTIME_LISTEN_TYPES.BROADCAST}`
event: REALTIME_POSTGRES_CHANGES_LISTEN_EVENT.UPDATE
payload: RealtimeBroadcastUpdatePayload<T>
}) => void
): RealtimeChannel
on<T extends { [key: string]: any }>(
type: `${REALTIME_LISTEN_TYPES.BROADCAST}`,
filter: { event: REALTIME_POSTGRES_CHANGES_LISTEN_EVENT.DELETE },
callback: (payload: {
type: `${REALTIME_LISTEN_TYPES.BROADCAST}`
event: REALTIME_POSTGRES_CHANGES_LISTEN_EVENT.DELETE
payload: RealtimeBroadcastDeletePayload<T>
}) => void
): RealtimeChannel
on<T extends { [key: string]: any }>(
type: `${REALTIME_LISTEN_TYPES.SYSTEM}`,
filter: {},
Expand Down