nsIObserverService

This interface provides methods to add, remove, notify, and enumerate observers of various notifications.
Inherits from: nsISupports Last changed in Gecko 0.9.6

The XPCOM nsObserverService implements this interface to provide global notifications for a variety of subsystems.

Implemented by @mozilla.org/observer-service;1 as a service:

var observerService = Components.classes["@mozilla.org/observer-service;1"]
                      .getService(Components.interfaces.nsIObserverService);

Method overview

void addObserver ( in nsIObserver anObserver, in string aTopic, in boolean ownsWeak);
nsISimpleEnumerator enumerateObservers ( in string aTopic );
void notifyObservers ( in nsISupports aSubject, in string aTopic, in wstring someData );
void removeObserver ( in nsIObserver anObserver, in string aTopic );

Methods

addObserver()

Registers a given listener for a notifications regarding the specified topic. See nsIObserver for a JavaScript example.

void addObserver(
  in nsIObserver anObserver,
  in string aTopic,
  in boolean ownsWeak
);
Parameters
anObserver
The nsIInterface object which will receive notifications.
aTopic
The notification topic or subject.
ownsWeak
If set to false , the nsIObserverService will hold a strong reference to anObserver . If set to true and anObserver supports the nsIWeakReference interface, a weak reference will be held. Otherwise an error will be returned. (In most cases, you should use false .)

enumerateObservers()

Called to enumerate all observers registered for a particular topic.

nsISimpleEnumerator enumerateObservers(
  in string aTopic 
);
Parameters
aTopic
The notification topic or subject.
Return value

Returns an enumeration of all registered listeners. See nsISimpleEnumerator .

notifyObservers()

This method is called to notify all observers for a particular topic. See Example .

void notifyObservers(
  in nsISupports aSubject,
  in string aTopic,
  in wstring someData 
);
Parameters
aSubject
A notification specific interface pointer. This usually corresponds to the source of the notification, but could be defined differently depending on the notification topic and may even be null .
aTopic
The notification topic. This string-valued key uniquely identifies the notification. This parameter must not be null .
someData
A notification specific string value. The meaning of this parameter is dependent on the topic. It may be null .

removeObserver()

This method is called to unregister an observer for a particular topic.

void removeObserver(
  in nsIObserver anObserver,
  in string aTopic 
);
Parameters
anObserver
The nsIObserver instance to remove.
aTopic
The notification topic or subject. This string-valued key uniquely identifies the notification. This parameter must not be null .

Example

This notifies all nsIObserver s watching the "myTopicID" topic with an additional data parameter.

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

See also

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值