MozillaZine

Using observers

From MozillaZine Knowledge Base

(Difference between revisions)
Revision as of 00:53, 27 November 2004
Asqueella (Talk | contribs)
(add links to Xulplanet docs)
<-- Previous diff
Revision as of 01:08, 27 November 2004
Asqueella (Talk | contribs)

Next diff -->
Line 1: Line 1:
-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.+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 [http://xulplanet.com/tutorials/mozsdk/observerserv.html XulPlanet article]).
-==Observer class==+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 [http://xulplanet.com/references/xpcomref/ifaces/nsIObserver.html 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. Each observer implements [http://xulplanet.com/references/xpcomref/ifaces/nsIObserver.html 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.
- function myObserver()+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.
- {+
- 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 & Register==+<pre>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");
 + }
 +}</pre>
 + 
 +==Instantiate and register==
Do the following in your init function (probably an onload handler) for each window: Do the following in your init function (probably an onload handler) for each window:
- var observer = new myObserver();+<pre>var observer = new myObserver();
- observer.register();+observer.register();</pre>
-To do things cleanly, you should also unregister your observer when the window is closed.+To do things cleanly, you should also unregister your observer when the window is closed (you must!)
==Notify all observers== ==Notify all observers==
- Components.classes["@mozilla.org/observer-service;1"]+You can send a notifications to all observers using nsIObserverService::notifyObservers():
- .getService(Components.interfaces.nsIObserverService)+ 
- .notifyObservers(null, "myTopicID", "someAdditionalInformationPassedAs'Data'Parameter");+<pre>Components.classes["@mozilla.org/observer-service;1"]
 + .getService(Components.interfaces.nsIObserverService)
 + .notifyObservers(null, "myTopicID", "someAdditionalInformationPassedAs'Data'Parameter");
 +</pre>
==References== ==References==
*[http://xulplanet.com/tutorials/mozsdk/observerserv.html An article on observers at XulPlanet] *[http://xulplanet.com/tutorials/mozsdk/observerserv.html An article on observers at XulPlanet]
-*[http://xulplanet.com/references/xpcomref/ifaces/nsIObserver.html nsIObserver] interface+*[http://xulplanet.com/references/xpcomref/ifaces/nsIObserver.html nsIObserver interface]
-*[http://xulplanet.com/references/xpcomref/ifaces/nsIObserverService.html nsIObserverService] interface+*[http://xulplanet.com/references/xpcomref/ifaces/nsIObserverService.html nsIObserverService interface]

Revision as of 01:08, 27 November 2004

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.

Contents

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();
observer.register();

To do things cleanly, you should also unregister your observer when the window is closed (you must!)

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

References