PDA

View Full Version : Ext.ux.Sprocket.PubSub - Hierarchical PubSub



xantus
16 Dec 2008, 11:31 AM
Ext.ux.Sprocket.PubSub (http://xant.us/ext-ux/lib/Sprocket/PubSub.js) is channel based hierarchical PubSub for Extjs

For conveince, any subclass of Ext.util.Observable will have 3 methods:


this.subscribe( '/foo/bar', this.fooEvent, this );
subscribes to a channel, returns nothing
this.publish( '/foo/bar', { data:'blah' } );
returns true if the event was delivered and, false if it was discarded (no subscribers)
this.removeSubscribers( '/foo/bar' );
clears a channel, returns nothingExt.ux.Sprocket.PubSub is a singleton, so you can use it directly to publish or subscribe to events:


Ext.ux.Sprocket.PubSub.publish( '/foo', { bar: 1 } );Sprocket.PubSub will walk the tree delivering the event to / then /foo, then /foo/bar
Non hierarchical events are supported too, so 'foobar' will work just fine.

I'm using this in Comet Desktop for events like /desktop/sound/play and /desktop/notify

You can use this Comet pubsub too. Cometd, Meteor, Orbited, Lightstreamer, etc

The license is BSD

Comments, or suggestions?

AndreTheDiminutive
5 Jun 2009, 4:47 PM
This is exactly what I've been looking for.

Thanks!!!

xantus
9 Jun 2009, 1:39 PM
BTW, This is used by ZipWhip. I'm mentioned in the article:

http://extjs.com/blog/2009/05/27/implementation-spotlight-zipwhip-and-ext-js/

mike.slattery
7 Aug 2009, 6:41 AM
Love to see Perl and Extjs used together. Do you have any more written about how it is used, from the server side out? I've checked your site with the Desktop etc, looks great.

xantus
7 Aug 2009, 11:52 AM
This is a client side pubsub. It can be used with a server side pubsub with comet techniques and some imagination.

Here's an example.

You have a flash based sound system like soundmanger2, and you have written a wrapper around it with Ext. You can use a pubsub channel of /soundman and have other components publish to it when they need to play a sound. Now, what if the sound manager isn't loaded or flash isn't supported? Everything will still work (except no sounds are played) because all of your components aren't relying on an object to fire methods on, just a channel to publish to. This is what I have done with comet desktop.

mike.slattery
7 Aug 2009, 7:45 PM
Thanks for the clarification.

crp_spaeth
9 Aug 2009, 11:21 PM
Hi xantus,
neat little implementation... Do you have some abstract tips like a best practice tutorial for the usage of pubsub? It's not that i dont know how to integrate an use our extension but it would be really helpful to have a kind of clarification when to use our PubSub thing!

regards martin

SmyersM
15 Aug 2009, 10:34 AM
The best way to make an application. Hands down. Makes managing a huge project manageable.

-Michael

cginzel
6 Jan 2010, 6:39 PM
I've been using PubSub and I ran into an interesting bit of code where I found that my registered functions were left over... I know! I should have been more careful, but I've added the following to the subscribe functions making it behave like the mon() function.


subscribe: function( eventName, fn, scope, o ) {
Ext.ux.Sprocket.PubSub.addEvents( eventName );
Ext.ux.Sprocket.PubSub.on( eventName, fn, scope, o);

if (this.on) {
// on requires the object to have an event array
this.events = this.events || [];

this.on('destroy', function( eventName, fn, object){
Ext.ux.Sprocket.PubSub.un( eventName, fn, object );
}.createDelegate(this, [eventName, fn, scope]));
}
},

rualatngua
7 Aug 2010, 11:51 PM
I've been using PubSub and I ran into an interesting bit of code where I found that my registered functions were left over... I know! I should have been more careful, but I've added the following to the subscribe functions making it behave like the mon() function.


subscribe: function( eventName, fn, scope, o ) {
Ext.ux.Sprocket.PubSub.addEvents( eventName );
Ext.ux.Sprocket.PubSub.on( eventName, fn, scope, o);

if (this.on) {
// on requires the object to have an event array
this.events = this.events || [];

this.on('destroy', function( eventName, fn, object){
Ext.ux.Sprocket.PubSub.un( eventName, fn, object );
}.createDelegate(this, [eventName, fn, scope]));
}
},

That's great and save my life. Thanks you xantus and cginzel!!!