nsIObserver

« XPCOM API Reference

概要

nsIObserver は、オブジェクトが通知を監視するために使われます。 これらの通知は、いつもではなく時々、nsIObserverServiceを通してブロードキャストされます。

Please add a summary to this article.
  最終更新: Gecko 0.9.6

インターフェース・コード

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

メソッド

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.

関連インターフェース

nsIObserverService

サンプルコード

The following is an implementation of nsIObserver that can be registered with the preference service to be notified of changes in preferences (see Using preferences on MozillaZine for a complete example and for more information about the preference system in general).

var prefObserver = {
  // nsIObserver
  observe: function (aSubject, aTopic, aData) {
    if (aTopic == "nsPref:changed") { // observe preference changes
      // aData contains the name of the changed preference
      dump(aData+" changed!");
    }
  },

  QueryInterface: function(aIID) {
    if(!aIID.equals(CI.nsISupports) && !aIID.equals(CI.nsIObserver))
      throw CR.NS_ERROR_NO_INTERFACE;
    return this;
  }
};

See also Using observers.

Document Tags and Contributors

Contributors to this page: Okome
最終更新者: Okome,