-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Use a set instead of an array to track clients #806
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
Conversation
I agree that this should be the case. However, not before the next major version. In fact, ideally, we make a subclass of |
There are already quite a few breaking changes in master.
|
Was about to point out that now that we're using |
Okay, then my next concern it with my comment above: |
Why is a subclass needed? |
I suppose a subclass specifically isn't needed. But the non-readonly methods should be stubbed nonetheless. Otherwise unsuspecting users (people who think the returned |
@JoshuaWise I don't get it, mind to write a code example of the issue? |
wss.on('connection', function (client) {
wss.clients.delete(client); // I shouldn't be allowed to do this.
}); |
It's very common for people to expect "safe copies" of arrays or sets. Meaning, they expect that the object that is returned will be a copy of the internally-kept original one, which allows them to modify it or do whatever they want with it. However, in our case, this is not the case. The |
I don't see it that way. I think all is needed is documentation. If the end users want to mess with the set they should be free to do so as long as it is a public API. It is already like this with the array. |
When would the user ever want to actually mess with it? I know that's how it is with the array—I see it as an oversight that should be fixed |
I can't think of a use case atm but I also can't see why it should be forbidden. |
Once it's rebased id say it's clear for merging. |
As for the deletion, people can already do that in the current way it's setup as well. Just |
lib/WebSocketServer.js
Outdated
this.clients[i].terminate(); | ||
let error = null; | ||
|
||
if (this.options.clientTracking) { |
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.
Why not check if this.clients
is set instead of checking for the options?
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 don't even remember these changes 😄. I'll take a look!
lib/WebSocketServer.js
Outdated
|
||
if (this.options.clientTracking) { | ||
try { | ||
for (const client of this.clients) client.terminate(); |
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.
The only change i'd make here is maybe move the try/catch to the inside of the loop so if one client fails to terminate we still try to kill the rest of them.
I think it makes sense to use a
Set
instead of anArray
to track clients.The main advantage is that
Set.prototype.delete()
complexity is sublinear per spec.