@@ -23,7 +23,7 @@ export type RealtimeChannelOptions = {
23
23
/**
24
24
* key option is used to track presence payload across clients
25
25
*/
26
- presence ?: { key ?: string }
26
+ presence ?: { key ?: string ; enabled ?: boolean }
27
27
/**
28
28
* defines if the channel is private or not and if RLS policies will be used to check data
29
29
*/
@@ -154,7 +154,7 @@ export default class RealtimeChannel {
154
154
this . params . config = {
155
155
...{
156
156
broadcast : { ack : false , self : false } ,
157
- presence : { key : '' } ,
157
+ presence : { key : '' , enabled : false } ,
158
158
private : false ,
159
159
} ,
160
160
...params . config ,
@@ -222,24 +222,28 @@ export default class RealtimeChannel {
222
222
config : { broadcast, presence, private : isPrivate } ,
223
223
} = this . params
224
224
225
- this . _onError ( ( e : Error ) =>
226
- callback ?.( REALTIME_SUBSCRIBE_STATES . CHANNEL_ERROR , e )
227
- )
228
- this . _onClose ( ( ) => callback ?.( REALTIME_SUBSCRIBE_STATES . CLOSED ) )
225
+ const postgres_changes =
226
+ this . bindings . postgres_changes ?. map ( ( r ) => r . filter ) ?? [ ]
229
227
228
+ const presence_enabled = ! ! this . bindings [ REALTIME_LISTEN_TYPES . PRESENCE ]
230
229
const accessTokenPayload : { access_token ?: string } = { }
231
230
const config = {
232
231
broadcast,
233
- presence,
234
- postgres_changes :
235
- this . bindings . postgres_changes ?. map ( ( r ) => r . filter ) ?? [ ] ,
232
+ presence : { ...presence , enabled : presence_enabled } ,
233
+ postgres_changes,
236
234
private : isPrivate ,
237
235
}
238
236
239
237
if ( this . socket . accessTokenValue ) {
240
238
accessTokenPayload . access_token = this . socket . accessTokenValue
241
239
}
242
240
241
+ this . _onError ( ( e : Error ) =>
242
+ callback ?.( REALTIME_SUBSCRIBE_STATES . CHANNEL_ERROR , e )
243
+ )
244
+
245
+ this . _onClose ( ( ) => callback ?.( REALTIME_SUBSCRIBE_STATES . CLOSED ) )
246
+
243
247
this . updateJoinPayload ( { ...{ config } , ...accessTokenPayload } )
244
248
245
249
this . joinedOnce = true
@@ -414,9 +418,19 @@ export default class RealtimeChannel {
414
418
) : RealtimeChannel
415
419
on (
416
420
type : `${REALTIME_LISTEN_TYPES } `,
417
- filter : { event : string ; [ key : string ] : string } ,
421
+ filter : { event : string ; [ key : string ] : string } ,
418
422
callback : ( payload : any ) => void
419
423
) : RealtimeChannel {
424
+ if (
425
+ this . state === CHANNEL_STATES . joined &&
426
+ type === REALTIME_LISTEN_TYPES . PRESENCE
427
+ ) {
428
+ this . socket . log (
429
+ 'channel' ,
430
+ `resubscribe to ${ this . topic } due to change in presence callbacks on joined channel`
431
+ )
432
+ this . unsubscribe ( ) . then ( ( ) => this . subscribe ( ) )
433
+ }
420
434
return this . _on ( type , filter , callback )
421
435
}
422
436
/**
@@ -535,10 +549,9 @@ export default class RealtimeChannel {
535
549
if ( ! this . _canPush ( ) ) {
536
550
leavePush . trigger ( 'ok' , { } )
537
551
}
552
+ } ) . finally ( ( ) => {
553
+ leavePush ?. destroy ( )
538
554
} )
539
- . finally ( ( ) => {
540
- leavePush ?. destroy ( )
541
- } )
542
555
}
543
556
/**
544
557
* Teardown the channel.
@@ -649,7 +662,7 @@ export default class RealtimeChannel {
649
662
payload . ids ?. includes ( bindId ) &&
650
663
( bindEvent === '*' ||
651
664
bindEvent ?. toLocaleLowerCase ( ) ===
652
- payload . data ?. type . toLocaleLowerCase ( ) )
665
+ payload . data ?. type . toLocaleLowerCase ( ) )
653
666
)
654
667
} else {
655
668
const bindEvent = bind ?. filter ?. event ?. toLocaleLowerCase ( )
@@ -714,7 +727,6 @@ export default class RealtimeChannel {
714
727
/** @internal */
715
728
_on ( type : string , filter : { [ key : string ] : any } , callback : Function ) {
716
729
const typeLower = type . toLocaleLowerCase ( )
717
-
718
730
const binding = {
719
731
type : typeLower ,
720
732
filter : filter ,
0 commit comments