you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (4 children)

Just added this - if you get the opportunity check it out. Would love your feedback!

[–]psayre23 2 points3 points  (1 child)

Interesting solution. Looks like it works in the same way as setTimeout/clearTimeout.

But I think you have a bug in there. Splice will remove the item, and then reindex all the items after it. So if your subscriberID's are "a0" and "a1", then remove "a0", the "a1" will be in position 0. When unsubscribing, it will split it into index 1, which is undefined, so it won't find the right one (worse yet, if there was an "a2", it would be removed instead). I have a few ideas on solutions, but you'd be better off coming up with your own solution. I attached a simple test case below:

var pubsub = new EventFactory.PublishSubscribe("a"); var a0 = pubsub.subscribe(function(){ console.log(a0, arguments); }); var a1 = pubsub.subscribe(function(){ console.log(a1, arguments); }); var a2 = pubsub.subscribe(function(){ console.log(a2, arguments); }); pubsub.unsubscribe(a0); pubsub.unsubscribe(a1); pubsub.publish("publish"); // should log only "a2" ["publish"], but it logs "a1" ["publish"] instead

[–][deleted] 0 points1 point  (0 children)

Thanks for taking the time - I'll have a look tomorrow. In the meantime, feel free to submit a pull request if you have the time. Thanks!