Constructors

  • 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

Properties

disposed: boolean

Methods

  • 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

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

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

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

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

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

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