[Project_owners] Global listeer

Michael Vincent van Rantwijk, MultiZilla mv_van_rantwijk at yahoo.com
Thu Nov 15 01:31:51 PST 2007


Nickolay Ponomarev wrote:
> On Nov 15, 2007 11:06 AM, Michael Vincent van Rantwijk, MultiZilla
> <mv_van_rantwijk at yahoo.com> wrote:
>>> //register Observer
>>> window.addEventListener("load", MyObserver, false);
>>> function MyObserver(){
>>> var observerService = Components.classes["@mozilla.org/observer-service;1"]
>>>         .getService(Components.interfaces.nsIObserverService);
>> Don't do that here, but outside the observer; because it will 'run' for
>> all loads!
>>
> Huh?

This is two fold as moving "var observerService" out of the observer 
scope, makes the observer a few ms faster, and there is probably no need 
to add a new observer for each time the load event is triggered.

>>>    1. how can i find that observer is registered before or not ?
>> Have you looked at enumerateObservers in nsIObserverService.idl already?
>>
> Oops. Still you shouldn't use it, at least in your case. (I question
> the kosherness of its in-tree uses).

Choice is something good in my books, and it is an option, but I would 
rather use the other tip (see link) myself.

>>>    2. when i register observer only when first window opens every thing
>>>    is ok, when i open some other windows still every thing is ok and observer
>>>    works perfectly, but when i close first window while other windows are still
>>>    open the observer doesn't work anymore ?!
>> There is a hidden window which you can use, or better use a (JavaScript)
>> component to store your global vars and observers, which makes listening
>> for opening and closing of (browser) windows also a lot easier.
>>
> Can we stop suggesting people use the hidden window? It's an
> implementation detail really, not to be used by extensions.

Well well... do we make progress ;)

-- 
Michael Vincent van Rantwijk
- MultiZilla Project Team Lead
- XUL Boot Camp Staff member (ActiveState Training Partner)
- iPhone Application Developer



More information about the Project_owners mailing list