I am using Peter Higgins pubsub library and I ran into an interesting problem:
If a specific publish event is used to unsubscribe from itself more than once I get an error on line 33 of pubsub TypeError: this.apply is not a function.
The reason that this happens is that the cache[topic] array changes its length on the first subscription to be handled, and therefore when pubsub tries to handle the second subscription it cannot find the correct function.
Here is the code needed to reproduce (you need pubsub and jquery too of course)
var handle1 = $.subscribe('pubsub',function(){
$.unsubscribe(handle1);
});
var handle2 = $.subscribe('pubsub',function(){
$.unsubscribe(handle2);
});
$.publish('pubsub');
Does anybody have an idea for a solution or workaround?
I wrote a fix of my own for this on github.
hope it helps someone.
https://github.com/eladzlot/bloody-jquery-plugins/blob/master/pubsub.js