-
Notifications
You must be signed in to change notification settings - Fork 185
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add optional callback argument to stopListening() #292
Conversation
9af4eaf
to
874e2bb
Compare
What is an event and what is a "channel event"? |
The order of the methods in the PR seems all weird. Can the methods be placed in a more logical order? |
6aa8aed
to
b880fb8
Compare
I have changed the PR now, so there are two props on SocketIoChannel:
I have now split the PR in two commits, which has simplified the SocketIoChannel by moving some responsibility to SocketIoConnector. The result is that there is just one new method at the end of SocketIoChannel. I hope this makes the changes I've made more clear. |
The 'reconnect' event is not sent on a channel, but on the socket, so it makes more sense to deal with it on a socket-wide basis instead of on a channel-by-channel basis.
This makes it possible to unregister a single callback for an event, without unregistering all other callbacks for that event on that channel.
callback(data); | ||
} | ||
}; | ||
on(event: string, callback: Function): SocketIoChannel { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed the return type of this method to be analogous to PusherChannel.on()
. I'm not sure if that was a good idea or not.
I guess I'm confused - why this difference between events and listeners is even needed at all? Does Echo let you listen to events with a given name across all channels? |
I think I understand the reasoning now. I have released this as Echo 1.10.0. Thanks. |
(Second attempt, see #291)
This makes it possible to unregister a single callback for an event, without unregistering all other callbacks for that event on that channel. This is very helpful for example when using react hooks, where you need to stop listening as the component is unmounted. Here's an example React component using this new functionality:
Prior to this PR it was not possible to unregister a callback for an event. This is because the callback was wrapped inside the channel class to filter out events from other channels (see
echo/src/channel/socketio-channel.ts
Line 112 in 8bd9a2b
channel.listen()
to the callback which was actually registered with the socket. This in turn meant it was not possible to selectively remove just that callback from the socket.I have solved this by registering just one callback per event, which in turn iterates over a list of user-provided callbacks. The list of user-provided callbacks can therefore be modified, and a single user-provided callback can be selectively unregistered.
I've added a few tests for the socket.io channel because the new code is a bit more complex than it used to be.
I have tried to not introduce any backwards incompatible changes here. That's why there's still an
channel.on()
function, despite it just proxying to the newchannel.bindChannelEvent
. All new functions has been marked asprotected
to discourage consumers of this package to call them directly. New properties have been marked as private.This PR also fixes a bug in the socket.io channel, where calling
stopListening(event)
would stop the socket from listening to that event for all channels, not just the current one. If you look at this line:echo/src/channel/socketio-channel.ts
Line 84 in 8bd9a2b