# Class PubSub

##### Index

### Constructors

constructor

### Properties

disposed

### Methods

clear
dispose
getCount
isDisposed
publish
subscribe
subscribeOnce
unsubscribe
unsubscribeByKey

## Constructors

### constructor

- new PubSub(async?: boolean): 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

  - async: boolean = false

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

  #### Returns PubSub

## Properties

### disposed

disposed: boolean

## Methods

### clear

- clear(opt_topic: string): 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

### dispose

- dispose(): void

  #### Returns void

### getCount

- getCount(opt_topic: string): 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.

### isDisposed

- isDisposed(): boolean

  #### Returns boolean

### publish

- publish(topic: string, ...var_args: any[]): 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 Promise.resolve(). 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.

  - ...var_args: any[]

    Arguments that are applied to each subscription function.

  #### Returns boolean

  Whether any subscriptions were called.

### subscribe

- subscribe(topic: string, fn: Function, opt_context?: any): 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.

### subscribeOnce

- subscribeOnce(topic: string, fn: Function, opt_context?: any): 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.

### unsubscribe

- unsubscribe(topic: string, fn: Function, opt_context?: any): 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.

### unsubscribeByKey

- unsubscribeByKey(key: number): 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.