Visit Mozilla.org

nsIObserver

From MDC

« XPCOM API Reference

Contents

[edit] Summary

nsIObserver is implemented by an object that wishes to observe notifications. These notifications are often, though not always, broadcast via the nsIObserverService.

nsIObserver is defined in xpcom/ds/nsIObserver.idl. It is scriptable and has been frozen since Mozilla 0.9.6.

[edit] Interface Code

[scriptable, uuid(DB242E01-E4D9-11d2-9DDE-000064657374)]
interface nsIObserver : nsISupports {
    void observe( in nsISupports aSubject,
                  in string      aTopic,
                  in wstring     aData );
};

[edit] Methods

[edit] observe

    void observe( in nsISupports aSubject,
                  in string      aTopic,
                  in wstring     aData );

observe will be called when there is a notification for the topic that the observer has been registered for.

In general, aSubject reflects the object whose change or action is being observed, aTopic indicates the specific change or action, and aData is an optional parameter or other auxiliary data further describing the change or action.

The specific values and meanings of the parameters provided varies widely, though, according to where the observer was registered, and what topic is being observed.

A single nsIObserver implementation can observe multiple types of notification, and is responsible for dispatching its own behaviour on the basis of the parameters for a given callback. In general, aTopic is the primary criterion for such dispatch; nsIObserver implementations should take care that they can handle being called with unknown values for aTopic.

While some observer-registration systems may make this safe in specific contexts, it is generally recommended that observe implementations not add or remove observers while they are being notified.

[edit] Related Interfaces

nsIObserverService

[edit] Example Code

The following code is an implementation of nsIObserver that is registered to receive notifications for the "myTopicID" topic. See Global Notifications at XULPlanet for a list of built-in topics possible.

Observing preferences works slightly differently. See Code snippets:Preferences - Using preference observers for an example.

function myObserver()
{
  this.register();
}
myObserver.prototype = {
  observe: function(subject, topic, data) {
     // Do your stuff here.
  },
  register: function() {
    var observerService = Components.classes["@mozilla.org/observer-service;1"]
                          .getService(Components.interfaces.nsIObserverService);
    observerService.addObserver(this, "myTopicID", false);
  },
  unregister: function() {
    var observerService = Components.classes["@mozilla.org/observer-service;1"]
                            .getService(Components.interfaces.nsIObserverService);
    observerService.removeObserver(this, "myTopicID");
  }
}

Instantiation - this should be fired once you're ready to start observing (ex: a window's load event).

observer = new myObserver();

Destruction - this should be fired once you're done observing (ex: a window's unload event). Failure to do so may result in memory leaks.

observer.unregister();