Home

# Class goog.pubsub.PubSub

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.

extends goog.Disposable

Instance Method Summary
| `clear(string= opt_topic)` Clears the subscription list for a topic, or all topics if unspecified.

`disposeInternal()`

Deletes or nulls out any references to COM objects, DOM nodes, or other disposable objects. Classes that extend {@code goog.Disposable} should override this method. Not reentrant. To avoid calling it twice, it must only be called from the subclass' {@code disposeInternal} method. Everywhere else the public {@code dispose} method must be used. For example:

    mypackage.MyClass = function() {
    goog.base(this);
    // Constructor logic specific to MyClass.
    ...
    };
    goog.inherits(mypackage.MyClass, goog.Disposable);

    mypackage.MyClass.prototype.disposeInternal = function() {
    goog.base(this, 'disposeInternal');
    // Dispose logic specific to MyClass.
    ...
    };

| `getCount(string= opt_topic) ⇒ number` Returns the number of subscriptions to the given topic (or all topics if unspecified).
| `publish(string topic, <Any Type> var_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.

`subscribe(string topic, ?Function fn, ?Object= opt_context) ⇒ number`

Subscribes a function to a topic. The function is invoked as a method on the given {@code 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 {@link #unsubscribeByKey}.

| `subscribeOnce(string topic, ?Function fn, ?Object= opt_context) ⇒ number` Subscribes a single-use function to a topic. The function is invoked as a method on the given {@code 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 {@link #unsubscribeByKey}.
| `unsubscribe(string topic, ?Function fn, ?Object= opt_context) ⇒ boolean` Unsubscribes a function from a topic. Only deletes the first match found. Returns a Boolean indicating whether a subscription was removed.
| `unsubscribeByKey(number key) ⇒ boolean` Removes a subscription based on the key returned by {@link #subscribe}. No-op if no matching subscription is found. Returns a Boolean indicating whether a subscription was removed.