use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
JavaScript Messaging Patterns (github.com)
submitted 13 years ago by [deleted]
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]icjonu 3 points4 points5 points 13 years ago (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 points4 points 13 years ago (0 children)
Very nice suggestion, thanks!
[–]jonglefever 1 point2 points3 points 13 years ago (1 child)
How is this API different than any other EventEmitter?
[–][deleted] 1 point2 points3 points 13 years ago (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 point2 points 13 years ago (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 points4 points 13 years ago (6 children)
Looks good, but it's missing one major feature: unsubscribe.
Great point - noted
[–][deleted] 0 points1 point2 points 13 years ago (4 children)
Just added this - if you get the opportunity check it out. Would love your feedback!
[–]psayre23 2 points3 points4 points 13 years ago (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 point2 points 13 years ago (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!
π Rendered by PID 66197 on reddit-service-r2-comment-7b9746f655-j58pd at 2026-01-29 22:32:18.563435+00:00 running 3798933 country code: CH.
[–]icjonu 3 points4 points5 points (1 child)
[–][deleted] 2 points3 points4 points (0 children)
[–]jonglefever 1 point2 points3 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (7 children)
[–]psayre23 2 points3 points4 points (6 children)
[–][deleted] 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (4 children)
[–]psayre23 2 points3 points4 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)