# Class PubSub

- Defined in core/pubsub/pubsub.js:1

#####
Index

### Constructors

constructor

### Properties

disposed

### Methods

clear
dispose
getCount
isDisposed
publish
subscribe
subscribeOnce
unsubscribe
unsubscribeByKey

##
Constructors

### constructor

- new PubSub(opt_async?): 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.

  #### Parameters

  - opt_async: boolean = false

    Enable asynchronous behavior. Recommended for new code. See notes on the publish() method.

  #### Returns PubSub

  - Defined in core/pubsub/pubsub.js:15

##
Properties

### disposed

disposed: boolean

- Defined in core/pubsub/pubsub.js:16
- Defined in core/pubsub/pubsub.js:325

##
Methods

### clear

- clear(opt_topic): void

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

  #### Parameters

  - opt_topic: string

    Topic to clear (all topics if unspecified).

  #### Returns void

  - Defined in core/pubsub/pubsub.js:285

### dispose

- dispose(): void

- #### Returns void

  - Defined in core/pubsub/pubsub.js:322

### getCount

- getCount(opt_topic): number

- Returns the number of subscriptions to the given topic (or all topics if unspecified). This number will not change while publishing any messages.

  #### Parameters

  - opt_topic: string

    The topic (all topics if unspecified).

  #### Returns number

  Number of subscriptions to the topic.

  - Defined in core/pubsub/pubsub.js:304

### isDisposed

- isDisposed(): boolean

- #### Returns boolean

  - Defined in core/pubsub/pubsub.js:318

### publish

- publish(topic, ...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 this object was created with async=true, subscribed functions are called via setTimeout(). Otherwise, the functions are called directly, and if any of them throw an uncaught error, publishing is aborted.

  #### Parameters

  - topic: string

    Topic to publish to.

  - `Rest`...var_args: any[]

    Arguments that are applied to each subscription function.

  #### Returns boolean

  Whether any subscriptions were called.

  - Defined in core/pubsub/pubsub.js:232

### subscribe

- subscribe(topic, fn, opt_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.

  #### Parameters

  - topic: string

    Topic to subscribe to.

  - fn: Function

    Function to be invoked when a message is published to the given topic.

  - opt_context: any = null

    Object in whose context the function is to be called (the global scope if none).

  #### Returns number

  Subscription key.

  - Defined in core/pubsub/pubsub.js:90

### subscribeOnce

- subscribeOnce(topic, fn, opt_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.

  #### Parameters

  - 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: any = null

    Object in whose context the function is to be called (the global scope if none).

  #### Returns number

  Subscription key.

  - Defined in core/pubsub/pubsub.js:125

### unsubscribe

- unsubscribe(topic, fn, opt_context?): boolean

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

  #### Parameters

  - topic: string

    Topic to unsubscribe from.

  - fn: Function

    Function to unsubscribe.

  - opt_context: any = null

    Object in whose context the function was to be called (the global scope if none).

  #### Returns boolean

  Whether a matching subscription was removed.

  - Defined in core/pubsub/pubsub.js:171

### unsubscribeByKey

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

  #### Parameters

  - key: number

    Subscription key.

  #### Returns boolean

  Whether a matching subscription was removed.

  - Defined in core/pubsub/pubsub.js:196