observer-service

The observer-service module provides access to the application-wide observer service singleton.

For a list of common observer topics across a variety of Mozilla-based applications, see the MDC page on Observer Notifications.

Observer Callbacks

Observer callbacks are functions of the following form:

function callback(subject, data) {
  /* Respond to the event notification here... */
}

In the above example, subject is any JavaScript object, as is data. The particulars of what the two contain are specific to the notification topic.

API Reference

Functions

add(topic, callback, thisObject)

Adds an observer callback to be triggered whenever a notification matching the topic is broadcast throughout the application.

topic : string

The topic to observe.

callback : function,object

Either a function or an object that implements nsIObserver. If a function, then it is called when the notification occurs. If an object, then its observe() method is called when the notification occurs.

[ thisObject : object ]

An optional object to use as this when a function callback is called.

remove(topic, callback, thisObject)

Unsubscribes a callback from being triggered whenever a notification matching the topic is broadcast throughout the application.

topic : string

The topic being observed by the previous call to add().

callback : function,object

The callback subscribed in the previous call to add(), either a function or object.

[ thisObject : object ]

If thisObject was passed to the previous call to add(), it should be passed to remove() as well.

notify(topic, subject)

Broadcasts a notification event for a topic, passing a subject and data to all applicable observers in the application.

topic : string

The topic about which to broadcast a notification.

[ subject : value ]

Optional information about the topic. This can be any JS object or primitive. If you have multiple values to pass to observers, wrap them in an object, e.g., { foo: 1, bar: "some string", baz: myObject }.