Skip to content

Commit e1b1b57

Browse files
w3b6x9inian
authored andcommitted
feat: add on bindings to subscribe
1 parent 117b255 commit e1b1b57

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

packages/core/realtime-js/src/RealtimeChannel.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,29 @@ export default class RealtimeChannel {
8080
if (this.joinedOnce) {
8181
throw `tried to subscribe multiple times. 'subscribe' can only be called a single time per channel instance`
8282
} else {
83+
const configs = this.bindings.reduce(
84+
(acc, binding: { [key: string]: any }) => {
85+
const { type } = binding
86+
if (
87+
![
88+
'phx_close',
89+
'phx_error',
90+
'phx_reply',
91+
'presence_diff',
92+
'presence_state',
93+
].includes(type)
94+
) {
95+
acc[type] = binding
96+
}
97+
return acc
98+
},
99+
{}
100+
)
101+
102+
if (Object.keys(configs).length) {
103+
this.updateJoinPayload({ configs })
104+
}
105+
83106
this.joinedOnce = true
84107
this.rejoin(timeout)
85108
return this.joinPush
@@ -211,9 +234,9 @@ export default class RealtimeChannel {
211234
send(payload: { type: string; [key: string]: any }) {
212235
const push = this.push(payload.type as any, payload)
213236

214-
return new Promise((resolve) => {
237+
return new Promise((resolve, reject) => {
215238
push.receive('ok', () => resolve('ok'))
216-
push.receive('timeout', () => resolve('timeout'))
239+
push.receive('timeout', () => reject('timeout'))
217240
})
218241
}
219242

0 commit comments

Comments
 (0)