Skip to content
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 .once #103

Closed
rauchg opened this issue Mar 7, 2011 · 4 comments
Closed

Add .once #103

rauchg opened this issue Mar 7, 2011 · 4 comments

Comments

@rauchg
Copy link
Contributor

rauchg commented Mar 7, 2011

No description provided.

@3rd-Eden
Copy link
Contributor

3rd-Eden commented Mar 8, 2011

I can understand the need of a .once event listener but there's a really big downside of this method. Once you attach a .once listener there is no way of removing it. Because the supplied function is wrapped in a closure and added to the .events queue. If you want to remove the event you would need to have the exact function to match it.

That's the reason why I removed .once from my reconnect example, because there is no clear way to remove them. Node.js suffers from the same limitation.

@3rd-Eden
Copy link
Contributor

3rd-Eden commented Mar 8, 2011

I can probably create a small walk around for this by adding a reference to the original code to the function. Than it would allow users to also remove the .once event using removeEvent..

For example:
Socket.prototype.once = function(name, fn){
var self = this
, once = function(){
self.removeEvent(name, once);
fn.apply(self, arguments);
};
once.reference = fn;
self.on(name, once);
return this;
};

Socket.prototype.removeEvent = function(name, fn){
  if (name in this.events){
    for (var a = 0, l = this.events[name].length; a < l; a++)
      if (this.events[name][a] == fn || this.events[name][a].reference && this.events[name][a].reference == fn) this.events[name].splice(a, 1);    
  }
  return this;
};

rauchg pushed a commit that referenced this issue May 13, 2011
… to also have the .once removed as it's wrapped in a function. Anyways this alone is enough to close bug #103 . Because we now have a once listener we can easily fix #38
@rauchg
Copy link
Contributor Author

rauchg commented May 13, 2011

Fixed

@rauchg rauchg closed this as completed May 13, 2011
@JuanAntelo
Copy link

Does this send a message once then close the socket or open only one socket?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants