Kcko
3/5/2017 - 8:47 AM

jQuery Tiny Pub/Sub: A really, really, REALLY tiny pub/sub implementation for jQuery.

jQuery Tiny Pub/Sub: A really, really, REALLY tiny pub/sub implementation for jQuery.

/* jQuery Tiny Pub/Sub - v0.7 - 10/27/2011
 * http://benalman.com/
 * Copyright (c) 2011 "Cowboy" Ben Alman; Licensed MIT, GPL */
(function(a){var b=a({});a.subscribe=function(){b.on.apply(b,arguments)},a.unsubscribe=function(){b.off.apply(b,arguments)},a.publish=function(){b.trigger.apply(b,arguments)}})(jQuery)
/* jQuery Tiny Pub/Sub - v0.7 - 10/27/2011
 * http://benalman.com/
 * Copyright (c) 2011 "Cowboy" Ben Alman; Licensed MIT, GPL */

(function($) {

  var o = $({});

  $.subscribe = function() {
    o.on.apply(o, arguments);
  };

  $.unsubscribe = function() {
    o.off.apply(o, arguments);
  };

  $.publish = function() {
    o.trigger.apply(o, arguments);
  };

}(jQuery));

This is now an actual repo:

https://github.com/cowboy/jquery-tiny-pubsub

This is the Original jQuery Tiny Pub/Sub plugin updated for jQuery 1.7 (which makes it EVEN SMALLER because bind and unbind are replaced with on and off)

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:

on → subscribe off → unsubscribe trigger → publish type → topic 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.

// Super-basic example:

function handle(e, a, b, c) { // e is the event object, you probably don't care about it. console.log(a + b + c); };

$.subscribe("/some/topic", handle);

$.publish("/some/topic", [ "a", "b", "c" ]); // logs: abc

$.unsubscribe("/some/topic", handle); // Unsubscribe just this handler

// Or:

$.subscribe("/some/topic", function(e, a, b, c) { console.log(a + b + c); });

$.publish("/some/topic", [ "a", "b", "c" ]); // logs: abc

$.unsubscribe("/some/topic"); // Unsubscribe all handlers for this topic