Stop listening to channel.here(), .joining() and leaving()
See original GitHub issueOriginally commented this in #51, but this deserves its own issue imo.
I want to stop listening to the pusher:member_added
and pusher:member_removed
events that are registered by channel.joining()
and channel.leaving()
. I tried channel.stopListening('pusher:member_added', callback);
and some other things, but this doesn’t seem to work. I also don’t want to run channel.leave()
and channel.join()
every time my page rerenders.
It seems like this is being caused by the here
, joining
and leaving
methods not using the actual callback you pass into it (like the listenForWhisper
method does), but instead creating one for itself, making it impossible to remove it afterwards from the other side of the library:
https://github.com/laravel/echo/blob/b583f2641b51e7b4ab39c5bb03626be8add47722/src/channel/pusher-presence-channel.ts#L22-L28
Can anyone think of a non-breaking fix for this?
I can only think of these (breaking) changes:
This changes the return type from this
to a function you can call to remove the event listener:
here(callback: Function): PusherPresenceChannel {
function removableCallback(data) {
callback(Object.keys(data.members).map((k) => data.members[k]));
}
this.on('pusher:subscription_succeeded', removableCallback);
return () => this.stopListening('pusher:subscription_succeeded', removableCallback);
}
This moves the burden of formatting the callback’s payload to the user, but it would allow us to run channel.stopListening('pusher:subscription_succeeded', callback)
(not taking into consideration the fact that listening
and stopListening
seem to format the listener keys 👀 ):
here(callback: Function): PusherPresenceChannel {
this.on('pusher:subscription_succeeded', callback);
return this;
}
_Originally posted by @AdrianMrn in https://github.com/laravel/echo/issues/51#issuecomment-850483993_
Issue Analytics
- State:
- Created 2 years ago
- Comments:10 (7 by maintainers)
Top GitHub Comments
Going to close this one as it’s best to follow up in the PR. Feel free to send it in when you can.
@AdrianMrn so yes feel free to attempt a pr which you feel is best 👍