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

Fixed case where an error occurs if a handle unsubscribes in the middle of a publish #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

bentruyman
Copy link

You can see where it errors here:
http://jsfiddle.net/EG4CL/

This is happening due to the way jQuery's each method is implemented. In this case, jQuery caches the length property in the for loop it uses to iterate over the items. When splice is used, that array (of course) is shortened but jQuery is still going to try to keep iterating using the original length.

When that happens (in jQuery), this becomes window and not the callback the user wants:

cache[topic] && d.each(cache[topic], function(){
  this.apply(d, args || []);
});

@bentruyman
Copy link
Author

Basically, the fix I came up with is to lookup the length property of the callbacks array every iteration in the for loop.

@phiggins42
Copy link
Owner

half tempted to just rip $.each use out of there and for(;;;) that shit.

@eladzlot
Copy link

There still is a problem with this solution.
You prevent the said errors, but one of the subscription may not fire.
Say cache[topic][0] removes itself. on the next iteration of the loop cache[topic][1] will already be cache[topic][0] and therefore it will not fire.

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

Successfully merging this pull request may close these issues.

3 participants