Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
@edsilv/jquery-tiny-pubsub
Advanced tools
A really, really, REALLY tiny pub/sub implementation for jQuery.
Readme
A really, really, REALLY tiny pub/sub implementation for jQuery.
(see the original gist)
Download the production version or the development version.
Example usage:
<script src="jquery.js"></script>
<script src="dist/tiny-pubsub.min.js"></script>
<script>
// Creates a "named" logging function.
function createLogger(name) {
return function(_, a, b) {
// Skip the first argument (event object) but log the name and other args.
console.log(name, a, b);
};
}
// Subscribe to the "foo" topic (bind to the "foo" event, no namespace).
$.subscribe('foo', createLogger('foo'));
// Subscribe to the "foo.bar" topic (bind to the "foo" event, "bar" namespace).
$.subscribe('foo.bar', createLogger('foo.bar'));
// Subscribe to the "foo.baz" topic (bind to the "foo" event, "baz" namespace).
$.subscribe('foo.baz', createLogger('foo.baz'));
// Publish arbitrary values.
$.publish('foo', [1, 2]);
// logs:
// foo 1 2
// foo.bar 1 2
// foo.baz 1 2
$.publish('foo.bar', [3, 4]);
// logs:
// foo.bar 3 4
$.publish('foo.baz', [5, 6]);
// logs:
// foo.baz 5 6
$.unsubscribe('foo.bar');
$.publish('foo', [7, 8]);
// logs:
// foo 7 8
// foo.baz 7 8
</script>
Note: Ignore the first argument passed to your subscribed callbacks (the jQuery event object).
Another Note: Previous versions (v0.4+) were written in an attempt to remove the first argument and create a more future-proof API, but unfortunately this resulted in much less elegant, larger and slower code. The point of this plugin is to be TINY, to be used in situations where only size (not performance or usability) is the primary concern (tweets, code golf, etc).**
I frequently see comments about how jQuery's events system has unnecessary overhead that precludes it from being used as the core of a Pub/Sub implementation. The jQuery events system is tried-and-true, having been architected to be both fast and robust, and the vast majority of users of this plugin should never encounter any kind of performance issues.
Because this plugin's $.subscribe
, $.unsubscribe
and $.publish
methods all use the jQuery .on(), .off() and .trigger() methods internally, those methods' complete signatures are available to you.
You can use namespaces for more control over unsubscribing and publishing.
Just use this handy terminology guide (jQuery events term → Pub/Sub term), and everything should make sense:
In addition, should you need it, these methods are fully compatible with the jQuery.proxy() method, in case you not only want more control over to which context the subscribed callback is bound, but want to be able to very easily unsubscribe via callback reference.
Regarding performance: If at some point, your application starts processing so many messages that performance issues start to develop, you could always write a replacement "jQuery Not-So-Tiny Pub/Sub" plugin with the same API and just drop it in as a replacement to this plugin. But keep in mind that you'll also need to add in the aforementioned features, too.
2013-01-29 - v0.7.0 - First official release.
FAQs
A really, really, REALLY tiny pub/sub implementation for jQuery.
The npm package @edsilv/jquery-tiny-pubsub receives a total of 12 weekly downloads. As such, @edsilv/jquery-tiny-pubsub popularity was classified as not popular.
We found that @edsilv/jquery-tiny-pubsub demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.