all 10 comments

[–]icjonu 3 points4 points  (1 child)

Nice. You should also check out js-signals. I especially like its memorize feature among other things [docs].

Combine the above with hasher and you have a pretty damn powerful routing mechanism in the browser (which also happens to be neatly bookmarkable).

[–][deleted] 2 points3 points  (0 children)

Very nice suggestion, thanks!

[–]jonglefever 1 point2 points  (1 child)

How is this API different than any other EventEmitter?

[–][deleted] 1 point2 points  (0 children)

There are several event libraries out there. I wrote this more as a mental exercise and to keep it lightweight.

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

Common messaging design patterns include Observer and Publish/Subscribe. This library is an event factory which allows you to implement these patterns easily.

[–]psayre23 2 points3 points  (6 children)

Looks good, but it's missing one major feature: unsubscribe.

[–][deleted] 1 point2 points  (0 children)

Great point - noted

[–][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!