Using observers: Difference between revisions

From MozillaZine Knowledge Base
Jump to navigationJump to search
(→‎References: the page about xul:observes is not related to this)
m (add note about threading.)
Line 44: Line 44:
         .notifyObservers(null, "myTopicID", "someAdditionalInformationPassedAs'Data'Parameter");
         .notifyObservers(null, "myTopicID", "someAdditionalInformationPassedAs'Data'Parameter");
</pre>
</pre>
==Advanced Topics==
The observer service should only be used from the main (UI) thread.


==References==
==References==

Revision as of 13:12, 5 March 2006

This page is part of the extension development documentation project.

Ask your questions in MozillaZine Forums. Also try browsing example code.

Note: development documentation is in process of being moved to Mozilla Development Center (MDC).

An extension overlaying the Firefox browser window will have one "instance" for each window, that is completely separate from all other windows—you cannot share variables. There are several ways to implement cross-window communication—probably the most convenient one is using observers (see also XulPlanet article).

There are two interfaces in the Mozilla platform related to observers system: nsIObserverService which is available as a service, and nsIObserver, which is what you'll need to implement.

Observer object

Each observer implements nsIObserver interface. It may register for one or more topics. For each notification, its observe function is called with the subject (can be null), the topic and additional data. All three parameters can be freely used by the code invoking the notification, see "Notify all observers" below.

In the snippet below we also include register and unregister convenience functions, which are not a part of nsIObserver interface. Including these functions in the observer object is a matter of taste.

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");
  }
}

Instantiate and register

Do the following in your init function (probably an onload handler) for each window:

var observer = new myObserver();

Note: myObserver's register method is called once in the constructor, so no need to call it manually.

You must also unregister your observer when the window is closed (probably in an onunload handler or earlier). Failing to do so will leak the window and cause various errors.

Notify all observers

You can send a notifications to all observers using nsIObserverService.notifyObservers():

Components.classes["@mozilla.org/observer-service;1"]
         .getService(Components.interfaces.nsIObserverService)
         .notifyObservers(null, "myTopicID", "someAdditionalInformationPassedAs'Data'Parameter");

Advanced Topics

The observer service should only be used from the main (UI) thread.

References