Skip to content

Commit

Permalink
fix: Enrich send function arguments
Browse files Browse the repository at this point in the history
Added more information about the arguments for channel.send()

Had to change the payload name since it could potentially conflict with the name within the object
  • Loading branch information
filipecabaco committed Oct 16, 2023
1 parent bc7b897 commit f4c0aef
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions src/RealtimeChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export enum REALTIME_SUBSCRIBE_STATES {
* and narrows the scope of data flow to subscribed clients.
* You can think of a channel as a chatroom where participants are able to see who's online
* and send and receive messages.
**/
*/
export default class RealtimeChannel {
bindings: {
[key: string]: {
Expand Down Expand Up @@ -406,13 +406,26 @@ export default class RealtimeChannel {
): RealtimeChannel {
return this._on(type, filter, callback)
}

/**
* Sends a message into the channel.
*
* @param args Arguments to send to channel
* @param args.type The type of event to send
* @param args.event The name of the event being sent
* @param args.payload Payload to be sent
* @param opts Options to be used during the send process
*/
async send(
payload: { type: string; [key: string]: any },
args: {
type: 'broadcast' | 'presence' | 'postgres_changes'
event: string
payload?: any
[key: string]: any
},
opts: { [key: string]: any } = {}
): Promise<RealtimeChannelSendResponse> {
if (!this._canPush() && payload.type === 'broadcast') {
const { event, payload: endpoint_payload } = payload
if (!this._canPush() && args.type === 'broadcast') {
const { event, payload: endpoint_payload } = args
const options = {
method: 'POST',
headers: {
Expand Down Expand Up @@ -447,20 +460,13 @@ export default class RealtimeChannel {
}
} else {
return new Promise((resolve) => {
const push = this._push(
payload.type,
payload,
opts.timeout || this.timeout
)
const push = this._push(args.type, args, opts.timeout || this.timeout)

if (push.rateLimited) {
resolve('rate limited')
}

if (
payload.type === 'broadcast' &&
!this.params?.config?.broadcast?.ack
) {
if (args.type === 'broadcast' && !this.params?.config?.broadcast?.ack) {
resolve('ok')
}

Expand Down

0 comments on commit f4c0aef

Please sign in to comment.