goog.pubsub.PubSub
Provided By | |
---|---|
Extends | |
All Implemented Interfaces |
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.
new PubSub( opt_async )
Parameters |
|
---|
Instance Methods
this.addOnDisposeCallback<T>( callback, opt_scope ) → void
void
Invokes a callback function when this object is disposed. Callbacks are invoked in the order in which they were added. If a callback is added to an already disposed Disposable, it will be called immediately.
Defined by | |||||||||
---|---|---|---|---|---|---|---|---|---|
Parameters |
|
this.clear( opt_topic ) → void
void
Clears the subscription list for a topic, or all topics if unspecified.
Parameters |
|
---|
this.dispose() → ?
?
Disposes of the object and its resources.
Overrides | ||
---|---|---|
Specified by | ||
Parameters | None. | |
Returns |
|
this.disposeInternal() → void
void
Performs appropriate cleanup. See description of goog.disposable.IDisposable
for examples. 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'); };
Overrides | |
---|---|
Parameters | None. |
this.getCount( opt_topic ) → number
number
this.getDisposed() → boolean
boolean
warning Deprecated | Use |
---|
Defined by | |||
---|---|---|---|
Parameters | None. | ||
Returns |
|
this.isDisposed() → boolean
boolean
Overrides | |||
---|---|---|---|
Specified by | |||
Parameters | None. | ||
Returns |
|
this.publish( topic, ...var_args ) → boolean
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 this object was created with async=true, subscribed functions are called via goog.async.run(). Otherwise, the functions are called directly, and if any of them throw an uncaught error, publishing is aborted.
Parameters |
| ||||||||
---|---|---|---|---|---|---|---|---|---|
Returns |
|
this.registerDisposable( disposable ) → void
void
Associates a disposable object with this object so that they will be disposed together.
Defined by | |||||
---|---|---|---|---|---|
Parameters |
|
this.subscribe( topic, fn, opt_context ) → number
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
.
Parameters |
| ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Returns |
|
this.subscribeOnce( topic, fn, opt_context ) → number
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
.
Parameters |
| ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Returns |
|
this.unsubscribe( topic, fn, opt_context ) → boolean
boolean
Unsubscribes a function from a topic. Only deletes the first match found. Returns a Boolean indicating whether a subscription was removed.
Parameters |
| ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Returns |
|
this.unsubscribeByKey( key ) → boolean
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.
Parameters |
| ||||
---|---|---|---|---|---|
Returns |
|
Instance Properties
this.creationStack → (string|undefined)
(string|undefined)
If monitoring the goog.Disposable instances is enabled, stores the creation stack trace of the Disposable instance.