Using observers: Difference between revisions

From MozillaZine Knowledge Base
Jump to navigationJump to search
m (down a level)
(content moved to mdc)
 
Line 1: Line 1:
{{extdev}}
See [http://developer.mozilla.org/en/docs/nsIObserver nsIObserver] and [http://developer.mozilla.org/en/docs/nsIObserverService nsIObserverService] at [http://developer.mozilla.org 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 [http://xulplanet.com/tutorials/mozsdk/observerserv.php XulPlanet article]).
[[Category:Redirects]]
 
There are two interfaces in the Mozilla platform related to observers system: <code>nsIObserverService</code> which is available as a service, and <code>nsIObserver</code>, which is what you'll need to implement.
 
==Observer object==
Each observer implements <code>[http://xulplanet.com/references/xpcomref/ifaces/nsIObserver.html nsIObserver]</code> interface. It may register for one or more '''topics'''. For each notification, its <code>observe</code> 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 <code>register</code> and <code>unregister</code> convenience functions, which are not a part of nsIObserver interface. Including these functions in the observer object is a matter of taste.
 
<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:
var observer = new myObserver();
Note: <code>myObserver</code>'s <code>register</code> 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 <code>onunload</code> 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 <code>nsIObserverService.notifyObservers()</code>:
<pre>
Components.classes["@mozilla.org/observer-service;1"]
        .getService(Components.interfaces.nsIObserverService)
        .notifyObservers(null, "myTopicID", "someAdditionalInformationPassedAs'Data'Parameter");
</pre>
 
==Advanced Topics==
The observer service should only be used from the main (UI) thread.
 
==References==
*[http://xulplanet.com/tutorials/mozsdk/observerserv.php An article on observers at XulPlanet]
*[http://xulplanet.com/references/xpcomref/ifaces/nsIObserver.html nsIObserver interface]
*[http://xulplanet.com/references/xpcomref/ifaces/nsIObserverService.html nsIObserverService interface]
 
[[Category:Example code]]

Latest revision as of 20:29, 23 May 2007