Bug Tracker

Ticket #7547 (closed enhancement: plugin)

Opened 4 years ago

Last modified 3 years ago

Adding pub/sub into jQuery core?

Reported by: cowboy Owned by:
Priority: low Milestone: 1.7
Component: event Version: 1.4.4
Keywords: Cc:
Blocking: Blocked by:

Description

Using the native events system, we can add a fairly robust Pub/Sub implementation into jQuery in under 200 bytes.

 https://gist.github.com/661855

Here are my comments from that gist:

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 .bind(), .unbind() and .trigger() methods internally, those methods' complete signatures are available to you.

This includes being able to subscribe to multiple topics simultaneously as well as specify data at both the time of subscription as well as at the time of publishing. It allows you to use namespaces for more control over unsubscribing and publishing. It even gives each bound callback an event object with many useful properties, like .type (topic) and .timeStamp.

Just use this handy terminology guide (jQuery events term → Pub/Sub term), and everything should make sense:

  • bind → subscribe
  • unbind → 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( event, a, b, c ) {
  console.log( event.type, a + b + c );
};

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

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

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

// Or:

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

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

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

Change History

comment:1 follow-up: ↓ 2 Changed 4 years ago by ajpiano

I know that this conversation will largely turn to implementation questions, but before that happens, I would just like to voice my support for this. When jQuery adds "Good Ideas" like pubsub - or event delegation - into core, it encourages and enlightens broad swathes of developers to techniques and patterns that improve their code and their skill as developers. +1

comment:2 in reply to: ↑ 1 Changed 4 years ago by peol

Replying to ajpiano:

I know that this conversation will largely turn to implementation questions, but before that happens, I would just like to voice my support for this. When jQuery adds "Good Ideas" like pubsub - or event delegation - into core, it encourages and enlightens broad swathes of developers to techniques and patterns that improve their code and their skill as developers. +1

I agree wholeheartedly. It's also lightweight and follows the usefulness that is jQuery overall.

comment:3 Changed 4 years ago by shadedecho

+1 for adding pub/sub to jquery core, however the underlying implementation ends up being.

comment:4 Changed 4 years ago by addyosmani

+1 I completely agree that this would be an excellent addition.

comment:5 Changed 4 years ago by rwaldron

+1 to simple, minimal overhead publish/subscribe systems.

comment:6 Changed 4 years ago by rwaldron

  • Priority changed from undecided to low
  • Component changed from unfiled to event

comment:7 follow-up: ↓ 9 Changed 4 years ago by SlexAxton

Would this have the behavior of splitting up publishes and subscribes with spaces in them?

$.subscribe('something here', func(){});

would actually listen for 'something' and 'here' separately and then subsequently fire the handler twice whenever it's published.

Is this (al)right?

comment:8 Changed 4 years ago by snover

  • Status changed from new to open
  • Milestone 1.5 deleted

comment:9 in reply to: ↑ 7 Changed 4 years ago by peol

Replying to SlexAxton:

Would this have the behavior of splitting up publishes and subscribes with spaces in them?

$.subscribe('something here', func(){});

would actually listen for 'something' and 'here' separately and then subsequently fire the handler twice whenever it's published.

Is this (al)right?

I would say so, yes. If it's documented. Worst case scenario, $.subscribe and $.publish could simply handle whitespace, replacing it with underscores or somesuch.

comment:10 Changed 4 years ago by rwaldron

  • Milestone set to 1.5

comment:11 Changed 4 years ago by danheberden

  • Milestone set to 1.6

comment:12 Changed 4 years ago by anonymous

You already have pub/sub in jQuery, just the syntax is different. Bind/Trigger is not limited to click events and the likes, just specify what you like..

jQuery(document).bind("/myEvent", function(){}); jQuery(document).triggerHandler("/myEvent");

comment:13 Changed 4 years ago by john

  • Status changed from open to closed
  • Resolution set to wontfix

This was discussed in the jQuery 1.6 roadmap meeting and we agreed that this was not something that we were going to put into core (leave as a plugin).

comment:14 Changed 3 years ago by timmywil

  • Status changed from closed to reopened
  • Resolution wontfix deleted
  • Milestone 1.6 deleted

With the introduction of $.Callbacks() in 1.7, jaubourg's implementation of pub/sub in  https://github.com/jquery/jquery/commit/96b0089c9ce5c65f9d002966256d90e778345a2a seems a more plausible solution if pub/sub was going to be added to core. However, this could also easily be a new plugin (even an official one), which is where I am leaning at this point. Thoughts?

comment:15 Changed 3 years ago by timmywil

  • Keywords needsreview added
  • Status changed from reopened to open
  • Milestone set to 1.7

comment:16 Changed 3 years ago by daniel.c.herman@…

+1, I'd love to see this in jQuery.

comment:17 Changed 3 years ago by timmywil

  • Keywords needsreview removed
  • Status changed from open to closed
  • Resolution set to plugin

We've decided to implement this as a plugin for now.

comment:18 Changed 3 years ago by addyosmani

Just posting a reminder for us to include a link to the plugin repo/gist when we get a chance.

Note: See TracTickets for help on using tickets.