pubsub.PubSub Extends goog.Disposable
Topic-based publish/subscribe channel. Maintains a map of topics to subscriptions. When a message is published to a topic, all functions subscribed to that topic are invoked in the order they were added. Uncaught errors abort publishing. Topics may be identified by any nonempty string, except strings corresponding to native Object properties, e.g. "constructor", "toString", "hasOwnProperty", etc.

Inheritance

Constructor

goog.pubsub.PubSub()

Instance Methods

Public Protected Private
clear(opt_topic)
Clears the subscription list for a topic, or all topics if unspecified.
Arguments:
opt_topic : string=
Topic to clear (all topics if unspecified).
code »
disposeInternal()
No description.
code »
getCount(opt_topic) number
Returns the number of subscriptions to the given topic (or all topics if unspecified).
Arguments:
opt_topic : string=
The topic (all topics if unspecified).
Returns: number  Number of subscriptions to the topic.
code »
publish(topicvar_args) boolean
Publishes a message to a topic. Calls functions subscribed to the topic in the order in which they were added, passing all arguments along. If any of the functions throws an uncaught error, publishing is aborted.
Arguments:
topic : string
Topic to publish to.
var_args : ...*
Arguments that are applied to each subscription function.
Returns: boolean  Whether any subscriptions were called.
code »
subscribe(topicfnopt_context) number
Subscribes a function to a topic. The function is invoked as a method on the given opt_context object, or in the global scope if no context is specified. Subscribing the same function to the same topic multiple times will result in multiple function invocations while publishing. Returns a subscription key that can be used to unsubscribe the function from the topic via #unsubscribeByKey.
Arguments:
topic : string
Topic to subscribe to.
fn : Function
Function to be invoked when a message is published to the given topic.
opt_context : Object=
Object in whose context the function is to be called (the global scope if none).
Returns: number  Subscription key.
code »
subscribeOnce(topicfnopt_context) number
Subscribes a single-use function to a topic. The function is invoked as a method on the given opt_context object, or in the global scope if no context is specified, and is then unsubscribed. Returns a subscription key that can be used to unsubscribe the function from the topic via #unsubscribeByKey.
Arguments:
topic : string
Topic to subscribe to.
fn : Function
Function to be invoked once and then unsubscribed when a message is published to the given topic.
opt_context : Object=
Object in whose context the function is to be called (the global scope if none).
Returns: number  Subscription key.
code »
unsubscribe(topicfnopt_context) boolean
Unsubscribes a function from a topic. Only deletes the first match found. Returns a Boolean indicating whether a subscription was removed.
Arguments:
topic : string
Topic to unsubscribe from.
fn : Function
Function to unsubscribe.
opt_context : Object=
Object in whose context the function was to be called (the global scope if none).
Returns: boolean  Whether a matching subscription was removed.
code »
unsubscribeByKey(key) boolean
Removes a subscription based on the key returned by #subscribe. No-op if no matching subscription is found. Returns a Boolean indicating whether a subscription was removed.
Arguments:
key : number
Subscription key.
Returns: boolean  Whether a matching subscription was removed.
code »
addOnDisposeCallback(callbackopt_scope)
Invokes a callback function when this object is disposed. Callbacks are invoked in the order in which they were added.
Arguments:
callback : function(this:T):?
The callback function.
opt_scope : T=
An optional scope to call the callback in.
code »
dispose() void
Disposes of the object. If the object hasn't already been disposed of, calls #disposeInternal. Classes that extend goog.Disposable should override #disposeInternal in order to delete references to COM objects, DOM nodes, and other disposable objects. Reentrant.
Returns: void  Nothing.
code »
disposeInternal()
Deletes or nulls out any references to COM objects, DOM nodes, or other disposable objects. Classes that extend goog.Disposable should override this method. Not reentrant. To avoid calling it twice, it must only be called from the subclass' disposeInternal method. Everywhere else the public dispose method must be used. For example:
  mypackage.MyClass = function() {
    mypackage.MyClass.base(this, 'constructor');
    // Constructor logic specific to MyClass.
    ...
  };
  goog.inherits(mypackage.MyClass, goog.Disposable);

  mypackage.MyClass.prototype.disposeInternal = function() {
    // Dispose logic specific to MyClass.
    ...
    // Call superclass's disposeInternal at the end of the subclass's, like
    // in C++, to avoid hard-to-catch issues.
    mypackage.MyClass.base(this, 'disposeInternal');
  };
code »
getDisposed() boolean
Use #isDisposed instead. No description.
Returns: boolean  Whether the object has been disposed of.
code »
isDisposed() boolean
No description.
Returns: boolean  Whether the object has been disposed of.
code »
registerDisposable(disposable)
Associates a disposable object with this object so that they will be disposed together.
Arguments:
disposable : goog.disposable.IDisposable
that will be disposed when this object is disposed.
code »

Instance Properties

constructor :
No description.
Code »
key_ :
The next available subscription key. Internally, this is an index into the sparse array of subscriptions.
Code »
pendingKeys_ :
Array of subscription keys pending removal once publishing is done.
Code »
publishDepth_ :
Lock to prevent the removal of subscriptions during publishing. Incremented at the beginning of #publish, and decremented at the end.
Code »
subscriptions_ :
Sparse array of subscriptions. Each subscription is represented by a tuple comprising a topic identifier, a function, and an optional context object. Each tuple occupies three consecutive positions in the array, with the topic identifier at index n, the function at index (n + 1), the context object at index (n + 2), the next topic at index (n + 3), etc. (This representation minimizes the number of object allocations and has been shown to be faster than an array of objects with three key-value pairs or three parallel arrays, especially on IE.) Once a subscription is removed via #unsubscribe or #unsubscribeByKey, the three corresponding array elements are deleted, and never reused. This means the total number of subscriptions during the lifetime of the pubsub channel is limited by the maximum length of a JavaScript array to (2^32 - 1) / 3 = 1,431,655,765 subscriptions, which should suffice for most applications.
Code »
topics_ :
Map of topics to arrays of subscription keys.
Code »
creationStack :
If monitoring the goog.Disposable instances is enabled, stores the creation stack trace of the Disposable instance.
Code »
disposed_ :
Whether the object has been disposed of.
Code »
onDisposeCallbacks_ :
Callbacks to invoke when this object is disposed.
Code »

Static Properties

goog.pubsub.PubSub.superClass_ :
No description.
Code »

Package pubsub

Package Reference