Skip to content
Open
Show file tree
Hide file tree
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
24 changes: 23 additions & 1 deletion packages/core/realtime-js/src/RealtimeChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ export default class RealtimeChannel {
broadcastEndpointURL: string
subTopic: string
private: boolean
private _subscribeCallback: ((status: REALTIME_SUBSCRIBE_STATES, err?: Error) => void) | null =
null
private _pendingSystemReady: Set<string> = new Set()

/**
* Creates a channel that can broadcast messages, sync presence, and listen to Postgres changes.
Expand Down Expand Up @@ -352,7 +355,12 @@ export default class RealtimeChannel {

this.bindings.postgres_changes = newPostgresBindings

callback && callback(REALTIME_SUBSCRIBE_STATES.SUBSCRIBED)
if (bindingsLen > 0) {
this._subscribeCallback = callback || null
this._pendingSystemReady.add('postgres_changes')
} else {
callback?.(REALTIME_SUBSCRIBE_STATES.SUBSCRIBED)
}
return
}
})
Expand Down Expand Up @@ -819,6 +827,14 @@ export default class RealtimeChannel {
return this.joinPush.ref
}

/** @internal */
_maybeEmitSubscribed(): void {
if (this._pendingSystemReady.size === 0 && this._subscribeCallback) {
this._subscribeCallback(REALTIME_SUBSCRIBE_STATES.SUBSCRIBED)
this._subscribeCallback = null
}
}

/** @internal */
_trigger(type: string, payload?: any, ref?: string) {
const typeLower = type.toLocaleLowerCase()
Expand All @@ -827,6 +843,12 @@ export default class RealtimeChannel {
if (ref && events.indexOf(typeLower) >= 0 && ref !== this._joinRef()) {
return
}

if (typeLower === 'system' && payload?.extension && payload?.status === 'ok') {
Comment thread
7ttp marked this conversation as resolved.
this._pendingSystemReady.delete(payload.extension)
this._maybeEmitSubscribed()
}

let handledPayload = this._onMessage(typeLower, payload, ref)
if (payload && !handledPayload) {
throw 'channel onMessage callbacks must return the payload, modified or unmodified'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,8 @@ describe('Channel Lifecycle Management', () => {
},
})

channel._trigger('system', { extension: 'postgres_changes', status: 'ok' })

expect(spy).toHaveBeenCalledTimes(1)
expect(spy).toHaveBeenCalledWith({
topic: 'realtime:topic',
Expand Down
Loading