The goog.pubsub.PubSub Class

goog.pubsub.PubSub
> goog.Disposable

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.

.addOnDisposeCallback(callback, opt_scope)

Inherited from goog.Disposable .

Invokes a callback function when this object is disposed. Callbacks are invoked in the order in which they were added.

callback {!Function}
The callback function.
opt_scope {Object=}
An optional scope to call the callback in.

.clear(opt_topic)

Clears the subscription list for a topic, or all topics if unspecified.

opt_topic {string=}
Topic to clear (all topics if unspecified).

.creationStack {string}

Inherited from goog.Disposable .

If monitoring the goog.Disposable instances is enabled, stores the creation stack trace of the Disposable instance.

.dispose()

Inherited from goog.Disposable .

Disposes of the object. If the object hasn't already been disposed of, calls {@link #disposeInternal}. Classes that extend {@code goog.Disposable} should override {@link #disposeInternal} in order to delete references to COM objects, DOM nodes, and other disposable objects. Reentrant.

returns {void}
Nothing.

.getCount(opt_topic)

Returns the number of subscriptions to the given topic (or all topics if unspecified).

opt_topic {string=}
The topic (all topics if unspecified).
returns {number}
Number of subscriptions to the topic.

.isDisposed()

Inherited from goog.Disposable .

returns {boolean}
Whether the object has been disposed of.

.publish(topic, var_args)

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.

topic {string}
Topic to publish to.
var_args {...*}
Arguments that are applied to each subscription function.
returns {boolean}
Whether any subscriptions were called.

.registerDisposable(disposable)

Inherited from goog.Disposable .

Associates a disposable object with this object so that they will be disposed together.

disposable {goog.disposable.IDisposable}
that will be disposed when this object is disposed.

.subscribe(topic, fn, opt_context)

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}.

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.

.subscribeOnce(topic, fn, opt_context)

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}.

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.

.unsubscribe(topic, fn, opt_context)

Unsubscribes a function from a topic. Only deletes the first match found. Returns a Boolean indicating whether a subscription was removed.

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.

.unsubscribeByKey(key)

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.

key {number}
Subscription key.
returns {boolean}
Whether a matching subscription was removed.